Rotary Encoder

Questions, answers, feedback or just general conversation about anything to do with the store 'Hacker's Warehouse'.

Rotary Encoder

Postby Tim Laren » Sat Mar 31, 2012 11:18 pm

The rotary encoder is a 2 bit gray code. There are two output pins and a common. This encoder also has a push button. Because of the form factor of the encoder I was not able to insert it directly into my solderless breadboard but instead use female jumpers on each pin and then plugged a each pin into my breadboard.

Looking at the side with three pins, number then 1 thru 3.
Pin 1 is encoder pin A.
Pin 2 is encoder common.
Pin 3 is encoder pin B.
The other two pins are the push button.
Pin 1 and Pin 2 require a 10K pull-down resistor to ground.

Here is a sample program.

Code: Select all
/* Read Rotary Encoder
    Connect Rotary Encoder Pins as follows:
    Pin 3 - Pin A (1)
    Pin 2 - Common (2)
    Pin 3 - Pin B (3)
*/ 

 int PinA = 3;
 int PinB = 4;
 int Pos = 0;
 int Last = LOW;

 void setup() {
   pinMode (PinA,INPUT);
   pinMode (PinB,INPUT);
   Serial.begin (9600);
 }

 void loop() {
   int v = digitalRead(PinA);
   if ((Last == LOW) && (v == HIGH)) {
     if (digitalRead(PinB) == LOW) {
       Pos--;
     } else {
       Pos++;
     }
     Serial.println (Pos);
   }
   Last = v;
 }


Upload this sketch and start the Serial Monitor. You should get a value every time the encoder changes position.
Have fun and let me know what you do with the Rotary Encoder.

encoder.zip
Rotary Encoder Sketch
(579 Bytes) Downloaded 526 times
Image
Tim Laren
Site Admin
 
Posts: 135
Joined: Thu Mar 04, 2010 8:25 pm

Return to Hacker's Warehouse Feedback

Who is online

Users browsing this forum: No registered users and 1 guest

cron