Lesson 28 Rotary Encoder

Share for us

Introduction

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.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Rotary Encoder module

– 1 * 5-Pin anti-reverse cable

Experimental Principle

There are mainly two types of rotary encoder: absolute and incremental (relative) ones. 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, we will use the latter.

An incremental encoder is a rotary sensor 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 is difficult to distinguish between the left turn and right turn during SCM programming. However, when using an oscilloscope to observe the left turn and right turn of a switch, you will find a phase difference between the signals of the two output pins as shown below.

It shows that if output 1 is high and output 2 is high, then the switch rotates clockwise; if output 1 is high and output 2 is low, then the switch rotates counterclockwise. As a result, during SCM programming, if output 1 is high, then you can tell whether the rotary encoder rotates left or right as long as you know the state of output 2.

Experimental Procedures

Step 1: Build the circuit

The wiring between the rotary encoder and SunFounder Uno board:

Rotary EncoderSunFounder Uno
CLK2
DT3
SW4
VCC5V
GNDGND

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

Now, you can see the angular displacement of the rotary encoder printed on Serial Monitor. When the rotary encoder rotates clockwise, the angular displacement increases; when it does counterclockwise, the value decreases. Press the switch on the rotary encoder, the value will return to zero.

Code

/***************************************************************
* Name:Rotary Encoder
* Function: You will see the angular displacement of the rotary encoder printed on the serial monitor of the computer.
* When the rotary encoder turns clockwise, the angular displacement is increased;
* when it turns counterclockwise, it’s decreased.
* If you press the switch on the rotary encoder, related readings will return to zero.
******************************************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
const int clkPin= 2; //the clk attach to pin2
const int dtPin= 3; //the dt attach to pin3
const int swPin= 4 ;//the number of the buttonint 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)
  {
    encoderVal = 0;
  }
  Serial.println(encoderVal);}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;