Lab 5: Interrupts

Introduction

In this lab you will use your MCU to determine the speed of a motor by reading from a quadrature encoder using interrupts.

Learning Objectives

By the end of this lab you will have…

  • Implemented an algorithm to sense quadrature encoder pulses and convert those pulses into a motor velocity.
  • Written code to display the measured velocity and direction to a user with an update rate of at least 1 Hz.
  • Understood the quadrature encoder protocol and implemented a multi-interrupt routine on the MCU to interface with it.

Requirements

Build a system to determine the angular velocity of a brushed DC motor. Use your MCU, a quadrature encoder, and a motor. The MCU should use interrupts to detect pulses from the encoder and use them to calculate a direction and velocity. Ensure that the system remains functional when the motor is spinning at very high and very low speeds. Display the direction and angular velocity of the motor in revolutions/s with an update rate of at least 1 Hz.

In addition to the standard deliverables, your report should include a flowchart (see examples here) showing each interrupt and the main loop that are executed. Test your system with the motor provided in the lab which spins at a known speed of approximately 10 rev/s.

Encoder Output Voltage Warning

The encoders used for this lab operate at a 5 V logic level. The STM32L432KC uses 3.3 V, but has a few pins that are 5V tolerant. Make sure to use those pins or a voltage conversion circuit so you don’t damage your board. The 5V tolerant pins are listed in Tables 13-14 in the STM32L432KC Datasheet).

Discussion

Quadrature encoders are a common type of encoder used to measure the relative or absolute angle of a motor. They do this by adding a patterned disk to the motor and tracking how fast the disk rotates. Using two sensors that are 90° degrees out of phase provides higher angular resolution and can be used to determine the direction of rotation.

Figure 1: Animation of quadrature encoder.

All quadrature encoders contain two stationary digital sensors, and a disc that rotates with the motor. The encoders you will use in this lab use hall effect sensors and magnets. You should design your system to take full advantage of both signals to get the highest resolution measurement.

Credits

This lab was original developed in 2024 by Kavi Dey ’26 and Prof. Josh Brake.

Share Your Feedback

Share Your Feedback!

If you caught any typos or have any suggestions for this page, please open an issue on the website Github repository. Click the link here for instructions on how to create an issue.