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.
Parts
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.
I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.ACCEPTPrivacy Policy
Manage consent
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.