Sunday 5 November 2017

Rotary Encoder With Arduino



A Rotary Encoder is an electromechanical device that converts angular movement of the shaft to analog or digital code.
Rotary Encoders can be found in radio equipment like amateur radio or hand held, where non-stop or continuous 3600 rotation is required for tuning to right frequency.
In this project, the working of Rotary Encoder is explained with the help of Arduino. The following sections are dived into introduction to rotary encoders, circuit and working.

Circuit Diagram








Hardware Required

  • Rotary Encoder
  • Arduino UNO
  • LCD Display

Introduction to Rotary Encoders

A Rotary Encoder is an input device that provides the information about the direction and amount of rotation of the knob. There is a select switch associated with the rotary encoders that can be activated by pushing the knob.
The commonly used rotary encoder is an incremental rotary encoder. This type provides an instantaneous result about the rotating shaft by generating two square wave outputs that are 90 degrees out of phase with each other.
A rotary encoder has to coding pins A and B produce either High or Low output depending on the rotation and direction of the shaft.
The basic function of a rotary encoder can be explained by considering it as a combination of two switches that close or open due to rotation of the shaft.
As the shaft rotates, the switches are closed and opened in a pattern. If the outputs are treated as binary, then for clock wise rotation, the outputs at A and B will be 00, 01, 11 and 10.
For anticlockwise or counter clockwise rotation, the outputs will be 10, 11, 01 and 00. The waveform of the pulses in both directions of rotation is shown below.

To test the sequence, the output pins of the rotary encoder are connected to two LEDs as shown in the following circuit.
When the knob of the rotary encoder is rotated in clockwise or anti clockwise directions, the LEDs light up as per the above mentioned sequence.

Circuit design of Rotary Encoder with Arduino

Rotary Encoder consists of five pins: two coding pins A and B (or CLK and DT), one pin for switch and two power supply pins for Vcc and GND.
The two coding pins and the switch pin provide digital signals. They are connected to pins 2, 3 and 4 of Arduino.
To display the objects of the menu, a 20X4 LCD display is used. The connections are similar to that of 16X2 LCD. RS and E of LCD are connected to pins 5 and 6 of Arduino.
The data pins i.e. D4 – D7 (pins 11 – 14) of LCD are connected to pins 7 – 10 of Arduino.

Working

Rotary Encoders provide infinite rotation in either direction with a select button (pushing the knob). Such unique functionality can be used in “menu” selection applications, where the rotation of shaft in either direction will allow us to browse the contents of the menu and push of the button will allow us to select the highlighted menu content.

In this project, the working of rotary encoder as a menu selector is explained with the help of Arduino and LCD. The working of the circuit is as follows.
The code for Arduino is written as per the application and uploaded to Arduino. On power up, the LCD displays the list of values horizontally.
By rotating the shaft of the rotary encoder in clockwise direction, the menus items will change from left to right with each item being highlighted for every turn of the knob.
Once the last item is reached, further rotation will not result in any action but the last item will be highlighted.
If the knob is rotated in anti-clockwise direction, the menus items will be highlighted in reverse direction. The highlighted item will be displayed below the list of items.
If we want to select any highlighted item, the button of the rotary encoder is pushed. This is shown as the selected item and the selected item stays that way until the next item is selected.

Code
#include <LiquidCrystal.h>
#include <RotaryEncoder.h>
LiquidCrystal lcd( 8 , 9 , 4 , 5 , 6 , 7);
RotaryEncoder encoder(10, 11);
int value = 0;
byte full[8] = {0b11111, 0b11111, 0b11111, 0b11111,
0b11111, 0b11111, 0b11111, 0b11111
};
byte one[8] = {0b11011, 0b10011, 0b11011, 0b11011,
0b11011, 0b11011, 0b10001, 0b11111
};
byte two[8] = {0b10001, 0b01110, 0b11110, 0b11101,
0b11011, 0b10111, 0b00000, 0b11111
};
byte three[8] = {0b00000, 0b11101, 0b11011, 0b11101,
0b11110, 0b01110, 0b10001, 0b11111
};
byte four[8] = {0b11101, 0b11001, 0b10101, 0b01101,
0b00000, 0b11101, 0b11101, 0b11111
};
byte five[8] = {0b00000, 0b01111, 0b00001, 0b11110,
0b11110, 0b01110, 0b10001, 0b11111
};
byte six[8] = {0b11001, 0b10111, 0b01111, 0b00001,
0b01110, 0b01110, 0b10001, 0b11111
};
static int pos = 1;
int newPos = 0;
int selected = 0;
void setup()
{
pinMode(12, INPUT_PULLUP);
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
lcd.begin(20, 4);
lcd.createChar(0, full);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
lcd.createChar(6, six);
lcd.setCursor(0, 0);
lcd.print(" 1 2 3 4 5 6 ");
lcd.setCursor(1, 2);
lcd.print("Present Value: ");
lcd.setCursor(1, 3);
lcd.print("Selected: -");
}
void loop()
{
value = digitalRead(12);
if (value == LOW)
{
lcd.setCursor(13, 3);
selected = newPos;
lcd.print(selected);
}
encoder.tick();
newPos = encoder.getPosition();
if (pos != newPos)
{
if (newPos > 6)
{
encoder.setPosition(6);
newPos = 6;
}
if (newPos < 1)
{
encoder.setPosition(1);
newPos = 1;
}
highlightedSelection(newPos);
pos = newPos;
}
}
void highlightedSelection(int count)
{
int present = (count * 3) - 1;
if (count > pos)
{
lcd.setCursor(present - 4, 0);
lcd.print(" ");
lcd.print(count - 1);
lcd.print(" ");
}
if (count < pos)
{
lcd.setCursor(present + 2, 0);
lcd.print(" ");
lcd.print(count + 1);
lcd.print(" ");
}
lcd.setCursor(present - 1, 0);
lcd.write((uint8_t)0);
lcd.write((uint8_t)0);
lcd.write((uint8_t)0);
lcd.setCursor(present, 0);
lcd.write((uint8_t)count);
lcd.setCursor(15, 2);
lcd.print(count);
}

0 comments:

Post a Comment