EE109 – Fall 2022 Introduction to Embedded Systems

EE109 – Fall 2022: Introduction to Embedded Systems

Lab 7

Rotary Encoders and Interrupts

Honor Statement

This is an individual assignment. The code you write, use for the demo and submit should be wholly your own and not produced by working in teams or using, in part or in whole, code written by someone else (found online, from a fellow student, acquaintance, etc.). You will be asked to verify this when you submit your code on Vocareum by signing your name in the honor.txt file on Vocareum. Penalties for violation include a 0 on the assignment and a potential further deduction or referral to Student Judicial Affairs.


In this lab exercise you will learn how rotary encoders work. The encoder will be used to control the incrementing and decrementing of a signed variable and the value of that variable will be displayed on the LCD shield. Turning the encoder in one direction increments the count, turning in the opposite direction decrements it. Each time the displayed value is a multiple of 8, a brief tone will be generated by a buzzer on the breadboard. In the second part of the lab, the performance of the program will be improved by using interrupts to monitor the inputs from the encoder.

To see a short video demonstrating the operation of lab, click here.

For a copy of the Lab 7 grading sheet, click here.

Getting Started

Set up the Lab 7 assignment the same as has been done previously by creating a lab7 folder in your ee109 folder.

You will need the rotary encoder and the buzzer from the parts bag in your tool kit. IMPORTANT: If the buzzer is new, it may have a piece of yellow plastic over the top of it. This needs to be peeled off before using it or the buzzer output will be very hard to hear.

The Rotary Encoder (Background Covered in Class)

A rotary encoder is used to determine the angular position of something that rotates. For example, you are building a weather monitoring system and one of the devices providing input to the system is a weather vane that rotates to point in the direction the wind is blowing. The weather vane could be attached to a rotary encoder and the system would read the inputs from the encoder to see what angle the vane is pointing. Rotary encoders are also commonly used in devices that have a knob on them that the user needs to turn to adjust something.

Rotary encoders come in two types: absolute encoders and incremental encoders. An absolute encoder has outputs to provide a binary number for each angular position of the shaft (0000 = 0°, 0001 = 22.5°, 0010 = 45°, etc.) The number of bits the encoder provides determines the resolution of the angle that can encoded. For example a 10-bit absolute encoder can resolve angles to 360 / 1024 = 0.35°.

Incremental encoders, also called quadrature encoders, are also available in a variety of resolutions (16, 64, 256, etc. per revolution) but they only provides information about whether the shaft has been turned clockwise or counter-clockwise from the previous position, nothing about the actual position of the shaft. As the shaft of one of these encoders is turned, it opens and closes two switches to generate two binary signals.

The two switches can be used (when pull-up resistors are installed) to produce two signals that both look like a 50% duty cycle square wave (see below) but they are 90° out of phase with each other, which gives the appearance when drawn that one of the square waves is lagging behind the other by one quarter of a period.

To see how this can work, examine the diagram below and move along the waveforms from left to right as they would be generated when the control is being rotated clockwise. If we consider the two signals as the outputs of state machine where B is the MSB and A is the LSB, then the output code goes through the sequence 00, 01, 11, 10, 00, 01, etc. as the control is rotated.

Now try moving along the waveforms from right to left (control being rotated counter-clockwise) and we can see that the output code goes through the sequence 00, 10, 11, 01, 00, etc. as the control is rotated. Note that the sequence is different depending on whether the control is being rotated clockwise or counter-clockwise. In fact every one of the transitions from one two-bit code to another in the clockwise direction is different from the ones that appear when rotating counter-clockwise. As a result whenever there is a code change the direction of rotation can be determined by knowing the old code and the new code.

The above sequences of numbers is called a Gray code and has the property that as you go from one number in the sequence to the next, in either direction, only one bit changes at every transition. Gray codes, usually with many more than two bits in them, are also used in absolute rotary encoders used to encode the position information in electromechanical devices such as scanners, printers, manufacturing equipment and many others. The property that only one bits changes at a time helps to eliminate errors in sensing the device's position. If a normal binary code was used there would be numerous places in the sequence where multiple bits would have to change simultaneously. With mechanical equipment, it's impossible to ensure that all the bits would change at exactly the same time and this can lead to errors as to the position or status of the device.

Using the two-bit Gray code number as a state variable we can construct the state diagram below for a state machine that describes the output of the encoder. Each of the four states corresponds to one of the four sets of output values the control can generate. From each state there are two transitions to an adjacent state, one for each of the code bits that might change. One transition is for clockwise rotation and the other is for counter-clockwise rotation. The direction of rotation is marked on the transition line.

The encoders provided for the lab exercise go through 64 states in the process of one revolution of the knob. The pins on the bottom are arranged with two pins (A and B) on one side, and the pin that goes to ground is on the opposite side. It's not too important which pin is A or B since swapping them only makes the state machine count in the opposite direction for a given direction of rotation.

