Go Down

Topic: Detecting how fast the encoder is turning! (Read 890 times) previous topic - next topic


Nov 26, 2012, 02:21 am Last Edit: Nov 26, 2012, 06:51 am by Coding Badly Reason: 1
Hi all, i have done this by far:
Code: [Select]
#define ENC_A 14
#define ENC_B 15

void setup()
 pinMode(ENC_A, INPUT);
 digitalWrite(ENC_A, HIGH);
 pinMode(ENC_B, INPUT);
 digitalWrite(ENC_B, HIGH);
 Serial.begin (115200);

void loop()
static uint8_t counter = 0;      
int8_t tmpdata;
 tmpdata = read_encoder();
 if( tmpdata ) {
   Serial.print("Counter value: ");
   Serial.println(counter, DEC);
   counter += tmpdata;

int8_t read_encoder()
 static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
 static uint8_t old_AB = 0;
 old_AB <<= 2;                  
 old_AB |= ( ENC_PORT & 0x03 );  
 return ( enc_states[( old_AB & 0x0f )]);

It works fine and everything but the next part to my project is to detect how fast my encoder is turning so my motor can react accordingly.
The arduino board im using is a Arduino UNO board and the encoder im using is this:

Any kind of help would be appreciated! :D

P.S. I'm kind of new here so pardon me for any mistakes i made in posting!

Moderator edit: [code] [/code] tags added.


P.S. I'm kind of new here so pardon me for any mistakes i made in posting!

To start with, only ask your question once.
I only provide help via the forum - please do not contact me for private consultancy.


Sorry about that. So do you have any idea on how to do?


Hey, I don't know nothing about what you are asking, but I can give a few tips to your post... Insert the code between CODE tags with the # button, and also comment the code so it would be easier to another person to understand what you are doing, and then is more probable that someone can help you.

Sorry for not answering your question :smiley-roll-blue:

Good luck!


Speed is change in distance over change in time.

With which one of those are you having trouble?
(the current time can be obtained with the millis() function)


Yes i know about the distance over time formula but i dont know how to infuse it with my code. May i see an example? Thank you for the reply!

Nick Gammon

Here's my thread about rotary encoders and interrupts:

Please post technical questions on the forum, not by personal message. Thanks!

More info:

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131