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 and led pins int l1 = 13; int l2

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      ------>     Any Analog/Digital pin

Arduino Tinkering : Controlling multiple LED with multiple potentiometers

HELLO THERE!!!!            How are you guys? I hope everyone is fine and well always. For this week's post, I would like to share with you guys a simple tinkering on Arduino, LED, and potentiometers. The objective of the experiment is to light up different amount of LED's with different intensity by controlling two potentiometers. Before starting, of course, we need to know what items are needed. The items needed are: 1. Arduino UNO   2. 4 to 5 LEDs 3. 2 Potentiometers The software used in this experiment is: 1. Arduino IDE. Now that all the hardware and software are obtained, let's go to the schematics of the circuit. The circuit is quite simple actually. The long legs of the LED's were connected to PWM pins while the shorter legs were connected to ground. The potentiometers however have three legs. The middle leg is connected to the analog pins (A0, A1) while the other two legs were connected to 5V and GND respectively. Don't w