How to Use Rotary Encoders on the Arduino - Ultimate Guide to the Arduino #25

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the three in one smart car and iot learning kit from Sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or Follow The Line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I had lots of fun building all the projects in it so click the link in the description below to order the kit from some founder foreign [Music] rotary encoders are useful for controlling servos stepper Motors and navigating menus on LCD displays rotary encoders look like potentiometers but unlike potentiometers rotary encoders can rotate around their axis without stopping and while potentiometers output a resistance value rotary encoders output a digital signal they can be used to determine the position of the knob in this video I'm going to show you how to set up a rotary encoder with a program that counts up and down when you turn the knob let's take a closer look at a rotary encoder the shaft of rotary encoders can rotate continuously around their axis without stopping as you turn the knob the rotary encoder has clicks or detents each detent generates a signal that's used by the Arduino to determine the position of the encoder knob this one has about 20 detents and a full rotation the number of detents per rotation defines the resolution of the encoder encoders with more detents have a greater resolution there's also a push button switch that can be controlled by depressing the shaft there are two types of rotary encoders you're likely to come across one is mounted to a breakout board like this one the key is ky040 and the other is a standalone encoder like this one breakout board rotary encoders have five pins clock DT switch VCC and ground Standalone rotary encoders have the same pins as breakout board rotary encoders but they also have an extra ground pin so how do they work inside the encoder is a disk of metal that's attached to the knob the disc has little tabs on it each tab corresponds to one detent then there are these two little sets of arms both of these inner arms are connected to the ground pin then the outer arm of this side is connected to the DT pin and the outer arm of this side is connected to the clock pin when the knob is turned The Arms contact the metal tabs on the disc making a connection between the ground pin and the DT pin or the ground pin and the clock pin if you look really close you can see that one set of arms is slightly shorter than the other set that makes it so only one set of arms contacts the metal tabs at a time so when you turn the knob either the DT pin is high and the clock pin is low or the DT pin is low and the clock pin is high let's look at a waveform diagram to see how this works when the encoder is rotating a square wave is output from both the DT pin and the clock pin when you turn the knob clockwise the DT pin goes High first at that point the clock pin is low when you turn the knob counterclockwise the clock pin goes High first and the DT pin is low by detecting which pin goes High while the other is low we can determine which direction the knob is turning let's see this in action by building a project that counts up when the knob is turned clockwise and counts down when the knob is turned counterclockwise connecting a rotary encoder to the Arduino is easy the clock pin connects to Arduino pin 3. the DT pin connects to Arduino pin 4. the SW pin connects to pin 12. and the VCC and ground pins connect to the 5 volt and ground pins now let's look at the sketch the first three lines are declaring variables for the clock pin DT pin and switch pin then we create a variable called count to store the count number the count will start at zero so it's set equal to zero this clock pin last variable keeps track of the last state of the clock pin it's set equal to low then we declare another variable called clock pin current that will store the current state of the clock pin and set it equal to low as well in the setup section we use pin mode to set each pin as an input we don't want the switch pin to float so we're using the internal pull-up resistor and we're going to print the counts to the serial monitor so we initialize that too now down to the loop section when I press the encoder knob in I want the counter reset to zero to do that we create a local variable called switch State and set it equal to the digital read of the switch pin this if statement sets the count variable equal to zero when the switch pin goes low now we take the clock pin current variable and set it equal to the digital read from the clock pin then we have an if statement if clock pin last equals low and clock pin current equals High which means that the clock pin went from low to high since the last time around the loop then if the DT pin is high at the same time decrease the count variable by 1. so these two if statements will be true if the knob is turning counterclockwise otherwise increase the count variable by 1. then we print the count now we set the clock pin last variable equal to the clock pin current variable that way the program can tell if the state of the clock pin is changed the next time through the loop okay let's upload this and see what happens so now I'll rotate the encoder knob clockwise it's definitely counting up like it should rotating it counterclockwise the numbers decrease like they should too but with each click of the knob the count is increasing a bunch of times it looks like we have some serious switch bouncing going on so let's see if we can fix that I tried debouncing this encoder a few different ways debouncing it with programming didn't work very well I also tried debouncing it with an RC circuit but that didn't work very well either the only way that really worked was with a Schmidt trigger I have the sn74 hc14 Schmidt trigger here I already explained how Schmidt triggers work in the switch debouncing video so I won't go into that here but basically we have the clock pin of the encoder going to one of the inputs of the Schmidt trigger the output of that trigger then goes to pin 3 of the Arduino the DT pin of the encoder goes to another one of the Schmidt trigger inputs the output of that Schmidt trigger goes to Arduino pin 4. I've also added one microfarad capacitors to each Schmidt trigger input the positive terminals of the capacitors connect to the Schmidt trigger inputs and the negative terminals connect to ground I've also tied all of the unused Schmidt trigger inputs to ground and connected pin a to the chip to 5 volts the same sketch we ran before all of the debouncing should now be taken care of with the Schmidt trigger so I'll upload this and see if it works now I'll turn the knob clockwise and counterclockwise foreign looks good the count increases and decreases once for each click of the encoder it works pretty well but the whole reason for having a rotary encoder is to use it to control other stuff so you're probably going to have a lot of other stuff going on in your sketch as we saw in the video on using interrupts the Arduino isn't very good at doing multiple things at the same time let me show you an example real quick this is the debounced rotary encoder circuit we just saw but now I want to have an LED blinking off at the same time the encoder is counting up and down I just added an LED and a current limiting resistor and connected it to Arduino pin 8. now we just need to add some code to get the LED to Blink this is the same sketch we've been using I just added a line to declare the pin variable set the pin mode of the LED pin to Output then at the end of the loop section I write the LED pin high for 100 milliseconds and then write it low for 100 milliseconds that'll make the LED blink on and off now upload it so I can see my LED blinking what happens when I turn the encoder the count only prints when I turn the knob almost a full rotation either way when the Arduino is waiting in the delay functions it's not able to read the encoder pins to fix this we'll have to use interrupts to read the encoder pins remember that the Arduino Uno has two interrupt pins pin 2 and pin 3. so we're going to need to switch the DT and clock pins over to Arduino pins two and three so I have the Schmidt trigger output for the clock pin connecting to pin 2 of the Arduino and the Schmidt trigger output for the DT pin connecting to pin 3. everything else is connected the same but here's the last sketch we saw first let me change the clock pin from pin 3 to pin 2. and the DT pin from pin 4 to pin 3. now I'm going to modify it to add an interrupt service routine for the DT and clock pins remember the variables need to be declared as volatile to be used in interrupt service routines so I'll go ahead and do that now create the interrupt service routine foreign I'll call it encoder interrupt so what do we put in here well pretty much all the code from the void Loop section that has to do with reading the DT and clock pins so all of this code here foreign T needs to be called in the setup section we use the attach interrupt function to do that remember the attach internet function takes three parameters the digital pin to interrupt the name of the interrupt service routine and the mode of the interrupt the digital pin I'm using for one of the interrupts is pin 2. so I'll type digital pin to interrupt and put a 2 in parentheses the name of the ISR is encoder interrupt and I want the interrupt to be triggered on rising and falling edges so I'll use change for the mode each internet pin needs its own attach interrupt function so I'll make another one for pin 3. okay that should do it let's try this out now so now if I turn the encoder knob clockwise the numbers increase smoothly once per click and the LED is still blinking counterclockwise same thing it works great rotary encoders are a great way to get inputs into the Arduino they can be used as volume controls menu selectors and Tuners but in the next video I'm going to show you another great way to get user inputs and that's with infrared remote controls and receivers [Music] Sun founder is my go-to source for sensors modules and other parts for the Arduino and Raspberry Pi they have a huge selection of stem Robotics and iot kits and lots of useful sensors and modules every product has an online tutorial with wiring diagrams and example code they also offer free shipping on all orders with no minimum give them a try at www.sunfounder.com next time you need to order some parts
Info
Channel: Circuit Basics
Views: 4,942
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: KX6K2WWJ2Yg
Channel Id: undefined
Length: 16min 37sec (997 seconds)
Published: Wed May 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.