Arduino Tutorial 32: Understanding and Using Joysticks in a Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 32 in our legendary our new are improved series of tutorials on the Arduino what we're gonna do today is we're gonna learn how to incorporate joysticks into your Arduino projects so what I will need you to do is start with a large mug of ice-cold coffee and I will need you to get out your Lego super starter kit don't have one look down in the description click on the link pick one up 35 bucks from Amazon and it has the Arduino and a boatload of components the nice thing is is that it does make life a little bit easier if we are working with identical hardware so it does make things a little bit easier so let's just jump right in and let's learn how to use this joystick so this is the equipment that you will need after I get out of your way this is the equipment that you will need for this project you are going to need your breadboard you are going to need the Arduino you are going to need the joystick as seen here and you can find that pretty easily in your kit you are going to need your joystick knob and then we will jump right in and start connecting things together all right one of the things that you notice about this joystick is that it has male pins as output so to connect it to the Arduino you need to use this ribbon cable from your kit and you see it has female pins on one end and male pins on the other so we can connect up and then plug it in but before we connect it let's saw let's kind of understand how a joystick works and a joystick really is very simple what it is is it's two potentiometers and you've already learned how to use potentiometers because we did that in Lesson number 12 now if you're not sure how potentiometers work I really strongly recommend that you go back and watch lesson number 12 because we'll make this one a lot easier but what this joystick is is you can think of it as a potentiometer which is a variable resistor and another potentiometer okay and then here is the center tap of the right potentiometer and here is the center tap of the left potentiometer and then this kind of is in the potentiometer case and then in the joystick case and then these two potentiometers share a common 5 volts and then they share a common they share a common ground all right and then also there is a switch like this there is a switch a switch pin that goes to ground okay like that so let's look at this and I think maybe I can zoom in a little bit and you might be able to see this a tad bit better alright give it a chance focus pretty well that time didn't it okay so let's look at this if you can see this with the pins pointing to the left the top connection is the ground and so that is here everything connects to the ground inside and then you have below that you have five volts the next pin is 5 volts well that's the common 5 volts for both of the potentiometers next up you have V R X so this would be the voltage of the resistor in the X direction well what does that mean that means this potentiometer right the resistance of this potentiometer changes as you move something well this is saying it's in the X direction so as I move this left and right that's the X direction this resistance changes and therefore this voltage changes and similarly over here we have the next pin-down which is v ry v r y and so v ry is the tap the center tap off of this second potentiometer now this switch down here is normally open but we have the ability if the knob is on okay so we can go left and right changes VXR up and down okay up and down change v ry and then if we take this and press it you hear a click or maybe you can't hear a click maybe you can hear a click okay that click is opening and closing this switch and so the joystick is really very simple to operate if you understand the operation of potentiometers but if you had two potentiometers and just turning them like this it's not as intuitive as having this and you can think sort of like an airplane goes down and airplane goes up and an airplane goes left and airplane goes right so like if you're trying to control something like drive a robot or control some flying device or or other things like that the joystick is a very intuitive way to give input to your circuit okay let's try to get together and hook this thing up I'm going to take this off I'm going to bring my ribbon cable in and then let's see if this this might be a suitable magnification to show this but we need the female ends towards the joystick and I'm going to start with the black on top because what is our first connection our first connection is oh my goodness let me see if I can dismiss that okay sorry about that all right so everyone is trying to call me today okay so we're gonna take the black wire which is the top one and we are going to hook it to ground the next wire is the white one and it goes to five volts stop that nonsense alright the next one is the rx that is that Center tap on the X potentiometer and it looks like that next wire will be the gray wire okay and then we have the next wire is the purple wire which will go to VR Y you see I'm just hooking them in order in the order that they're coming and then the next one is the switch and the next wire is the blue wire okay and when I can I like to kind of hook things up in a way that have meaning and so a lot of times ground is black and a lot of times the hot wire can be like white or red and so it sort of makes sense to hook it up that way so now we have this hooked up now let's hook up over on the arduino side and on the arduino side we had okay first the black was ground and so i'm going to put the black over here to the GND and i'm going to use the GND that is next to 5 volts why am i going to do that because my next pin my white wire is 5 volts and you see if i put those together the wires that are adjacent plug in to adjacent pin so white goes to 5 volts and then black goes to ground the next one gray I believe if I am not mistaken gray was the X that X potentiometer so I will put that in a zero because again a zero is very handy I could put it in any of those eight pins but a zero is nearby and handy let me see I mean that's a pretty good focus and then the brown wire is the next one which is the voltage across the Y potentiometer so I will plug that in and then we're left with the blue wire in the blue wire we will do a digital read on that and so I will connect that over here to digital pin 2 and now I have this hooked up and it's hard to get this it's hard hard hard to get this where it stays upright because this cable is kind of stiff and so it is going to be hard for me to make this as neat as I want it so now if I come over here we haven't written our code yet but if I put this back on here if I move it left and right I should be changing the V X value if I move it up and down I should be changing the V Y value now if we are doing analog reads on those voltages we know that we read a number between 0 and 10 23 a number between 0 and 10 23 well let's think about X in the neutral position this is up so we would expect that to be in the middle which would be about 512 and coming this way we would be moving towards 0 on the X and coming this way we should be moving towards 10 23 in the Y we should be reading about 512 in the neutral position and the way this works is it goes down when you go up and then it comes up when you pull back and so it's kind of like an airplane that if the stick goes forward the nose goes down so if the stick goes forward the value that you read goes down if I remember this right all right in fact in fact I did it wrong because this should have been to the left and so with it like in this orientation X is low X is high Y is low Y is high but you'll figure that out when you read the values and so let's see if we can move back and actually start coding ok let's start coding so I will come I will come over to this other view let's get a good view here I think this is a pretty good view all right so we're going to think about programming this joystick we're going to think about it as two potentiometers and so let's think what we need to do we need to set up our pins well what all pins do we have well we have a X pin and the X pin I believe was a zero if I'm not stakin the gray goes to no and I pulled these wires office I was twisting around let me get those back on these ribbon cables are kind of stiff so they're a little hard - they're a little hard to do okay so the gray is is X and so exponents say ro and then remember the Y pen was right next to it which was a 1 and then we also had the switch will call it switch pin and that was digital pin - okay now we're gonna read values since I'm gonna have an an X Val in that it's not going to be set because we're gonna read it and then I'm gonna have a Y Val and I'm not going to set that because I'm going to read it and then I'm gonna have a switch Val switch Val and understand the switch is going to be a digital read so it's going to read either a 0 or a 1 if the switch is up it should read a 0 and know if the switch is up its read a 1 and then if you press the switch down it should read a 0 all right now we need to turn on our serial monitor so I will do serial dot begin for the most excellent 9600 baud rate I will do my pin modes we are going to set X pin as an input as you could imagine pin pin mode and that is pinmode not pin modes pinmode pinmode Y pin is an input input like that and then pin mode s and is also in and put ok now s pen is an input it's pin - what we want to do is we want to do a digital read from it but remember it's just a switch so the way we would need to do it is we would need to put a pull-up resistor in series with a voltage and then read off of that but the easier thing that you can do is is that if you set pin two if you set s-pen too high even though you're saying it's an input set it too high you'll have that pull up resistor already in there in the penalty high and so it will work without having to do any other circuitry hope you understand if you don't go back and look at our series of lessons on using pull-up and pulldown resistors with with switches and might might help you understand pull-up resistors a little bit so I need to do a digital right and I'm going to digital right s-pen hi and that will set it up where we can read from it without having to do a lot of external circuitry all right we are down here in our void loop what do we need to do well I need to read ex Val so my x value a read and where do I read from X pin do you guys see if you name your variables good how much easier it is to write code and now similarly analogously perhaps I should say Y Val is equal to analog read and I'm going to read Y pin okay and then s val is going to equal to remember that was a digital pin and we're just looking for a 0 or 1 so I will do a digital read and then what am I going to read s pin alright I have three values now what am I going to do I'm going to delay just so it doesn't go too fast I'm going to delay by DT delay time and I need to be a good boy and come up here and declare that variable and delay time is equal to let's say 200 that should be about right that looks pretty good now what I want to do is I want to print out these values that I have done and I want to print them all out in a row so I can see my x value I can see my Y value and I can see my switch state so I will do a series of serial dot print and remember print goes across only when you use a println does it go to the next line so I'm going to serial dot print and I'm going to say X value equal and in quotes I'm printing a string right in quotes I'm printing a string in serial dot dot print now what do I want to print I want to print X Val okay serial dot print now what do I want to print the string Y value equal you can close that up now I want to serial dot print what am I going to print the variable Y vowel Y bell like that now serial dot print and I will say the switch state is and that will be a 1 in the unsession and then when I switch it will be a 0 if I am thinking about it right which I think I am a serial pint no serial print serial dot print this is the last one so I'll use the Ln so it will bring you back to the next line and I will print s Val ok man I wrote a lot of code there really quick what are the chances it is going to work better take a shot of coffee I need everyone to hold their breath while I am downloading this if everyone holds their breath it will work boom it worked it actually shouldn't say it worked I should say that it downloaded ok so let's come over here and let's look at my let's look at my serial monitor I will have to find a suitable window that looks like a suitable alright one thing that I see right off the bat is I should have put a space before the Y in the string so that the Y doesn't run into the X and similar the before the S I should have put a space because that's 508 and then Y and that doesn't make sense I have a stickler for good formatting so we're gonna come over here and before the Y we're gonna put a space and before the essence which we're gonna put a space and then we are going to download this again boom alright man I am I'm gonna I am going to say that this is good alright so hopefully you can see it I'll be mindful up here on the camera let me see if I can get out of your way further all right now does this make sense this sort of makes sense how does it make sense well we would expect the potentiometer and let's think about the X potentiometer on the joystick we would expect it to go from 0 to 1023 from 0 to 1023 well this neutral position is kind of up we would expect it about in the middle and 508 is about in the middle of 0 to 1023 about and then on y 0 to 1023 we would expect that to be in the middle in the neutral position and that is 512 5 and about in the middle and then the switch has not been pressed and so it is in the 1 state as I correctly anticipated on that okay now let's see what happens I'm going to move it to the left we expect the x value to start going down what's happening boom the X is going down all the way to 0 look at that what if I go the other way X comes up up okay up up up look at that all the way to 10 23 and then it goes back to the middle position this is working like a charm and if I move it carefully I can move the xvalue without affecting the y-value do you see that now why we're gonna go this way and so I'm gonna go up and look why comes down all the way to zero back to neutral now it's going to go as I pull it back to ten twenty three all right now if I go here like this I can change x and y so if I go right and down at least my ride in my down like that to the corner look at that I get ten twenty three ten twenty three what if I go up into the left I'm going to expect zero zero which I can get there zero zero up into the left down into the left I get about zero in ten twenty three okay up and to the right I get ten twenty three and zero so do you see the fine control that you can get with a joystick on something like controlling position or controlling two different things very precisely it works way better than sitting and turning two knobs it's really a very very effective input device but the moment you have all been waiting for if I press this does it recognize it as a switch hold your breath boom I got a zero I'll let it go I get a one look at that boom zero one there's been no trick photography here no sleight of hand this thing is actually working this thing is actually working okay guys this has been a fun project I really like joysticks are a great way to get input into your probe into your programs and so I'm going to give you a heads up on what the next lesson is going to be the lesson number 33 I am going to have you have your circuit your joystick circuit control two potentiometers left and right on a control two servos left and right on your joystick will move this servo from like let's say that the neutral position is 90 and then left is going to be zero to 180 and then the Y potentiometer on the joystick is going to take this one from zero one-eighty why is that neat we'll imagine if I put this servo on top of this servo like this then you could twist it this way and you could point it this way so imagine if you had a little laser pointer on here using proper eye protection and the low power cheap ones that you buy at Walmart and taking all the proper precautions you could take this and then you could aim the laser around the Loom using the joystick and that should be a whole lot of fun ok guys this is Palmer quarter from top tech topoi comm I hope you guys will leave a comment down below in fact you know what I really wonder I will be honest with you I wonder if anyone actually watches these videos to the end does anyone watch these videos to the end if you did let's see let me know you watched all the way to the end by leaving a comment of what the secret word is for today the secret word for today is indigo and there you see indigo my faithful guard dog in Africa this is my home Holmes home compound in Africa in indigo is my most faithful guard dog ok so if you watched all the way to the end Tata indigo to let me know also you might think about subscribing to the channel might think about giving me a thumbs up sharing with other people and let's get ready for that project next week oh I forgot to tell you you need to get these two servos because the servos that are in the the one servo that is in the oligo kit is kind of a cheap one get you a couple of these in the link down below in the description because really if you're going to be doing any pro you know any projects you want to have in your workbench some nice servos and these are really nice once they have a nice full range of motion and they're very smooth and very precise and so I think these are 1314 bucks apiece and it's good to have a couple of these in your tool kit ok remember what was the secret word the secret word for the day was indigo Palma quarter from top tech boy calm I will talk to you
Info
Channel: Paul McWhorter
Views: 70,745
Rating: undefined out of 5
Keywords: Joystick, Arduino, Projects, STEM
Id: B6YEQj4d5WU
Channel Id: undefined
Length: 24min 24sec (1464 seconds)
Published: Tue Oct 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.