ROTARY ENCODER WITH INTERRUPTS - Arduino tutorial #12

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ค. 2024
  • In this Arduino for beginners tutorial I'll teach you how to read a Rotary Encoder with interrupts.
    The Arduino for beginners tutorials series guides you through the world of Arduino. A video is around 10 minutes and shows you step-by-step how to build the circuit and write the code. The website contains the course material, circuit diagram, Arduino code and shopping list of all the components used in the tutorial.
    ⬇️ Arduino code, circuit diagram and part list arduino-tutorials.net/tutoria...
    👉 More tutorials and projects on arduino-tutorials.net
    🇳🇱 Deze tutorial is ook in het Nederlands beschikbaar • 🇳🇱 ROTERENDE ENCODER E...
    CHAPTERS:
    00:00 Components needed
    00:16 Building the circuit
    01:51 The rotary encoder inner working
    04:35 Arduino code - part 1
    09:31 Arduino code - part 2
    12:07 The challenge
    12:47 Outro
    COMPONENTS
    🇨🇳 Arduino Uno (clone) with cable - AliExpress
    arduino-tutorials.net/r/Jd28hF
    🇳🇱 Arduino Uno (clone) - Opencircuit
    arduino-tutorials.net/r/8237Hd
    🇳🇱 Arduino Uno SMD (original) - Opencircuit
    arduino-tutorials.net/r/Ud29dh
    🇳🇱 Arduino Uno Chip (original) - Opencircuit
    arduino-tutorials.net/r/Jd82ha
    🇨🇳 1x breadboard small - AliExpress
    arduino-tutorials.net/r/7364GD
    🇳🇱 1x breadboard small - Opencircuit
    arduino-tutorials.net/r/238Jds
    🇨🇳 1x breadboard large - AliExpress
    arduino-tutorials.net/r/FDH28dh
    🇳🇱 1x breadboard large - Opencircuit
    arduino-tutorials.net/r/8237Dh
    🇨🇳 Jumper wire assortment (MM/MF/FF) - AliExpress
    arduino-tutorials.net/r/Fh378fg
    🇨🇳 Jumper wire male-male - AliExpress
    arduino-tutorials.net/r/7346GD
    🇳🇱 Jumper wire male-male - Opencircuit
    arduino-tutorials.net/r/8237Jd
    🇨🇳 Rotary encoder - AliExpress
    arduino-tutorials.net/r/hfFh8
    🇳🇱 Rotary encoder - Opencircuit
    arduino-tutorials.net/r/ykcvBW
    #arduino #BasOnTech

