Lab 6
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.
Introduction
In this lab exercise you will learn how rotary encoders work and how the performance of the program can be improved by using interrupts rather than polling to monitor the inputs from the encoder.
The encoder will be used to control the incrementing and decrementing of a number that will be displayed on the LCD. The value of that number determines how long the program will take to play a sequence of 25 tones on the buzzer. Turning the encoder in one direction increments the count, turning in the opposite direction decrements it. The number on the LCD is the time it will take to play the sequence in tenth of seconds.
For a copy of the Lab 6 grading sheet, click here
Displaying binary numbers
As part of this lab you will have to display on the LCD a number that shows the
value of count variable that is incremented or decremented by turning the
rotary encoder. Up to this point we have only displayed constant strings like
“Hello there” by using the lcd_stringout function. So how can we display the
value of variable that contains some binary value? Many people try doing the
following only to find out that it doesn’t work.
int x = 1234; lcd_stringout(x);
The “x” variable contains the value 1234. It’s not the name of an array containing the ASCII codes for ‘1’,’2’, ‘3’, ‘4’ and then a zero byte. If we try the above code the LCD will end up with either nothing or garbage displayed on it, and the Arduino’s program may stop running.
It’s important to remember that the LCD only displays ASCII characters.
Any other type of value must somehow be converted into an array of ASCII
characters, a C string, before it can be displayed. In the section below we
will discuss the snprintf function which is used to convert binary variables
to a C string of ASCII codes.
Creating character strings
Since the LCD is used to display strings of characters, it’s necessary to have
a efficient way to create the strings to be displayed. Constant strings are no
problem and can be given as an argument to the lcd_stringout function.
lcd_stringout("Hello, world");
It gets more complicated when you need to create a string containing variable values, such as numbers. For example, in C++ when you write
cout << "Answer is " << x << endl;
cout is converting the numeric value of x into the ASCII representation of that number.
In C, the best tool for creating character strings is the snprintf function
that is a standard part of most C languages. Important: In order use
snprintf, or any other routine from the C standard I/O library, you must have
the following line at the top of the program with the other #include
statements.
#include <stdio.h>
The snprintf function is called in the following manner
snprintf(buffer, size, format, arg1, arg2, arg3, ...);
A quick example is:
char buf[17]; int x = /* some value */ snprintf(buf, 17, "Answer is %d", x); lcd_stringout(buf);
The arguments for snprintf are:
buffer
The destination char array large enough to hold the resulting string including the NULL byte that has to be at the end. For example, if you want to store 4-digit numbers in a array, the array must be at least 5 elements long.
size
The size of the destination char array. This tells the snprintf program the maximum number of character it can put in buffer regardless of how many you try to make it do. Keep in mind that the snprintf functions always puts a NULL byte at the end of the string it creates. If you call snprintf with a “size” argument of five, and try to put “12345” in the array, you will end up with the string “1234”.
format
The heart of the snprintf function is a character string containing formatting codes that tell the function exactly how you want the following arguments to be formatted in the output string. More on this below.
arguments
After the format argument comes zero or more variables containing the values to be placed in the string according to the formatting codes that appear in the format argument. For every formatting code that appears in the format argument, there must be a corresponding argument containing the value to be formatted.
The Format Argument in snprintf
The format argument tells snprintf how to format the output string. Typical format arguments can contain plain text that is copied verbatim to the output buffer, and formatting codes that specify how to translate the variables into ASCII character codes. There are a large number of formattting codes that can be used depending on how an argument is to be converted to ASCII codes. The formatting codes all start with a percent sign and for now we will only be working with two of them (and a simple variation of one of them):
-
%d - Used to format decimal integer numbers. When this appears in the format string, the corresponding argument will be formatted as an decimal integer number and placed in the output string. It will only place as many characters in the buffer as needed to display the number. A number from 10 to 99 will take up two places, a number from 100 to 999 will take up three places, etc.
A useful variation of this is to specify a minimum field width by using the form “%nd” where the ‘n’ is the minimum number of spaces the converted number should occupy. If it takes less space than ‘n’ characters it will be right justified in the ‘n’ character field with spaces to the left. For example, a format of “%4d” will print an argument of 65 as “ 65” with the number right justified and two leading spaces.
-
%s - Used to format string variables. When this appears in the format string, the corresponding argument will be assumed to be a string variable and the whole string variable will be copied into the output string.
The format string must have the same number of formatting codes as there are arguments that follow the format argument in the function call. Each formatting code tells how to format its corresponding argument. The first code tells how to format “arg1”, the second code is for “arg2”, etc. Anything in the format argument that is not a formatting code will be copied verbatim from the format string to the output string.
Example: Assume you have three unsigned char variables containing the month, day and year values, and a string variable containing the day of the week, and you want to create a string of the form “Date is month/day/year = dayofweek”.
char date[30]; unsigned char month, day, year; char *dayofweek = "Wednesday"; month = 2 day = 14; year = 18; snprintf(date, 30, "Date is %d/%d/%d = %s", month, day, year, dayofweek);
After the function has executed, the array “date” will contain
Date is 2/14/18 = Wednesday
Since you told snprintf that the size of the array was 30, the maximum number of characters it will put in the array is 29 since as with all C strings, the string has to be terminated with a null byte (0x00).
An additional example is shown here:
char x = 10, char y = 12; char z = x - y; char buf[12]; // make sure you add one spot for the null character snprintf(buf, 12, "%d-%d=%d", x, y, z); // yields "10-12=-2" but not on the LCD lcd_stringout(buf); // prints "10-12=-2" on the LCD
Beautifying Your Output
Often times we may want a number to take up a fixed number of spaces on the
screen. For example if we print out 9 and then 10 the fact that 10 grew
into a 2nd digit may make the visual layout look sloppy. We might want all our
numbers to use 2 digits (if we know in advance that they will be in the range
00-99) or 3 digits (if we know the range will be 0-255). To do this we can add
modifiers to the %d format string modifier.
-
%2dwill print the decimal number using 2 spaces leaving the first space blank for single-digit values 0-9. -
%3dwill print the decimal number using 3 spaces leaving the first one or two blank.
We can also set the string to use 0-padding to fill in blank spaces with 0s.
%03dwill print the decimal number using 3 spaces if needed and fill in unused digits with0s. For example,5would be printed as005, while34would be printed as034.
A good reference for format strings can be found here
The Rotary Encoder (Background Covered in Introductory Video)
This section is a brief introduction to the rotary encoder that is discussed in more detail in the introductory video that should be watched before attempting to do this lab assignment.
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 can be attached to a rotary encoder and the system reads 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 inside the encoder that generate two binary signals.

