Rotary Encoder Working With Arduino Uno

A rotary encoder (M274) is a type of position sensor which is used for determining the angular position of a rotating shaft. It generates an electrical signal, either analog or digital, according to the rotational movement.
ROTARY ENCODER PIN DETAILS

PIN Description
1. GND Ground or Arduino Board
2. +Vcc 5V DC power Supply
3. SW Output og Internal
4. DT Contact A Output or Data
5. CLK Contact B output or Data

There are many different types of rotary encoders which are classified by either Output Signal or Sensing Technology.
WORKING DETAILS:
Here A and B are outputs and these contacts are left sliding internally on a rotating disk. This disk has divided in to conductive (dark segment) and nonconductive segments (white segments) as shown in figure. The conductive contacts or dark segments are jointly connected to +5V. So when the points come in contact with dark segments they are simply coming in contact with +5V.
Now when the disk rotates clock wise, the point A comes in contact with conductive segment (dark part) first and provides an output +5V. After that point B comes in contact with conductive segment and provides +5V. So in the circuit D1 LED turns ON first following D2 LED. Hence when the axis of encoder is rotated clock wise point A provides +5V first and then B provides +5V.

ALL SENSOR DETAILS:
1. HC-05 – Bluetooth
2. Mpu 6050 accelerometer-gyroscope- -module
3. Flame-sensor
4. Rotary-encoder
5. Force-sensor-fsr400
6. Current-sensor-acs712
7. Flex-sensor
8. IR-sensor-tcrt5000
9. Pulse-sensor-pin
10. Color-sensor-tcs230
11. SD-card-module
12. Oled-with-arduino
13. Addressable-rgb-led-strip-ws1812
14. Relay-module
15. TFT-1-8-display-st7735r
16. 8×8-dot-matrix-display-max7219
17. Smoke-sensor-mq-2
18. Ultrasonic-sensor-HC-sr04
19. PIR-motion-sensor-hc-sr501
20. Tilt-sensor-sw-520d
21. Microphone-sound-sensor
22. Reed-switch-reed-sensor
23. Rfid-reader-Em18
24. Rfid-tag
25. RTC-real-time-clock-ds1307
26. Temperature-sensor-ds18b20
27. Moisture-sensor
28. Rain-sensor
29. Pressure-sensor
30. Accelerometer
31. MOC-3021-circuit
32. DHT11-DHT22-temperature-and-humidity-sensor
If the axis keeps rotating the point A leaves the conductive segment (dark part) and moves on to nonconductive part (white part). So the output A goes LOW. Next the point B leaves conductive part making output B LOW. With that D1 turns OFF first next D2 turns OFF.
If the axis keeps rotating the cycle repeats and output A goes high following output B. So we will have a pulse output every time the points move from one segment to other.
As these pulses state the change of axis, we can take them as a measure for axial movement. If this logic signal is given to controller, the controller can measure the clockwise axial movement. Similarly if the disk moves anticlockwise, point B comes in contact with conductive segment first providing +5V at output B. With disk keep moving point A comes in contact with dark segment. With that output A gives +5V. So in the circuit D2 LED turns ON first after that D1 LED turns ON. Turning OFF also follows the same pattern to complete a cycle. For every cycle there will be voltage pulse at the output.
As before these voltage signals represent the axial movement. We can take them as a measure for axial movement. If this logic signal is given to controller, the controller can measure the anticlockwise axial movement.
ROTARY ENCODER FEATURES:
1. 360º free rotation.
2. 20 steps or cycles per revolution
3. Incremental type encoder
4. Can work on low voltages
5. Maximum operating temperature: 0°C to + 80°C
6. Easy interface
7. Long life.
APPLICATIONS:
1. Security systems.
2. Motors
3. Robotic arms
4. Vending machines.
5. Industrial machines.
6. Engineering systems.
7. Measuring instruments.
8. Hobby projects.
9. Systems where systematic change INPUT device is needed like CROs, signal generator etc.
ROTARY ENCODER WITH ARDUINO CIRCUIT DIAGRAM:
ROTARY ENCODER WITH ARDUINO CODE:
Copy and paste the code or download via given link:
//Circuito.io is an automatic generator of schematics and code.
//Copyright (C) 2016 Roboplan Technologies Ltd.
// Include Libraries
#include “Arduino.h”
#include “LiquidCrystal.h”
#include “Encoder.h”
#include “Button.h”
#include “Servo.h”
// Pin Definitions
#define LCD_PIN_RS 9
#define LCD_PIN_E 8
#define LCD_PIN_DB4 4
#define LCD_PIN_DB5 5
#define LCD_PIN_DB6 6
#define LCD_PIN_DB7 7
#define ROTARYENCI_PIN_D 3
#define ROTARYENCI_PIN_CLK 2
#define ROTARYENCI_PIN_S1 10
#define SERVOMD_PIN_SIG 11
// Global variables and defines
long rotaryEncIOldPosition = 0;
const int servoMDRestPosition = 20; //Starting position
const int servoMDTargetPosition = 150; //Position when event is detected
// object initialization
Encoder rotaryEncI(ROTARYENCI_PIN_D,ROTARYENCI_PIN_CLK);
Button rotaryEncIButton(ROTARYENCI_PIN_S1);
Servo servoMD;
LiquidCrystal lcd(LCD_PIN_RS,LCD_PIN_E,LCD_PIN_DB4,LCD_PIN_DB5,LCD_PIN_DB6,LCD_PIN_DB7);
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println(“start”);
// set up the LCD’s number of columns and rows
lcd.begin(16, 2);
rotaryEncIButton.init();
pinMode(ROTARYENCI_PIN_S1, INPUT_PULLUP);
servoMD.attach(SERVOMD_PIN_SIG);
servoMD.write(servoMDRestPosition);
delay(100);
servoMD.detach();
menuOption = menu();
}
void loop()
{
if(menuOption == ‘1’) {
// LCD 16×2 – Test Code
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print(“Circuito Rocks !”);
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}
else if(menuOption == ‘2’) {
// Rotary Encoder Module – Test Code
//Read encoder new position
long rotaryEncINewPosition = rotaryEncI.read();
bool rotaryEncIButtonVal = rotaryEncIButton.onPress();
if (rotaryEncINewPosition != rotaryEncIOldPosition || rotaryEncIButtonVal) {
rotaryEncIOldPosition = rotaryEncINewPosition;
Serial.print(F(“Pos: “));
Serial.print(rotaryEncINewPosition);
Serial.print(F(“\tButton: “));
Serial.println(rotaryEncIButtonVal);
}
}
else if(menuOption == ‘3’)
{
servoMD.attach(SERVOMD_PIN_SIG);
servoMD.write(servoMDTargetPosition);
delay(500);
servoMD.write(servoMDRestPosition);
delay(500);
servoMD.detach();
}
if (millis() – time0 > timeout)
{
menuOption = menu();
}
}
char menu()
{
Serial.println(F(“\nWhich component would you like to test?”));
Serial.println(F(“(1) LCD 16×2”));
Serial.println(F(“(2) Rotary Encoder Module”));
Serial.println(F(“(3) Standard Size – High Torque – Metal Gear Servo – MG995”));
Serial.println(F(“(menu) send anything else or press on board reset button\n”));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == ‘1’)
Serial.println(F(“Now Testing LCD 16×2”));
else if(c == ‘2’)
Serial.println(F(“Now Testing Rotary Encoder Module”));
else if(c == ‘3’)
Serial.println(F(“Now Testing Standard Size – High Torque – Metal Gear Servo – MG995”));
else
{
Serial.println(F(“illegal input!”));
return 0;
}
time0 = millis();
return c;
}
}
}
Download Full Arduino sketch here.Rotory Encoder code

Subramanian
Subramanian

Subramanian MK, currently serving as a workshop instructor at Sakthi Polytechnic College, Erode Tamil Nadu. With a career spanning 25 + years, Subramanian MK has dedicated himself to advancing knowledge in Electronics and Communication Engineering (ECE). His passion for exploring new technologies has led to the development of numerous projects, showcasing expertise in IoT and PCB design.

Articles: 508

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

×

Hi, How can I help you?

×