A long time ago I bought a couple RGB rotary encoders
from Sparkfun because they were cheap and I was already spending a bunch on other stuff. I thought they would be neat for some interfaces since it includes a push button. The interrupt service routine (ISR) for pin changes on the rotary encoder (for terminals A and B, which will from this point be referred to as AB) can be found in this post
(Oleg covers this topic in good detail, so I will not be recapping much on pin change interrupts). We will also need to add an external interrupt for the button. Lastly we will configure the PWM for the RGB LED that is contained withing the encoder. (I also have three separate LEDs set up to demonstrate the the actions from the rotary encoder: forward, backward, and push button)
Oleg, in one of his earlier posts
, explains how to use a rotary encoder with a look up table. Basically the principle works by polling the current port and comparing to the previous reading. By comparing the previous states of AB to the current we can determine forward or reverse motion. For more detail see Oleg’s post.
You will need an RGB rotary encoder (or, as you can use three LEDs if you do not have an RGB rotary encoder), three LEDs/resistors to observe correct functionality, and an Arduino or an ATmega328 (or comparable) mcu.
This is the suggested R/C filter from the datasheet to help avoid chatter (bounce). There is also a timing diagram in there that describes a time period that would mask chatter. Although the R/C filter should help alleviate chatter the chatter timing mask they mention is only 3ms, so a little chatter should not really effect anything (depending on what your ISR will do).
A and B will be connected to the first two pins on whatever port we choose to use (if the first two pins aren’t chosen you will have to change the way that you read the port and compare your last values). Terminal C goes to ground.
This next diagram comes from the dimensional drawing:
In this picture note that pin 5 is a common anode and pin three is for the switch/button.
Left is from the dimensional drawing of the pins. Right is the actual dimensional drawing of the encoder. A resistor is needed between each LED and its corresponding output from the ATmega328. We also need a pull-down resistor on the switch. NOTE: The switch is very easily destroyed with heat. It will most likely break if you continue to heat it up and move it around when soldering. Figure out where you want it and leave it there.