Task 1 - The Circuit

Install the rotary encoder on the breadboard being careful to make sure that all three pins are in different sets of the five hole connections strips (see below). Make the connections between the encoder and the Arduino as shown in schematic diagram below.

Two I/O port bits will be required for reading the encoder output into the Arduino, and one will be required for the buzzer output. To standardize things, everyone should use the A1 and A5 bits (Port C, bits 1 and 5) for the encoder inputs and D12 (Port B, bit 4) for the buzzer output.

The buzzer has two pins on the bottom and these should be placed in two separate 5-hole connection block as shown below. There is no polarity to the buzzer so it doesn't matter which way it is oriented.

Use your male-female jumpers to make the four connections needed between the breadboard and the Arduino as shown in the schematic.

When connecting the buzzer make sure you are using the correct pin on the LCD for D12 (PB4). The D12 pin is marked in white lettering along the row of pins that stick up on the LCD. Don't plug a wire into the black connector above where the Arduino is marked "12" since this is actually the "RX" input to the Arduino and if there is a signal present on that input it can interfere with the "make flash" step.

Task 2 - Playing the Musical Notes

The buzzer will be used to generate the musical notes. The lab7.c file contains a skeleton of the routine, play_note for playing the tones.

For the buzzer to create a tone at a specified frequency, it must be driven by a digital squarewave (50% duty cycle) signal at the proper frequency. The frequency of the squarewave can be adjusted by changing the amount of time the signal is in both the high state and the low state by using the the delay functions in the same way you created a specified waveform in Lab 3. For example, to create a 1kHz squarewave, the period needs to be 1ms, so the signal is high for 0.5ms = 500μs and then low for 500μs before repeating. Our buzzer is connected to the Arduino port D12 (Port B, bit 4) so the code below would work.

    while (1) {  // generate an infinite sequence 1KHz cycles
        PORTB |= (1 << PB4);
        PORTB &= ~(1 << PB4);

The less time it is both high and low, the shorter the period and the higher the frequency. If the signal spends more time high and low, the frequency will be lower.

The "lab7.c" file contains an array of 8 "short" (16-bit) numbers that define the frequencies (in Hz.) of each of the tones that can be generated. These are the tone frequencies that your program should use whenever it generates one of the 8 tones.

    unsigned short frequency[8] = 
        { 262, 294, 330, 349, 392, 440, 494, 523 };

The time for one period of the signal in microseconds is then given by 1,000,000 divided by the frequency value. Once you know the time in microseconds that the buzzer signal should be high and low for one cycle you can generate a output signal as shown above.

Unfortunately the _delay_us function only works with constant values as arguments, like _delay_ms(100);. It does NOT work with an argument that's a variable, or the result of a calculation involving a variable. Let's not get into the details of why this is so, and just work around it. The lab7.c file contains a replacement delay function, variable_delay_us that does accept a variable argument at the expense of slightly less accuracy, which we can live with. Write your squarewave generatation code using the variable_delay_us function and pass to it the number of microseconds you want to delay (i.e. half of a period)

Doing the above will generate one period of the tone. Inside the play_note routine it should loop and generate enough periods of the tone to fill one second. You already know how many to generate since that is just the frequency of the signal (number of periods of the signal per second.)

The play_note routine will have to do the following:

Once you have the play_note function working you should test it by itself. Try testing it with some code like this that just loops calling play_note.

    while (1) {

This should generate the 262Hz tone for one second and then turn off for 0.5s, and then repeat. If that is working you can then try something like this to play all the notes in sequence.

    while (1) {
        unsigned char i;
        for (i = 0; i < 8; i++) {

Once you are sure you can generate tones at different frequencies you can then proceed to make the rotary encoder select which tone to play.

Task 3 - Confirm Encoder Operation on Oscilloscope

The next task is to use two channels of the oscilloscope to confirm that the encoder outputs are acting correctly as the knob is turned. All that's required from your software for this test is for it to enable the pull up resistors on the encoder input ports, PC1 and PC5.

Try rotating the encoder slowly and see if both traces on the scope go up and down as it's rotated. The scope is showing the states of two encoder output bits. If you rotate slowly you should be able to see it go through the 2-bit Gray code sequence as discussed in class: 00 → 01 → 11 → 10 → 00 …. The main thing to look for is to confirm that both outputs are changing as the encoder is rotated and that the signal levels cover most of the range from ground to 5 volts.

Task 4: Counting Encoder State Changes Using Polling

Your program will contain a signed variable that starts with a value of zero and then increments or decrements by one for each state change of the encoder outputs. Turning the encoder in one direction increments the count, turning in the opposite direction decrements it. Since there are 64 encoder state changes for each revolution, the count should go up or down 64 each time the encoder is turned one revolution. The count value is not limited to 64, it should continue to grow (positive or negative) as the encoder is rotated. The encoder count will be used to determine the frequency of the tones to be played, but for this task we are not generating the tones, just making sure it counts correctly.

At the start of the lab7.c file some variables are defined for you.

This version of the program uses polling to watch the two input bits (Port C, bits 1 and 5) connected to the encoder outputs. The beginning of the program is as follows.

Following the above code is the main loop of the program:

When you run the program you should be able to rotate the knob back and forth and see the numbers change on the display.

Task 5 - Generating Tones Based on Encoder Count

Once you are sure the encoder is operating properly and the count value is showing the state changes, you can add the code to generate the tones. The count value is use to determine which musical note is played. Every time the count value is a multiple of 8 (positive or negative) it will play one of eight musical notes for one second. If count is zero, play tone 0 (262Hz). If count is 8 or -8, play tone 1 (294Hz). If count is 16 or -16 play tone 2 (330Hz), etc. Once the count values reach 64 or -64 the sequence should repeat. For example at a count of 64 play the same note as at a count of 0, and at 72 (64+8) play the same note as for a count of 8, etc. The note number (0-7) is defined as:

     (|count| modulo 64) / 8

For this task add code to your program to do the following:

Once you have it working, check the following:

The Problem With Polling

Using polling to read the encoder inputs works reasonably well but it has an problem that can easily be demonstrated. As the count value is increased or decreased, it has to stop at each multiple of eight and wait for the tone to play before it can move on. While the program is playing the tone, it is not responding to any of the encoder inputs so the count can not change. We would prefer to have the program continue to respond to the encoder, and update the internal count value, while the tone is playing. Once the tone is done playing, it can then show the new count value on the LCD.

The best way to solve this problem is to make the encoder inputs trigger an interrupt. Regardless of what the program is doing at the time, the interrupt will cause execution to temporarily go to the interrupt service routine to handle the state change. The program can service the interrupt while executing other code and this should prevent encoder state transitions from being lost.

To implement this we will use the Pin Change Interrupt capability of the microcontroller. All of the I/O pins in the three ports (B, C and D) are capable of generating an interrupt if the pin does a 0→1 or 1→0 transition. While individual pins in a port can trigger an interrupt, there is only one interrupt vector (and associated Interrupt Service Routine) for each port.

Pin Change Interrupts for a port are enabled by setting the PCIE0, PCIE1 or PCIE2 bits in the PCICR register. In addition there is an 8-bit mask register (PCMSK0, PCMSK1 and PCMSK2) associated with each port that determines which pins can generate an interrupt. If a bit in the mask register is set to a one, then a 0→1 or 1→0 transition on the corresponding bit in the I/O port will cause a Pin Change Interrupt. If a bit in the mask register is a zero, then changes on the corresponding I/O port bit will not cause an interrupt.

The Pin Change Interrupts are ideal for our application of watching for state changes on the two bits from the encoder. Once the Pin Change Interrupts are enabled on the two encoder bits, any change of state will cause the interrupt to occur. The ISR can include the code that determines what state change happened and whether to increment or decrement the count value.

Task 6 - Modify the Program to Use Interrupts

Before modifying your program to use interrupts, make a copy of your lab7.c just in case you have to go back to it for some reason. To change it to use interrupts, make the following modifications to the program.

Any variable that needs to be accessed from both the main program and the ISR must be declared as a global variable at the top of the program (not in the main routine). In addition these variables should be declared with the "volatile" keyword to tell the compiler that their contents can change outside the main stream of code execution.

After the changes have been incorporated into the program, try running it and confirm that rotating the encoder still makes the count value go up and down. Once that is working, then try the experiment of rotating the control to reach a multiple of eight, and then continuing to rotate the control while the tone is playing. The count value on the LCD won't update while the tone is playing, but once it is finished, the new count value should be displayed.


When your program is running you should be able to confirm the following

The answers to the review questions below should be edited into the Lab7_Answers.txt file. The Lab7_Answers.txt file and all source code (lab7.c that uses interrupts, lcd.c, lcd.h and Makefile) must be uploaded to the Vocareum web site by the due date. See the Assignments page of the class web site for a link for uploading.

Please make sure to save all the parts used in this lab in your project box. These will be needed for labs throughout the rest of the semester. We suggest also saving all the pieces of wire you cut and stripped since those will be useful in later labs.

Review Questions

Answer the following questions and submit them in a separate text file ("Lab7_Answers.txt") along with your source code.

  1. Briefly explain why it is undesirable to read the two encoder input signals with two separate PINC statements.
  2. Briefly explain why updating the LCD display in your ISR could lead to undesirable behavior for this lab.