How to use a Rotary Encoder with an Arduino - CODE EXPLAINED!

preview_player
Показать описание
In this video we will have a look at rotary encoders and in particular go through a robust and reliable code for the Arduino.

The code in the video can be downloaded here:

Make sure that the pins used to connect the rotary encoder to support interrupts. See:
Рекомендации по теме
Комментарии
Автор

I came here just to figure out how to hook up a Rotary Encoder, but I got so much more! Thank you for this excellent tutorial!

colr
Автор

Whoever worked out the coding for increasing and decreasing is a genius

mrdrgonzo
Автор

I thought I had a pot, but instead I had a rotary encoder! You have unblocked my project with this helpful video. :) Thanks

BenjaminParisi-ob
Автор

I know this is a little late in the game, but I thought this may help someone. I loaded your code onto an ESP32 DevKit1C. Used pin 14 as A and 15 as B. Worked well except that: (1) the first turn of the knob did not register (counter did not change) and (2) after any change of direction, the first turn did not register. The code I downloaded from github has a few "static" declarations under the "read_encoder" function. The first is: "static uint8_t old_AB = 3; // Lookup table index 3". I changed "uint8_t old_AB = 3" to "uint8_t old_AB = 4" and the problem on my ESP32 is now solved. To the author. Thank you so much for a great piece of code. Brilliant!

thomawhite
Автор

This is still by far the best code for handling rotary encoders that are a little noisy. This code works flawlessly every time I use it. Thanks for sharing and posting this video.

sophtware
Автор

thank you sir, Your video is the clearest one to explain the encoder state machine(the lookup table),I modified the code a little bit, When a tick check failed because of a large number of noise.Assume the enc value has been added to 2. If next turn direction was CCW, the enc value would be changed like 2-->1-->0-->-1, The counter value won't change even enc value changed 4 times, Your code clears the enc value to zero only when it>3 or <-3, So the tick check will fail when enc value is not 0 due to last failed tick check, So I add a clear operation by adding a out time variable if there's no turnning action,After that the code works very nice, thanks again!

jesenzeng
Автор

Finally! Finally I found someone who clearly explain it! Thanks so much!

valked
Автор

Thanks for this video. I've been concentrating on becoming more fluent with encoders and steppers lately, and you've explained somethings that I haven't run across before. This is a very clear, concise and informational video. You have gained a subscriber 🙂

trailranger
Автор

Hi Mo...that code is very responsive and reliable. I may need to adjust the multiplier value for the fast rotation, but that should be straightforward. I plan on using this as part of a precision AC current Trip. I'm using a Current Transformer to sense the current, which drives a Precision Rectifier and a comparator. I'm using an MCP4725 12-Bit DAC to set the reference for the Comparator. The falling-edge of the comparator output, triggers a D-FF which drives a DPDT relay to isolate the supply being monitored. Keep up the good work and thank you.

brendanbarbour
Автор

I have a bunch of different encoders I am using to build a flight sim avionics panel. I was using some button box code I found on the internet, and the encoders were not behaving as expected. Specifically some of them only triggered every second click. Testing the encoders as you detail here, I found that some of them don't behave as the one in your example. A and B both alternate low, high, low... for subsequents rest positions. So using your LED test, at the rest positions, the lights go on, off, on... not just off. So I have a mixture of encoder types, X2 and X1 or X4, not sure which. Now I need to figure out how to adapt this code to cater for the different types. Be useful if these types were mentioned in this video. Great video, I am new to this stuff and your video was the most comprehensive explanation I have found for a newbie like me.

tedwaetford
Автор

Excellent video Sir, very well explained, a real pleasure to watch. I will use your code for a stepper motor driven indexing head. However there is a tiny flaw: If you turn the knob really fast, you may end in a state where encval is +2 / -2 in the rest position and you need two clicks backward/forward to decrease/increase the counter by one. Just set old_AB = 3 after abs(encval) >= 4 to get bullet proof code 🙂

stefanwinkler
Автор

It works flawlessly. I tried different codes but they didn't work properly. Your code works perfect. Thanks :)

requiemadreamc
Автор

This is the only code that worked perfectly. All others I found on the internet for me only worked 80% correctly...

PostalPete
Автор

Very helpful, I nearly gave up on my encoder but this works remarkably well. I need an position on a positional axis and previous designs struggled with all kinds of misreading's.
Thank you for a very good explanation aswell.

ottosteehouder
Автор

Great! You explained everything in detail. Thanks so much

clarkso
Автор

Thanks for sharing the information! ❤
Really helpful for my project I’m working on at the moment

SamuelKreyenbuehl
Автор

Great video, and very clear instructions. 😊

airdedman
Автор

I dunno what to do! Im just using a rotary with the atmega328p chip, and then I make the pull up resistor circuit + filters but the values still bounce any time.

MarioPToledo
Автор

Just wondering, maybe it's clearly stated and I'm just not getting it right now: What happens, if the user would change the direction mid-click? As in after 2 Statechanges he returns back to the starting point instead of completing the click? Thank you for the nice work, it helps a lot!

FGRFPV
Автор

Interesting, I have a large rotary encoder on small PCB with 5 pins and with the LED test it behaves exactly as the one in this video. However, one of the mouse scroll wheel rotary encoders (Huano S-10 from Amazon) LED 1 lights up, then LED 2 (both are now lit), and then they both go out at the same time. In the other direction it's like: both are lit, LED 2 goes out, LED 1 goes out. Weird. I wonder if I can modify this code to work with this particular rotary encoder, as I am trying to save space in my project and not use one of the larger encoders.

PostalPete