Skip to main content

Arduino Beginner Experiment: Arduino And Rotary Encoder

HELLO THERE!!!

    How are you guys? I hope everyone is having fun and enjoying their holidays. In this post, I would like to share with you guys on my tinkerings with the rotary encoder. In this experiment, I would read my rotary encoder values by using Arduino.


What Is A Rotary Encoder?

     A rotary encoder is a device that changes motion into digital or analog values. This device are mostly used in robotic projects as it has precise unlimited rotation motion.There are different types of rotary encoders tailored for different types of uses. In this experiment, I had use one of the common rotary encoders, Keyes 0-40 rotary encoder.


HARDWARE:

1. Arduino UNO
2.Rotary Encoder (KYS 0-40)
SOFTWARE:
1.Arduino IDE


CIRCUIT ASSEMBLY:

Referring to image above, the connection of the rotary encoder to Arduino is as follows:

Rotary                                 Arduino
---------                                ----------
GND        ---------------->    GND
+              ----------------->   5V
SW          ------------------> none
DT           ------------------> any digital pin
CLK         ------------------> any digital pin


CODING THE ARDUINO

The coding for the Arduino is shown below:
V1

int dt =4;//connect the dt pin to digital pin 4
int clk =5; //connect the clk to digital pin 5
int val;//variable to keep current val 
int lastval;//variable to keep the last value
int pos = 0;//position value


void setup() {
 pinMode( dt, INPUT); //declare bpth dt and clk as input
 pinMode(clk, INPUT);
 Serial.begin(9600);// serial communication at 9600bps

int lastval = digitalRead(clk); //whatever happens it will show the last position value 

}

void loop() {
 val =  digitalRead(clk);//current value of position

 if (val != lastval) //if current value is not same as the lastvalue(movement detected)
 {
  if (digitalRead(dt) != val)// if there is clockwise movement
  {
        pos++;//increase the position value
        
  } else {
    pos--; //if there is counter clockwise movement , decrease the position value
  }

  
  }
Serial.println(pos);//print out the value
lastval = val;

}


V2

int dt =4;//connect the dt pin to digital pin 4
int clk =5; //connect the clk to digital pin 5
int val;//variable to keep current val 
int lastval;//variable to keep the last value
int pos = 0;//position value


void setup() {
 pinMode( dt, INPUT); //declare bpth dt and clk as input
 pinMode(clk, INPUT);
 Serial.begin(9600);// serial communication at 9600bps

int lastval = digitalRead(clk); //whatever happens it will show the last position value 

}

void loop() {
 val =  digitalRead(clk);//current value of position

 if (val != lastval) //if current value is not same as the lastvalue(movement detected)
 {
  if (digitalRead(dt) == val)// if there is counter clockwise movement
  {
        pos--;//decrease the position value
        
  } else {
    pos++; //if there is clockwise movement , increase the position value
  }

  
  }
Serial.println(pos);//print out the value
lastval = val;

}



 There are just some minor difference it code V1 and V2 but both output from the codes will be the same. It's just that there are more than one way to write the code down.Below is a simple demo video of the experiment.



Well, that's all from me this week, folks. I hope to see you guys in the next post. Till then, I would like to say HAPPY TINKERING!!!!







Comments

Popular posts from this blog

Arduino and Multi Function Shield: Pots,LEDs and buzzers

HELLO THERE!!!!!    How are you guys? I hope everyone is fine and in good mood always. This week, I continue my tinkerings with the Multi Function Shield. In this experiment, I used the potentiometer in the shield to control the LEDs in the shield. Without further wasting time, let's jump in straight to the experiment!! HARDWARE 1.ARDUINO UNO  2.MULTI-FUNCTION SHIELD SOFTWARE: 1. ARDUINO IDE CIRCUIT ASSEMBLY    The circuit assembly is fairly simple. Just attach the multi function shield on top of Arduino properly. Please attach the shield properly as failure to do so would cause the shield  not to function as intended. CODING THE ARDUINO: Let's refer to the multi-function shield image below: Referring to the image of the shield, it can be seen that the potentiometer (blue object near buttons) is connected to pin A0 (analog 0)     int pot = A0; //declaring the pot ...

Arduino Beginner Experiments: Arduino and Light Blocking Sensor

HELLO THERE!!!     How are you guys? I hope everyone is fine and in a good mood always. This week, I would like to share with you guys my experiment on Arduino and Light Blocking Sensor. The objective of this experiment is to receive feedback from the sensor ( Digital and Analog) What is Light Blocking Sensor?      A linear hall sensor is a type of sensor which responds when the sensor is blocked out from light. The sensor is different for LDR's as LDR's responds to the amount of light it received while the light blocking sensor responds if the light is blocked out or not from the sensor.It can be used for both digital and analog measurements. HARDWARE: 1.Arduino UNO 2. Light Blocking Sensor SOFTWARE: 1.Arduino IDE CIRCUIT ASSEMBLY   Let's refer to the image of the blocking sensor below: The Connection of the Sensor to the Arduino is shown below: SENSOR  ----->       ARDUINO Signal   ...

Arduino and Keyes KY-017 Mercury Tilt Sensor

HELLO THERE!!!     How are you guys? I hope everyone is fine and good as always. I am back in tinkering after a few months of hiatus( busy stuff :) ). So, today i would like to share with you guys on KY-017, which is a Keyes Mercury Tilt Sensor. KY-017 Sensor KY-017      The picture above shows the KY-017 Sensor from Keyes. It has 3 pins, A ground, A vcc, and A signal pin. If you look closely, there is a mercury ball inside the bulb like object. The mercury ball acts like a switch for this sensor. KY-017 Working Procedure: 1. When the sensor is not tilted, the mercury ball will be at the bottom of the bulb. This will complete the circuit and turns on the LED of the sensor. The output that is sent through the signal pin will be LOW. 2.When the sensor is tilted, the mercury ball will be at the top of the bulb(near at the sharper end). This will complete break circuit and turns off the LED of the sensor. The output that is sent through t...