A rotary encoder that is “NEVER” wrong!
No debounce code needed!
All my content is free.
If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.
Select amount then click the “Donate” button.
I’ve made many tutorials using the very popular KY-040 rotary encoder, it works, it’s cheap, easy to find, but like many others rotary encoders it bounces a ‘lot’ and requires quite a bit of programming to make it accurate.
I made a tutorial on the best way I’ve found so far to use the KY-040 in a tutorial HERE.
Today we will look at the ‘Bourns EAW’ absolute contacting encoder, which uses a mechanical way instead of a purely digital one like the KY-040.
The ‘Bourns EAW’ as many advantages compared to the KY-040:
- It has 128 rotary positions
- Uses a mechanical way to know where it is, so no debounce code is needed
- It’s fairly cheap compared to other encoders like it (‘$7)
How does it work?
Inside the Bourns encoders are 8 contactor tracks (which are connected to the 8 external pins on the encoder), and depending on where the shaft of the encoder is, those tracks convert to a specific binary code (00010001).
So for every 128 positions a different binary code can be obtained to know ‘exactly’ where the encoder is.
Here’s an sample of some of the positions and the corresponding binary results:
So no matter where the shaft is rotated to, we can know for certain where it is just by reading the 8 pins and matching that result to the binary map above.
Since it uses a mechanical way to know where it is, this type of encoder will always return the same results, even when the power is removed from the Arduino.
One downside of using this encoder might be that it requires 8 Pins to connect to an Arduino, compared to only 2 Pins when using the KY-040.
** But we will see in a future tutorial how to reduce the number of pins needed… Stay tuned!
In this tutorial were using the ‘Bourns EAW’ encoder to control a Stepper Motor.
Were also using an I2C LCD to display the encoder values and number of steps taken by the Stepper Motor. The 16×2 character LCD is connected to the popular I2C LCD Backpack that can be found on Amazon.
5v and Ground from the Arduino and A4 – A5 to SDA and SCL of the backpack
Pin 10 and 11 of Arduino to DIR and STEP pin of the driver
Ground of Arduino to Ground of driver
The EasyDriver is connected to a 12V power supply
Bourns EAW connections:
Pin 2 to 9 of Arduino connected to pin 1 to 8 of the encoder
Ground of Arduino connected to a Common pin of the encoder
We are using a library created for the Bourns EAW encoder.
The library has many options to get values from the encoder, the most useful being:
- Raw: this is the actual position of the encoder from 0 to 127, this will always be the same even if power is lost and restored.
- Upost: same as Raw but starts at logical zero to 127, so everytime the Arduino is powered the value will be zero and is based on the Raw value.
- Mpost: this is a multiturn value, that will start from zero when the Arduino is powered and can go from -32768 to 32768, this is the one we are using in this tutorial.
For the price, this encoder is a really good option for many projects that requires accurate readings.
In this tutorial we didnt’ use the capability of the encoder to ‘remember’ it’s position after a power failure, but this could be a great option for some projects.
The KY-040 still has a place for systems that don’t require absolute accuracy, like for menu selection, but the Bourns EAW sure has a place for other circuits.
As many electronic components, you might not have a use for such an encoder right now, but it’s always good to know it exists and might be the solution for a future project!
Thank you for stopping by!
Copy and Paste the above code/sketch in your Arduino IDE software.
Link to the libraries used in this tutorial:
- I2C LCD library by Francisco Malpartida: https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
- AccelStepper library by Mike McCauley : https://www.arduinolibraries.info/libraries/accel-stepper
- Bourns EAW Encoder library: https://github.com/arielnh56/ACE128