Reading rotary encoder (getting random values)

All technical discussions and projects around startKIT
Posts: 9
Joined: Thu Jun 25, 2015 12:14 pm

Reading rotary encoder (getting random values)

Postby rebb » Sat Apr 16, 2016 9:48 am

I'm trying to read rotary encoder with this simple code i found from Arduino forums:

Code: Select all

 n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
     } else {
   encoder0PinALast = n;
Of course with similar xc code. But just getting random values when reading the pins. I also tried debouncing i'm using for buttons (just simple timer), but still cannot get steady reading. It is connected straight to 1-bit pins and middle pin to ground.

Encoder is Piher CI-11 (2 bit in quadrature code).

Does anyone have an example how to properly read the encoder or give any tips how to interface it properly to startKit and reading algorithm.
User avatar
XCore Expert
Posts: 768
Joined: Thu Dec 10, 2009 9:20 pm

Postby Ross » Sat Apr 16, 2016 12:17 pm

Can you post the rest of your code - the digitalRead() function for example.
Respected Member
Posts: 271
Joined: Fri Mar 19, 2010 4:49 am

Postby bearcat » Wed Apr 20, 2016 4:36 am

In my use, a simple bit test didn't work. The switches are noisy. You need to use a state machine to detect valid transitions between the two channels (and reset on errors) to detect CW and CCW rotations. I did not need to de-bounce, but I did limit to 10mS reads. I don't have XC code for this routine, sorry (Microchip).

Who is online

Users browsing this forum: No registered users and 0 guests