Lesson 14 Rotary Encoder

Share for us


In this experiment, you will learn how to use rotary encoders. A rotary encoder is an electro-mechanical device that converts the angular position or motion of a shaft or axle to an analog or digital code. Rotary encoders are usually placed at the side which is perpendicular to the shaft. Rotary encoders act as sensors for detecting angle, speed, length, position and acceleration in automation field.


– 1 * SunFounder Uno board

– 1 * USB cable

– 1 * Breadboard

– 1 * Rotary encoder module

– Jumper wires


There are two main types of rotary encoders: absolute and incremental (relative). The output of absolute encoders indicates the current position of the shaft, making them angle transducers. The output of incremental encoders provides information about the motion of the shaft, which is typically further processed elsewhere into information such as speed, distance, and position.

In this experiment, an incremental encoder is used.

An incremental encoder is a rotary sensor intended to turn rotational displacement into a series of digital pulse signals which are then used to control the angular displacement. It generates two-phase square waves whose phase difference is 90°. Usually the two-phase square waves are called channel A and channel B as shown below:

It’s difficult to distinguish left turn and right turn during SCM programming. However, when using an oscilloscope to observe the left and right turn of a switch, you will find that a phase difference exists between the signals of the two output pins. The phase difference is shown as follows:

If both channel A and channel B are high, the switch rotates clockwise; if channel A is high and channel B is low, the switch rotates counterclockwise. As a result, if channel A is high, you can judge whether the rotary encoder turns left or right as long as you know the state of channel B.

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

For the convenience of wiring, a rotary encoder module is used. Connect pins + and GND of the rotary encoder module to pins 5V and GND of the SunFounder Uno board, and pins CLK and DT of the module to digital pins 2 and 3 of the board

Step 2: Program (please go to our official website www.sunfounder.com to download related code by clicking LEARN -> Get Tutorials)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

You will see the angular displacement of the rotary encoder printed on the serial monitor of the computer. When you turn the shaft of the rotary encoder clockwise, the angular displacement is increased; when counterclockwise, it’s decreased. If you press down the shaft, it is reset and restores to the initial state.


//Rotary Encoder/*You will see the angular displacement of the rotary encoder printed on Serial Monitor.
When you turn the rotary encoder clockwise, the angular displacement is increased;
when turn it counterclockwise, the displacement is decreased.
If you press the switch on the rotary encoder, the readings will return to zero.*/
//Email: support@sunfounder.com
//Website: www.sunfounder.com
//2015.5.7 const int clkPin= 2; //the clk attach to pin 2
const int dtPin= 3; //the dt pin attach to pin 3
const int swPin= 4 ;//the sw pin attach to pin 4 int encoderVal = 0; void setup()
//set clkPin,dePin,swPin as INPUT
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600); // initialize serial communications at 9600 bps } void loop()
int change = getEncoderTurn();//
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)//if button pull down
encoderVal = 0;
} int getEncoderTurn(void)
static int oldA = HIGH; //set the oldA as HIGH
static int oldB = HIGH; //set the oldB as HIGH
int result = 0;
int newA = digitalRead(clkPin);//read the value of clkPin to newA
int newB = digitalRead(dtPin);//read the value of dtPin to newB
if (newA != oldA || newB != oldB) //if the value of clkPin or the dtPin has changed
// something has changed
if (oldA == HIGH && newA == LOW)
result = (oldB * 2 – 1);
oldA = newA;
oldB = newB;
return result;