PIXELFLOW :: an intergalactic discourse

Pixelflow

An Intergalactic Discourse

Arduino Rotary Encoder

Noisy!

When using mechanical buttons to trigger events on a micro-controller there can be some voltage spikes that can re-trigger whatever logic you have for that button.

The same goes for rotary encoders, which in case of this one at sparkfun is two ‘buttons’ 90’ out of phase from one another, thus allowing you to detect clockwise or counter clockwise movement.

We can tune it out

There are a few ways to filter out unwanted mechanical noise. Using capacitors to react to the sudden spikes, or in software. In software you can check the values in your main loop, but that can be troublesome if your loop includes a lot of logic.

The other two approaches involve interrupts. You can use a timer to poll the pins at a regular interval, or you can attach an interrupt to the change of each pin.

Lets do it!

Arduino cooking timer has a great example of polling based matching. I really liked his code but wanted to try to code my own since this is a learning experience for me.

By looking at the value of both pins when one of them changes, you can predict the next valid value. By ignoring any other values, you do a good job of filtering out any noisy values that happen during one turn of the knob.

Check out the source over at my git hub

Here’s a video of it in action!