ความคิดเห็น • 91

  • @BasonTech
    @BasonTech  4 ปีที่แล้ว +2

    👉 Don't forget to check arduino-tutorials.net for more Arduino tutorials and projects.
    If you have questions, don't hesitate to ask them in de comments. Remember: There are no stupid questions 😃

  • @naboulsikhalid7763
    @naboulsikhalid7763 2 ปีที่แล้ว +3

    that explains all the things I wanted to know and settles down all the dust that cloud my understanding of rotary encoder. Great tutorial. thank you very much

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Thank you! 😃

  • @allanminers1662
    @allanminers1662 2 ปีที่แล้ว

    Best ever explanation of how a rotary encoder works.Two little dots fit it

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Thanks! 😃

  • @stevenirish6160
    @stevenirish6160 4 ปีที่แล้ว +2

    Thank you!

  • @Sparklift
    @Sparklift 4 ปีที่แล้ว +1

    Great video. Clear instructions. Please do more and more videos

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Thanks, will do! 😃

  • @soonfamous
    @soonfamous 5 วันที่ผ่านมา

    Great video Bas :) i bumped into your channel and I immediately got hooked on the quality of your explanation! I did follow the tutorial with ease. Unfortunately my rotary encoder is still giving me false values (even when I try to move it slowly). I am thinking to apply some filter on the raw values (avg of 3) perhaps? Any pro advice on how manufacturers usually solve problems like that?

  • @longdongsilver4719
    @longdongsilver4719 3 ปีที่แล้ว

    Thanks for your help. This video kept me surely from searching for the solution during hours. I was using the polling method to read my encoders and it was far from satisfying. I had lots of misreads and the reliablilty depended too much on the encoder quality. Interrupts was the way to go.
    BTW, your english pronunciation is perfect ;-)

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      Glad it helped and thanks! 😄

  • @mrcanisters
    @mrcanisters 2 ปีที่แล้ว

    thanks. just what was after. very well explained.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว +1

      Great to hear, thanks! 😃

  • @unalakdogan9416
    @unalakdogan9416 2 ปีที่แล้ว

    Perfect

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Thanks! 😃

  • @EricEllenbrook
    @EricEllenbrook 3 ปีที่แล้ว

    Theoretically could you assign a count to the encoder so when you turn to the left it decreases and to the right it increases? And could you display that value on the LCD screen?

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Hi Eric, that is not only theoretically but also practically possible 😃

    • @EricEllenbrook
      @EricEllenbrook 3 ปีที่แล้ว

      @@BasonTech im new to arduino and this video gave me the inspiration I needed to get started! I hope to share a video with you one day :)

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Hi Eric, if you're new to Arduino, I would really recommend to start with tutorial #1. You'll learn a lot of basics in the first few tutorials. Looking forward for your video! Use the hashtag #basontech 😃

  • @_twich_8096
    @_twich_8096 2 ปีที่แล้ว

    lets say i want to connect 2 encoder, i assume is possible to contec the pinA to arduino pin2 and 10 and the other encoder pinA to arduino pin3 and 11. i assume thinks will work the same way. of course by doublening the code, with deferent pins and names, will this work?

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Correct, however it is better to not repeat code but split it up in reusable functions 😃

  • @tonholper6282
    @tonholper6282 ปีที่แล้ว

    Hi Bas, wich Rotary encoder are u using, it has a pushfunction wich i need

    • @BasonTech
      @BasonTech  ปีที่แล้ว

      Hi, in the description of every video is a list with all the components. Here you find links to Dutch and international stores.

  • @MikeysLab
    @MikeysLab 3 ปีที่แล้ว +3

    You should handle the denouncing in hardware, not software. Adding a couple resistors, a cap and a 74hc14 will give you a nice clean signal.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      Thanks for sharing! 😃

    • @longdongsilver4719
      @longdongsilver4719 3 ปีที่แล้ว

      I suppose you are kidding...

    • @MikeysLab
      @MikeysLab 3 ปีที่แล้ว +1

      @@longdongsilver4719 not at all, bouncing is a hardware problem. Always solve the problem where the problem is. Pushing it off to software is just lazy engineering

    • @longdongsilver4719
      @longdongsilver4719 3 ปีที่แล้ว

      @@MikeysLab The right solution is the simplest one. Adding some debouncing circuitry is just over-engeneering when it comes to a simple rotary encoder. Adding active or even passive components increase the risk of failure and the cost.
      Lazyness is not a criteria in engeneering but efficiency, production cost and MTBF are the real way to think your design.

    • @MikeysLab
      @MikeysLab 3 ปีที่แล้ว

      @@longdongsilver4719 okay let's look at the "efficency" of your idea, on one hand we save about 7 cents per encoder. On the other we have to pay an embedded software engineer to write a complicated debounce function that will not use delays or hinder main program execution in anyway, or use a multi core micro controller and dedicate a thread to the ui alone, which as a embedded software engineer is going to take about 16 hours to design implement and effectively test before it should be released in a product. Factor in I charge $200/hr for dev time and your 7 cent savings just cost you a whole hell of a lot of money. The easiest solution is not always the right solution. This is what makes people use strings over char[] in Arduino and get into trouble with memory fragmentation

  • @jaimeriquelme6727
    @jaimeriquelme6727 3 ปีที่แล้ว

    Hi Bas on tech, how did you verify this program when ' digitalPinToInterrupt' was not declare in this scope, help me please to understand. Thanks for your time.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Hi Jaime, please make sure you have no typos in the naming of digitalPinToInterrupt and see if all the { and } are opening and closing at the correct place.

    • @jaimeriquelme6727
      @jaimeriquelme6727 3 ปีที่แล้ว

      @@BasonTech, I've already checked many times, but I'm not the only one with this problem, someone said reinstall the IDE program and try again but it didn't work.
      Any other suggestion, I'm using an old Dell laptop with Windows XP. Thanks a lot for your patience.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      @@jaimeriquelme6727 Did you copy my source code from the course material to make sure there aren't any typos?

  • @AnakRantauan_NL-Industry
    @AnakRantauan_NL-Industry 4 ปีที่แล้ว

    If led brightness control with two leds. How to code ?

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Hi Anak, I am not sure what you want to achieve. Do you want to control the led brightness? You can use PWM for that. It is explained in this tutorial th-cam.com/video/1fhRf-0RxyQ/w-d-xo.html

  • @muratcanbarut64
    @muratcanbarut64 4 ปีที่แล้ว

    süperb

  • @dreamseer2007
    @dreamseer2007 2 ปีที่แล้ว

    I loved your video, but when implementing the interrupt, I am now getting left on left and mostly left on right. Before implementing the interrupt each direction is mostly correct.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว +1

      Hi, make sure you have wires connected correctly. Sometimes the encoder is damaged and "slips" resulting in wrong output

    • @dreamseer2007
      @dreamseer2007 2 ปีที่แล้ว

      @@BasonTech Thank you! I changed the encoder and the new one works perfectly. 🙂

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว +1

      Great! A disadvantage of rotary encoders is that they wear over time and start slipping

  • @ipadize
    @ipadize ปีที่แล้ว

    i have first added the encoder without interrupts- which worked semi okay (its not reading fast enough), so i added the interrupt. Now when i turn the encoder my whole program will stop. I call a switch case with the interrupt and the switch case calls another switch case inside, maybe using an interrupt is forbidden with nested switch cases?

    • @BasonTech
      @BasonTech  ปีที่แล้ว

      I know that the code within the interrupt needs to be as small as possible. A switch adds logic so perhaps you are right and is it not advised to do so.

    • @ipadize
      @ipadize ปีที่แล้ว

      @@BasonTech i have changed my code to only use buttons - short press once and long press continuously change a value (i still have to add that long press tho).

  • @djproducerty88
    @djproducerty88 5 ปีที่แล้ว

    Kan je een video maaken over RBG led

    • @BasonTechNL
      @BasonTechNL 5 ปีที่แล้ว

      Bedoel je RGB led met vier pootjes of de WS2812B adresseerbare LED strips?

    • @djproducerty88
      @djproducerty88 5 ปีที่แล้ว

      Met vier potjes

    • @BasonTechNL
      @BasonTechNL 5 ปีที่แล้ว

      @@djproducerty88 Die werkt precies hetzelfde als een gewone LED :) Een RGB led is eigenlijk een 3 in 1 LED. Je kunt hem dus aansluiten als een gewone LED. Afhankelijk van welk pootje je kiest wordt hij rood, groen of blauw zie sites.google.com/site/ardunitydoc/_/rsrc/1453009974510/getting-started/run-examples/rgb-led/RGB_LEDCircuit%28Analog%29.png

    • @djproducerty88
      @djproducerty88 5 ปีที่แล้ว

      @@BasonTechNL maar je moet tog resistors gebruiken?

    • @BasonTechNL
      @BasonTechNL 5 ปีที่แล้ว

      @@djproducerty88 Klopt, dat kan op precies dezelfde manier als in het plaatje. Over elke korte pin zet je een weerstand :)

  • @mahela1993
    @mahela1993 2 ปีที่แล้ว +1

    Thank you for the video.
    While this does a fantastic job of explaining the Principle of a rotary encoder, the code can't really be used in a project because this it doesn't do any debouncing or filtering.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      The goal of my tutorials is to show the basics. The nice thing is that a challenge could be to add debounce or filtering by the viewer itself 😃

  • @user-hx2me8bg8e
    @user-hx2me8bg8e 5 ปีที่แล้ว

    Teach us!

    • @BasonTech
      @BasonTech  5 ปีที่แล้ว

      I will, don't worry. Unfortunately this is quite a busy time for me. I borrow the camera from a friend, so I am not always able to record new episodes. However, I have certainly plans to make new episodes this year :)

  • @EliasMjolk
    @EliasMjolk 3 ปีที่แล้ว +1

    I´ve tested it on other pins that arent 3 and 4 and it doesnt work, why does this happend? Before you ask, I´ve changed the code so that the pins are different. If there is something misssing please let me know!

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      Hi Elías, very good question! In the code you see we attach an interrupt to pinB. As www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ shows, for most Arduinos only pin 2 and 3 have external interrupts. So therefore you can only use these pins 😃

    • @EliasMjolk
      @EliasMjolk 3 ปีที่แล้ว

      @@BasonTech Thank you soo much for answering this fast!! This means we could only use this method for 2 simultaneous digital encoders??

    • @EliasMjolk
      @EliasMjolk 3 ปีที่แล้ว

      @@BasonTech Well I've seen that my arduino Leonardo has more than 2 pins with external interrupts. Thats means I only need one for each digital encoder? Thank you again and hope to see something from you soon!

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      Indeed 1 interrupt pin for 1 encoder. However there might be some PCB out there which can multiplex multiple encoders on a single pin

  • @startobytes
    @startobytes 4 ปีที่แล้ว

    Can you make more videos please

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      I would really like to. Unfortunately I currently have quite a busy job, which makes it very hard to record new videos. When times are less rural I will certainly record new videos :)

    • @startobytes
      @startobytes 4 ปีที่แล้ว

      @@BasonTech than can make the videos on the weekend, Not?

    • @startobytes
      @startobytes 4 ปีที่แล้ว

      Can you make 1 Video per week?
      I Hope you can make it.

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว +1

      @@startobytes Unfortunately that is not going to work. Keep in mind that creating a video requires preparation, recording, editing, writing the code, artwork and updating the website. Topic getting more complicated as well, and therefore preparation will take more and more time before I can start recording :)

    • @startobytes
      @startobytes 4 ปีที่แล้ว

      @@BasonTech thanks,

  • @mihalceaalexandru9114
    @mihalceaalexandru9114 ปีที่แล้ว

    Where are interrupts used?!

  • @darrenmurrey637
    @darrenmurrey637 6 หลายเดือนก่อน

    that annoying that there are only two interrupts . i want to run two encoders and just realised why 1 is having difficulties

    • @BasonTech
      @BasonTech  6 หลายเดือนก่อน

      I believe the Arduino Mega has more interrupts.

    • @shokdj1
      @shokdj1 6 หลายเดือนก่อน +1

      Cheers mate I appreciate your info I may buy a few mega’s. Turns out there’s another way I found something a library that give you interrupts on any pin. There a catch it stops interrupts on everything but it doesn’t matter on my project as you’ll only use 1 thing at a time so it’s working pretty decent now. Although I’m trying to work on an optical rotary encoder and 3d printing the rotary ir blocker there’s just not enough ticks per rotation on a general encoder for what I need it for.

  • @Leviathan0707
    @Leviathan0707 ปีที่แล้ว

    my encoder bounce all the time and it is driving me crazy, how do you make it not bounce

    • @BasonTech
      @BasonTech  ปีที่แล้ว +1

      Maybe there is a malfunction in the rotary encoder? Have you tried another one?

    • @Leviathan0707
      @Leviathan0707 ปีที่แล้ว

      @@BasonTech I try all of it mate. From the rotary encoder with breakout board to the stand alone one, they all bounce the same way to me :((( it's pretty frustrating

    • @BasonTech
      @BasonTech  ปีที่แล้ว

      If that is the case, it should be either your wiring, or your code. Please check the pin layout of your encoder to be absolutely sure the pins are not labeled different than mine in the video

    • @Leviathan0707
      @Leviathan0707 ปีที่แล้ว

      @@BasonTech i double check everything for an entire month, I search on youtube a found that many people have the same problem as me. But it seems like you don't encounter such problem at all

    • @BasonTech
      @BasonTech  ปีที่แล้ว +1

      Indeed not at all, it just worked. Are you using the Arduino Uno as well?