Rotary Encode Module
DESCRIPTION:
Rotary encoder is a rotary input device (as in knob) that provides an indication of how
much the knob has been rotated AND what direction it is rotating [Link]’s a great device
for stepper and servo motor control. You could also use it to control devices like digital
potentiometers.
Specification:
● Operation voltage: 5V
● 5Pinout
● Size:28.35*18.55*26.19mm
● Weight: 6.301g
1/4
PIN CONFIGURATION:
1、 “CLK”: Encoder A
2、 “DT” : Encoder B
3、 “SW”:Switch button
4、 “+” : Power(+5V DC)
5、 “Gnd”: Ground
Example:
This is a simple sketch that shows how to count the encoder position and how to
determine direction of rotation. It has no switch debounce, nor does it use interrupts.
A fully developed application might need to incorporate these in order to make it
robust.
Code:
int pinA = 3; // Connected to CLK
int pinB = 4; // Connected to DT
2/4
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;
void setup()
{
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
/* Read Pin A
Whatever state it's in will reflect the last position
*/
pinALast = digitalRead(pinA);
[Link] (9600);
}
void loop() {
aVal = digitalRead(pinA);
if (aVal != pinALast)
{ // Means the knob is rotating
if (digitalRead(pinB) != aVal)
{ // Means pin A Changed first - We're
Rotating Clockwise
encoderPosCount ++;
bCW = true;
}
else { // Otherwise B changed first and we're
moving CCW bCW = false;
encoderPosCount--;
}
3/4
[Link] ("Rotated: ");
if (bCW)
{
[Link] ("clockwise");
}else
{
[Link]("counterclockwise");
}
[Link]("Encoder Position: ");
[Link](encoderPosCount);
}
pinALast = aVal;
}
Result:
4/4