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

  Рет қаралды 32,078

MoThunderz

MoThunderz

Күн бұрын

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:
github.com/mo-thunderz/Rotary...
Make sure that the pins used to connect the rotary encoder to support interrupts. See:
www.arduino.cc/reference/de/l...

Пікірлер: 90
@col000r
@col000r 3 ай бұрын
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!
@sophtware
@sophtware 2 ай бұрын
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.
@trailranger6602
@trailranger6602 Жыл бұрын
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 🙂
@mothunderz
@mothunderz Жыл бұрын
Great to hear! I am glad the video was insightful 👌
@mrdrgonzo
@mrdrgonzo Жыл бұрын
Whoever worked out the coding for increasing and decreasing is a genius
@mothunderz
@mothunderz Жыл бұрын
Yes, look-up tables are awesome 🙂
@thomawhite
@thomawhite Жыл бұрын
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!
@mothunderz
@mothunderz Жыл бұрын
Thank you very much for your comment to the video! Have you tried to characterize the rotary encoder as I show in 4:26 of the video? My suspicion is that your rotary coder works exactly the other way around. In that case, the LEDs would be lit up in the resting position and switch off when you turn the encoder. Can you try to compile the code with "static uint8_t old_AB = 0;" -> does it work for your then as well?
@jesenzeng2335
@jesenzeng2335 7 ай бұрын
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
@mothunderz
@mothunderz 6 ай бұрын
Thank you for your feedback and suggestion. A time-out makes a lot of sense.
@clarkso65
@clarkso65 Жыл бұрын
Great! You explained everything in detail. Thanks so much
@mothunderz
@mothunderz Жыл бұрын
You're welcome, glad the vid was insightful for you!
@SamuelKreyenbuehl
@SamuelKreyenbuehl Жыл бұрын
Thanks for sharing the information! ❤ Really helpful for my project I’m working on at the moment
@mothunderz
@mothunderz Жыл бұрын
Wonderful! Wish you all the best for your project!
@user-jt2td3ui5v
@user-jt2td3ui5v 4 ай бұрын
I thought I had a pot, but instead I had a rotary encoder! You have unblocked my project with this helpful video. :) Thanks
@brendanbarbour8568
@brendanbarbour8568 8 ай бұрын
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.
@mothunderz
@mothunderz 8 ай бұрын
Sounds like a very interesting project. Thank you for sharing!
@stefanwinkler799
@stefanwinkler799 8 ай бұрын
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 🙂
@mothunderz
@mothunderz 6 ай бұрын
Thank you very much for your feedback. I had made an update in the final code download that also addresses this issue. Did you still experience the issue in this final version: github.com/mo-thunderz/RotaryEncoder Thank you for the feedback! (and sorry for my late reply, am swamped with work ATM)
@ottosteehouder5921
@ottosteehouder5921 7 ай бұрын
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.
@mothunderz
@mothunderz 6 ай бұрын
Thank you very much for your feedback!
@requiem4adreamc
@requiem4adreamc Жыл бұрын
It works flawlessly. I tried different codes but they didn't work properly. Your code works perfect. Thanks :)
@requiem4adreamc
@requiem4adreamc Жыл бұрын
I wrote down this comment before watching full video. But after watching full video i am more impressed. You did it professionally. Other videos which i watched were amateurish. Your code logis is way better. It is time to modify this code and hopefully make it work like a volume up/down thing :)
@mothunderz
@mothunderz Жыл бұрын
Thank you very much for your feedback! Yes, you can use this as well for volume control. Good luck with your project!
@requiem4adreamc
@requiem4adreamc Жыл бұрын
@@mothunderz Thanks :)
@airdedman3221
@airdedman3221 7 ай бұрын
Great video, and very clear instructions. 😊
@mothunderz
@mothunderz 6 ай бұрын
Thank you very much!
@Paul122M
@Paul122M Жыл бұрын
Excellent video as always!
@mothunderz
@mothunderz Жыл бұрын
Thank you! Cheers!
@tedwaetford
@tedwaetford 11 ай бұрын
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.
@mothunderz
@mothunderz 11 ай бұрын
Thank you for the feedback. When doing the LED test, turn the encoder very slowly to make sure you get it right. The creation of the look-up table would be a bit to long to add to this video, it can be tricky. Hope you get it to work!
@aarriikknn33ll
@aarriikknn33ll Жыл бұрын
Very inormative video! Thanks, you didnt miss a thing
@mothunderz
@mothunderz Жыл бұрын
Thank you very much!
@Csmitty-uw3ke
@Csmitty-uw3ke 3 ай бұрын
Any suggestions for how to convert the values this code puts out into degrees?
@PostalPete
@PostalPete 3 ай бұрын
This is the only code that worked perfectly. All others I found on the internet for me only worked 80% correctly...
@stephanc7192
@stephanc7192 Күн бұрын
Great explanation
@tcolliass
@tcolliass 3 ай бұрын
is there a way to use the rotary encoder without the interrupt pins? or can the inputs be polled instead?
@PostalPete
@PostalPete 2 ай бұрын
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.
@MarioPToledo
@MarioPToledo 4 күн бұрын
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.
@charley408
@charley408 Жыл бұрын
I've been trying to figure out what kind of switch my cycle selctor is on my GE front load washer. It doesn't look like a rotory switch. It's soldered to the board, mine had cold solder joints and separated from the board. The switch looks like one of these. It has 2 bigger pins holding it on, not sure if they do anything because I don't see they are in a circuit line. Theb there a 4 smaller pins off to the side. It has those clicks as you turn it.
@mothunderz
@mothunderz Жыл бұрын
Is it a rotary encoder? You can figure that out by checking if it turns infinity. Does it have a push button function? If it does the first step should be to figure out which pins are used for that function. You can do that with the short circuit detection function of a multi meter (the one that makes a sound when the two wires are connected). You find the right pins as it will make a sound when you pushed the rotary encoder. The same way you can figure out which pins are used for the rotary encoder -> by rotating very slowly. Then, like I do in 4:27 you connect a resistor and LED with 5V on one end to figure out how the encoder behaves. Good luck with your project!
@FGRFPV
@FGRFPV 2 ай бұрын
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!
@GergelyDrCsany
@GergelyDrCsany Жыл бұрын
Thank you so much for the video, the nice explanation and the code! :) This has solved my problem (having much of the mentioned noise in my signals) in one of my projects - I'm really grateful for that. :) Is there a way to do it without interrupts as well? - I'm planning to make another project in which I'd like to use 4 rotary encoders with an Arduino Pro Micro (having only 5 pins with interrupts support).
@mothunderz
@mothunderz Жыл бұрын
well, the main problem with implementing this code without interrupts will be that you will miss events and that will lead to laggy responses on the encoders. Why do you want to use an Arduino Pro Micro? I recommend using an ESP32. That has interrupts on all ports.
@larbigueraiche6721
@larbigueraiche6721 10 ай бұрын
Awesome video.
@mothunderz
@mothunderz 10 ай бұрын
Glad you enjoyed it
@Maan_Arora
@Maan_Arora 9 ай бұрын
Hey, i’m trying to make a steering wheel for racing games , can i do this and have it work in racing games?
@mothunderz
@mothunderz 8 ай бұрын
Yes, for that a rotary encoder should be very useful.
@GapRecordingsNamibia
@GapRecordingsNamibia Жыл бұрын
So, may I ask please, everyone does the rotary encoder and an Arduino uno. Could you please do a sketch using the encoder with isr and an Arduino nano EVERY the one that uses the ATMega 4809, I have not found one YT video using this MCU with an Encoder and for us normal none program savvy people this is really hard to try and figure out how it works from the 600 page MCU manual. Thank you.
@mothunderz
@mothunderz Жыл бұрын
Unfortunately I dont have a nano here to test it out with, but the next time I buy some equipment I will order a nano as well. However, why dont you use an ESP32? It is about the same price level, but way faster, has Ethernet and Bluetooth and all PINs support interrupts. That is why on the ESP32 you can use rotary encoders on any of the pins. If you really want to stick to the nano, why do you want to use isr? According to www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ the nano has interrupts on pins: 2, 3, 9, 10, 11, 13, A1, A5, A7 is that not sufficient?
@joelloyd2936
@joelloyd2936 Жыл бұрын
is there a way to do this with multiple encoders even though only pins 2 and 3 support interrupts?
@mothunderz
@mothunderz Жыл бұрын
If you want to use multiple rotary encoders you cannot use the same pins on the Arduino. You will have two use pins with interrupts per encoder. There are platforms such as the esp32 that support interrupts on all pins if you want to use many rotary encoders
@joelloyd2936
@joelloyd2936 Жыл бұрын
@@mothunderz thanks for the reply I'll look into that
@flotshii8036
@flotshii8036 Жыл бұрын
Idk if this is just with me, but if i turn the encoder in one direction, then in the other, it first increments and then starts decrementing the value. So it is like this: turn right: 1,2,3,4,5 turn left: 6,5,4,3,2,1
@mothunderz
@mothunderz Жыл бұрын
Sorry for my late reply, I briefly wanted to doublecheck the behavior myself. I just did with my Arduino if I turn the other direction it does not lag so turn right 1, 2, 3, 4, 5 then turn left 4, 3, 2, 1, 0. On 4:27 in the video I show a simple method to verify operation of the rotary encoder. Can you verify if your rotary encoder behaves the same way? Thank you
@flotshii8036
@flotshii8036 Жыл бұрын
@@mothunderz Thanks for your reply. Yes my Encoder does work the same way. I can figure out the reason my encoder setup behaves this way. i am using a pi pico insted of an arduino, but this shouldnt matter i think.... Did you notice anyone else having the same issue? Thanks
@mothunderz
@mothunderz Жыл бұрын
Do you have another Arduino to double check? I dont have a pico, but could find online some differences. Have you for instance changed Serial into Serial1? Have you checked with different ports? See for instance: forum.arduino.cc/t/working-rotary-encoder-for-arduino-doesn-t-work-for-pico/1045042/18 hope you can make it work!
@mariusjunkerkrabbe9225
@mariusjunkerkrabbe9225 Жыл бұрын
@@flotshii8036 I have the same issue with my arduino Mega, did you figure it out?
@mariusjunkerkrabbe9225
@mariusjunkerkrabbe9225 Жыл бұрын
Hmmm🤔 I did nothing but rewrite what I believe is the exact same code, and it suddently works.
@almirmulaosmanovic3401
@almirmulaosmanovic3401 Жыл бұрын
Can KY-040 work without VCC connected?
@mothunderz
@mothunderz Жыл бұрын
As discussed in the video only the GND, DT and CLK need to be connected for the rotary encoder. The + and SW are for the push buttons.
@almirmulaosmanovic3401
@almirmulaosmanovic3401 Жыл бұрын
@MoThunderz I watched the video, and it was most helpful. My mistake was asking before watching the video in it's entirety however after doing some math it appears that connecting the + would make the encoder more precise as it has internal 10k ohm resistors rather than using Arduino's pull up ressitors. Either that or put external pull-up resistors. Either way, your video helped me a lot. Thank you for taking the time to answer me
@zanthor6789
@zanthor6789 Жыл бұрын
@@almirmulaosmanovic3401 Sorry for missing your comment back to me. Yes, you can use external pull-ups, but for me the performance with the internal pull-ups was already excellent, therefore, I never had the need to add additional components.
@laenga6044
@laenga6044 10 ай бұрын
So amazing. It almost feels like applying AI to a rotary encoder, and it works so well. Big thanks, and Jesus loves you.
@mothunderz
@mothunderz 10 ай бұрын
Wow, thank you! You are very welcome
@egyrmexiq1
@egyrmexiq1 8 ай бұрын
Great video, very explicit, thanks for your efforts.. Subscribed.!!
@mothunderz
@mothunderz 6 ай бұрын
Thank you very much, well appreciated!
@DuyLe-yh8nf
@DuyLe-yh8nf 6 ай бұрын
that's excellent
@mothunderz
@mothunderz 6 ай бұрын
Thank you!!
@luiscastillejo9336
@luiscastillejo9336 Жыл бұрын
excelente gracias
@mothunderz
@mothunderz Жыл бұрын
de nada :-)
@numeprenume512
@numeprenume512 Жыл бұрын
mine prints only ???????????, no numbers. must be broken...
@joelloyd2936
@joelloyd2936 Жыл бұрын
make sure the baud rate in the serial monitor is the same as in the code
@mothunderz
@mothunderz Жыл бұрын
Thanks for chiming in Joe! Yes, for this code pls make sure the baudrate is 115200 in the serial monitor. You can change that on the bottom of the monitor window
@victorleonets179
@victorleonets179 9 ай бұрын
You save my time, thanks from Ukraine
@mothunderz
@mothunderz 8 ай бұрын
You are very welcome!
@MrEvildigga
@MrEvildigga Жыл бұрын
your video is a little bit to quiet i have trouble hearing you even at max volume with my laptop
@mothunderz
@mothunderz Жыл бұрын
Thank you for your feedback. Yes, you are right, it seems I did not get the audio level right on the master when uploading this video, my apologies for that. Unfortunately KZfaq does not let me change the audio for this particular video anymore. I will make sure this does not happen again and look into re-uploading this video with better audio.
@MrEvildigga
@MrEvildigga Жыл бұрын
@MoThunderz no worries m8 just crank it up next time 👍
@mothunderz
@mothunderz Жыл бұрын
@@MrEvildigga Thanks! Glad you could make it work 🙂
@dalerogers1134
@dalerogers1134 Жыл бұрын
Thank you for making this video! I appreciate the time you put into it and the professional graphics. Very well done. I am building an Omni wheel platform and my coding skill level certainly needs a boost. Eventually I will put together enough good examples to succeed with my project. I am doing this in support of my omnidirectional standing wheelchair project, to perfect the steering. That project has Roboclaw motor controllers that eliminate the need for the basic coding to support its encoders. I am a rookie and it took me a huge chunk of time to incorporate encoders. In the future I want to use affordable motor controllers and use the sketch to do the encoder translation. HTTPS://kzfaq.info/get/bejne/q8CIm9uFv6rVpaeI.html. I’d like not to be a rookie forever.
@mothunderz
@mothunderz Жыл бұрын
Thank you for your comment, I am glad my video helped you increase your understanding of the Arduino. I wish you all the best with your project!
@TheUnofficialMaker
@TheUnofficialMaker 11 ай бұрын
man your audio is low. Why do so many YT's spend so much on video and then have low volume? Hard to listen to
@mothunderz
@mothunderz 11 ай бұрын
My apologies - forgot to normalize the audio on this one. Unfortunately YT does not allow me to make any corrections, so will have to re-upload
@hagarelsayed7891
@hagarelsayed7891 4 ай бұрын
tarsh
@randomdamian
@randomdamian 5 ай бұрын
The mic is so bad, great informative video but the mic is extremly quiet... if I would play a song now it would blow out my hifi speakers. It's actually half the volume than other videos on youtube
@woodybaines1953
@woodybaines1953 Жыл бұрын
Love this guys videos !! #1 subs provider -> Promo'SM!!
@mothunderz
@mothunderz Жыл бұрын
Thank you very much for your support!
Using Rotary Encoders with Arduino
32:10
DroneBot Workshop
Рет қаралды 702 М.
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 14 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 123 МЛН
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 61 МЛН
Каха и суп
00:39
К-Media
Рет қаралды 6 МЛН
How to use rotary encoders
14:12
FriendlyWire
Рет қаралды 30 М.
Troubleshooting a Rotary Encoder for Arduino
16:35
MrCsfrancis
Рет қаралды 87
Understanding Arduino Interrupts | Hardware, Pin Change & Timer Interrupts
48:17
ROTARY ENCODER WITH INTERRUPTS - Arduino tutorial #12
13:12
Bas on Tech
Рет қаралды 55 М.
Adventures in Science: How to Use Rotary Encoders
12:05
SparkFun Electronics
Рет қаралды 222 М.
How to Use Arduino Interrupts The Easy Way
33:28
Rachel De Barros
Рет қаралды 78 М.
#226 ROTARY ENCODER with no switch bounce 🤓 - you MUST use this
25:49
Can the ARDUINO NANO handle 64 potentiometers?
26:55
Notes and Volts
Рет қаралды 14 М.
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 14 МЛН