Go Down

Topic: Help Rotary encoder (Read 841 times) previous topic - next topic

noxasdj

Hey guys,

I bought a rotary encoder from sparkfun (http://www.sparkfun.com/products/9117). I have never worked with rotary, first I thought that it was like a potentiometer, but it doesn't. So my question is: Is it possible to know the direction of the encoder?
I want to built a weather vane using this encoder as a way to know the direction of the wind. (N,S,E,W).

I've this piece of code from http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino, but I don't understand almost anything, even reading the website which is explained. Maybe it is not the best way to start.

Thank you.

Code: [Select]
/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC

void setup()
{
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  Serial.begin (115200);
  Serial.println("Start");
}

void loop()
{
static uint8_t counter = 0;      //this variable will be changed by encoder input
int8_t tmpdata;
/**/
  tmpdata = read_encoder();
  if( tmpdata ) {
    Serial.print("Counter value: ");
    Serial.println(counter, DEC);
    counter += tmpdata;
  }
}

/* returns change in encoder state (-1,0,1) */
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;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);
}



PaulS

Quote
Is it possible to know the direction of the encoder?

From your link:
Quote
The unit outputs gray code so that you can tell how much and in which direction the encoder has been turned.

Sure looks like it.

Does that code work? Do you see counter going up when you turn the encoder one way, and down when you turn it the other?

I'd be inclined to print counter AFTER adding tmpdata, but your mileage may vary.

noxasdj

Yes, in fact the code is working good, the encoder is counting from 0 to 255, if I turn the knob clockwise and it reverses if I go counterclockwise. First problem is that "almost" ,and I say almost 'cause is not always, every time it counts +4 steps or -4 depending the direction. Using this code I could know if the weather vane is turning clockwise or counter clockwise, but I can't get the actually position of the knob to determinate north, south, east, west.

PaulS

Quote
but I can't get the actually position of the knob to determinate north, south, east, west.

Why not? Put a knob with a pointer on it on the encoder. Rotate the encoder one complete turn. You should see the counter count up to some value for one complete turn. If the counter goes back to 0 before one complete rotation, change the type from int8_t to int16_t. You'll need to disable interrupts before updating counter, and enable them again afterwards, to that you don't get an interrupt in the middle of updating counter.

Riva

#4
May 24, 2012, 08:40 am Last Edit: May 24, 2012, 08:57 am by Riva Reason: 1
Hi noxasdj,

The encoder you describe is not really suitable as it's a 2 bit encoder (no pun intended) you can only count pulses when the shaft is turned and depending on A/B high/low sequence what direction it was turned. What you need is at least a 4 bit encoder, this would give you 16 distinct values so a certain shaft position would always return the same value.

This is more along the lines of what you looking for (see truth table in datasheet) but not really sturdy enough for a weather vane. Try looking for a metal equivilent
Code: [Select]
http://www.sparkfun.com/products/10064

majenko

You would probably be better off with something along the lines of a magnet mounted on the shaft and a ring of hall-effect sensors around it.  As the shaft rotates it triggers a different hall effect sensor.  The smaller and more numerous the sensors the more accurate the direction can be determined.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up