Rotary Encoder + Extras (Arduino)

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Lenigan96
 
Posts: 20
Joined: Wed Feb 01, 2017 11:36 pm

Rotary Encoder + Extras (Arduino)

Post by Lenigan96 »

I need some help regarding the use of this Rotary Encoder (https://www.adafruit.com/product/377). I want to use this with an Arduino project. I see this Rotary Encoder has five pins. I know the purpose of 3 of them (A,B and C). I would like to know the purpose of the other 2. I’m assuming that one of them is to connect the 5V pin of the Arduino and the other maybe controls the push-button.

User avatar
adafruit_support_bill
 
Posts: 88041
Joined: Sat Feb 07, 2009 10:11 am

Re: Rotary Encoder + Extras (Arduino)

Post by adafruit_support_bill »

The other 2 pins are for the switch. The encoder and switch are passive devices. No 5v power is required.

User avatar
Lenigan96
 
Posts: 20
Joined: Wed Feb 01, 2017 11:36 pm

Re: Rotary Encoder + Extras (Arduino)

Post by Lenigan96 »

I'm new to this so sorry if I make a dumb question haha. The "switch" is the push-button? Where do I connect the 2 remaining pins on my Arduino? Thanks for the help!

User avatar
adafruit_support_bill
 
Posts: 88041
Joined: Sat Feb 07, 2009 10:11 am

Re: Rotary Encoder + Extras (Arduino)

Post by adafruit_support_bill »

You would connect those two pins like any other SPST pushbutton switch: https://learn.adafruit.com/adafruit-ard ... duino-code

User avatar
Lenigan96
 
Posts: 20
Joined: Wed Feb 01, 2017 11:36 pm

Re: Rotary Encoder + Extras (Arduino)

Post by Lenigan96 »

Thanks! Now I'm having trouble programming the rotary encoder to work with mi Arduino UNO. I’m using this code as reference

Code: Select all

*     Arduino Rotary Encoder Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
 
 #define outputA 6
 #define outputB 5
 int counter = 0; 
 int aState;
 int aLastState;  
 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA);   
 } 
 void loop() { 
   aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 }

User avatar
adafruit_support_bill
 
Posts: 88041
Joined: Sat Feb 07, 2009 10:11 am

Re: Rotary Encoder + Extras (Arduino)

Post by adafruit_support_bill »

I've never seen it done that way. But that code might work if you turn the knob very slowly. It has several things working against it:
1) it is polling the pins.
2) it has two Serial.print statements in the polling loop.
3) At best it is giving you half of the resolution the encoder is capable of.

At 9600 baud, the serial prints will take about 15 milliseconds to execute. That means your poll rate is only about about 66Hz, so you will probably be missing some pulses.
Speeding up your baud-rate will help some.

But the best way to handle rotary encoders is with interrupts. The PJRC Encoder Library is an excellent implementation of interrupt driven encoder reading.
https://www.pjrc.com/teensy/td_libs_Encoder.html

User avatar
Lenigan96
 
Posts: 20
Joined: Wed Feb 01, 2017 11:36 pm

Re: Rotary Encoder + Extras (Arduino)

Post by Lenigan96 »

Thanks a lot for the help! I’ll try to make my own code and see what happens haha

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”