The two switches can be used (when pull-up resistors are installed) to produce as the encoder is rotated 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
You will need the rotary encoder, buzzer and a button from the parts bag in your tool kit. The rotary encoder has a blue square base with three pins sticking out and a silver or black knob attached to it. The buzzer is round and black with two pins on the bottom.
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, one will be required for the buzzer output, and one for the button input. To standardize things, everyone should use the A1 and A5 bits (Port C, bits 1 and 5) for the encoder inputs, D12 (Port B, bit 4) for the buzzer output, and D2 (Port D, bit 2) for the button input.

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.
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.

Use your male-female jumpers to make the five 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.

Install a button on the breadboard and connect it to Group D, bit 2 (PD2) on the Arduino.
Task 2 - Complete the Counter Program
From the table in front of the podium, get a copy of the source code for a program that would run on the circuit you just built to increment or decrement a count value as the encoder is rotated. Your task is to complete the program by filling in the parts of the program that have been left blank.
-
Complete the two lines near the top that enable the Pin Change Interrupt for Port C, and then set the interrupt mask so only PC1 and PC5 in Port C can cause an interrupt.
-
Fill in the correct name for ISR that is invoked when there is a Pin Change Interrupt on Port C.
-
In the section that handles the encoder’s state transition, complete the code that sets the next state and changes the count value either up or down. Refer to the state diagram above for help.
When you have completed this task, show it to one of the instructors for credit.
Task 3: Producing Tones on the Buzzer
In this task you will add code to the lab6.c file to have it play a sequence
of 25 tones that rise in frequency over time span that you determine with the
encoder. Most of the code has been written for you but you will still have to
do the following parts of this task.
Set up the Lab 6 assignment the same as has been done previously by creating a lab6 folder in your ee109 folder.
- From the class web site, download the file
lab6.zip. This file contains lab6.c and Lab6_Answers.txt. - Extract the files into the
lab6folder. - Add a copy of Makefile, lcd.c and lcd.h from a previous lab to the lab6 folder.
- Modify the Makefile to work for the Lab 6 files.
Open the lab6.c file in your editor and make the additions and changes described
below.
In this task, we want you to practice creating strings that contain variable
values using snprintf(). Look for the portion of the code near the top
where it says to add a “splash screen”. In this section add code to display
two lines of text on the LCD. The first line on the LCD should display your
name (as much as will fit) on the first row. The second row should contain a
string with your birthday that was created using snprintf, similar to the
way the string above was formatted with a date using numerical values and the
“%d” format strings. It should be displayed somewhat centered on the bottom
row and not just start at the 0-th column of the row. This ensures you
understand how to use the lcd_moveto function. After both rows are displayed
on the LCD, make the program delay for 1 second, and then clear the screen.
<pre>
/* Call lcd_stringout to print out your name */
/* Use snprintf to create a string with your birthdate */
/* Use lcd_moveto to start at an appropriate column
in the bottom row to appear centered */
/* Use lcd_stringout to print the birthdate string */
/* Delay 1 second */
/* Use lcd_writecommand to clear the screen */
</pre>
Now add more code to the lab6.c as described below.
-
Provide the code to initialize TIMER1. Use your code from the stopwatch lab assignment to help complete this routine. The two actions this routine must do are:
- Set the bits to put TIMER1 in CTC mode.
- Enable interrupts by setting the OCIE1A bit to a one.
Do not set the prescalar bits in this function! Setting the prescalar bits starts the timer running and we don’t it running until we need a tone created. In addition you can’t set the OCR1A register to a value since that will change each time a tone is created at a different frequency. The OCR1A value will be determined by the
play_tonefunction. -
Write the ISR for TIMER1. The ISR has to perform two functions whenever it is invoked. A global variable
buzz_countthat is set in the main program tells how many times the ISR needs to be run. Each time the ISR is called this variable should be decemented. Ifbuzz_counthas not reached zero, the ISR should invert the bit that goes to the buzzer on Port B, bit 4. This creates the squarewave signal to make the buzzer output a tone. Ifbuzz_counthas reached zero, TIMER1 should be stopped so there are no more interrupts, and thebuzzingvariable must be cleared to zero. This tells theplay_tonefunction that this note has completed and the next can be played. -
We want to change the program from using polling to read the encoder to using Pin Change Interrupts. Do to this make following additions and change to the code. Your answers to Task 2 above provide most of this.
- Add code to top of the program to configure the Pin Change Interrupts on inputs PC1 and PC5.
- Create a ISR for Pin Change Interrupts on Port C.
- Move all the code currently in the main loop that reads the encoder inputs
and determines the next state and the value of the
countvarible to the new ISR.
Your program will contain a variable that 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. Your code will have to limit the value to being between 10 and 80. The number will be used to determine how quickly the sequence of tones is played.
At the start of the lab6.c file some variables are defined for you.
- An array to hold the frequencies in Hz. of the 25 tones to be played.
- Variables are defined for holding the encoder state information (
old_state, new_state) and a flag variable (changed) to indicate whether the encoder’s state has changed. - A variable,
countis defined to store the count value that the encoder will increment and decrement. It can be initialized to any value between 10 and 80.
The beginning of the program is as follows.
- Code to enable the pull-up resistors for the A1 and A5 input bits (Port C, bits 1 and 5), set Port B, bit 4 as output, and initialize the LCD.
- Enable the pull-up resistor for the button on PD2.
- Initialize the Pin Change Interrupts for the two encoder inputs.
- Initialize TIMER1 which is used to generate the tones.
- As with previous labs, write a splash screen showing the lab number and your name for one second, then clear the screen.
- Reads the two encoder bits to determine the initial current state of the encoder (and our initial position in the state machine.)
Following the above code is the main loop of the program which does two things.
-
If the ISR handling the encoder inputs has changed the count value (
changed == 1) check that the new count value is between 10 and 80, then print the new count value on the LCD. Use snprintf() to generate the string containing the count value and use lcd_stringout() to print it. -
If the buttons has been pressed, determine how long each tone should play based on the
countvalue, then loop through all 25 tones playing each one.
When you run the program you should be able to rotate the knob back and forth and see the numbers change on the display.
- Check that the numbers change smoothly as the encoder is rotated without skipping or repeating any values.
- These encoders go through 64 states per revolution. Confirm that rotating the knob one full revolution in either direction makes the count change by 64.
- Pressing the button should cause the sequence of 25 tones to be played.
Results
The answers to the review questions below should be edited into the Lab6_Answers.txt file. The Lab6_Answers.txt file and all source code (lab6.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 (“Lab6_Answers.txt”) along with your source code.
-
TIMER1 is to be used to output a 50% duty cycle signal (squarewave) for tone generation as was done in this lab. For the two tones specified below, determine a prescalar, the OCR1A value, and the number of interrupts to allow before stopping the timer. Your answer for the prescalar and OCR1A values should be the ones that give the most accurate timing.
- 200 Hz tone for 5 seconds
- 32 Hz tone for 2 seconds
-
Briefly explain why updating the LCD display in your ISR could lead to undesirable behavior for this lab.

