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
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
Post a Comment