ESP32 vs Arduino: Servo Motor Control!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys want to build a robot if you're just starting off building robots I feel like there are four foundational principles you've got to understand if you're programming Arduino that would be things like digital right digital read analog right analog read look to have those four functions down there is so so much you can do with it and I would also add to that list controlling DC motors using a motor driver and controlling servos once you have those six things down you can add them together in so many different ways now there are tons of awesome tutorials out there for controlling servos with Arduino but today I wanted to focus on the differences between controlling servos with Arduino versus controlling servos with the ESP 32 now this can seem a little intimidating at first and there are a few key differences what do you understand those differences there's so much you can do with it let's take a look so here we have in Arduino Nano and an ESP 32 now I love this kind of Arduino Nano here so this is the RF Nano it has the NRF 24 l 0 1 radio built in which is awesome for creating wireless projects now the ESP 32 also has a ton of wireless communication built-in right we have Bluetooth ble we have a Wi-Fi of course we even have ESP now I love love love of that stuff so the Arduino has obviously been around for a lot longer and so when you think of a number of projects and all of the amazing things that people have done with Arduino over the years it's it's just extraordinary you know I think they deserve so much credit in helping shape this community of makers and young engineers and there are Arduino that have more PWM pins than the uno or the Nano you know in this case we're gonna be talking about the Arduino Nano when you know you can totally use the malagos 2560 if you wanted to if you needed to use Arduino and you needed more PWM pins but that being said if you are using an Arduino Uno or an Arduino Nano in this case you're stuck to 6 PWM pins that's 3 5 6 9 10 and 11 so let's see I don't think this this model even labels them for you obviously they're labeled with mark on and who know so we have pins D 3 D 5 D 6 D 9 D 10 and D 11 now one downside though is that the radio here uses SPI communication and so the the pins are actually hardwired 13 through 9 and so in reality we actually just have three five six and that's pretty much it and so if you only have three PWM pins and say you want to use some of them to drive a motor driver you don't have as many options and so if you need a lot of pins you could totally use an I squared C motor servo driver those are awesome if you if you really wanted to but if you wanted to do this all in one chip you may want to think about using an ESP 32 so when ESP 32 comes in a lot of different shapes and sizes I love this model right here with the external antenna I use these for all of our crazy robots and it's a ton of fun and so obviously the ESP 32 has way more GPIO pins and so what's so cool is with the exception of a few input only pins you can use almost any of these pins as a pwm pin for servo control now the biggest difference and I think this can trip people up sometimes is that there are not specifically PWM pins there are PWM channels and so even though there are 30-plus GPIO pins on here you can't use 30 different servos you want to but you can use sixteen and so what you do is you set up your PWM channels and then you attach pins to that channel and I know that can seem kind of confusing but that's why I'm making this video so I want to make sure that by the end of this video this difference is really really clear and you have no problems controlling either a servo with an Arduino Nano or an ESP 32 let's go ahead and take a look at the Arduino Nano so to demonstrate how to program a servo using an Arduino I thought I would bust out the AC BR robotic laser cannon kit this kit is a ton of fun we actually just finished a little bit of a redesign on this guy we added the screw terminal here for the laser and a screw terminal here for some external power this is a really really simple example here of how we can take some joystick data and we can correlate that to servo position now in order for us to get a better understanding of how this all works let's go and look at the code so we can break things down and talk about how to program servo control so just like the ESP 32 there are more than one way to program a servo using the Arduino some people like to get in there and actually control the PWM directly using the digital right microseconds command but the most popular way by far is to use the servo library now the servo library for Arduino has been around almost as long as Arduino has and it is really really useful for controlling servos it makes it really really straightforward and so the first thing we need to do is to add the servo library so the servo library is built-in whenever you download arduino so oftentimes whenever you're using a crazier library you have to download it and install it this is pre-installed in arduino so you don't have to add any other files all you have to do is call it ok so then we need to name our servo we're going to be super boring here and call it servo 1 all right now we need to attach our servo to a pin in this case we're gonna start with pin 3 so we're gonna start with the name dot attached and then the pin number pin 3 so I have servo 1 dot attached pin 3 now we mentioned this a little bit ago but if you want to control a servo using the Arduino you've got to use pins 3 5 6 9 10 or 11 and in some cases you need those higher level pins the you know 11 12 13 for our communication luckily if you're using I squared C some like I squared C sensor that's using a 4 and 5 so that gets out of the way but there are those options 2 3 5 6 9 10 and 11 okay the first thing we're gonna do it's going to be crazy all we're gonna do is we're going to tell it to go to zero degrees oh my gosh so crazy just kidding okay so we're telling this to go to zero degrees that being said if we send our servo to zero degrees and it was already at zero degrees there's no way for us to tell if it's actually working and so we're gonna get a little bit crazier here we're gonna go ahead and we'll say for half a second then we want you to go to 180 degrees stay there for half a second and so now this is super simple all we've done is we have imported the servo library we've named our servo we've told it to attach to pin three we've told it to go to zero degrees for half a second then go to 180 degrees for half a second okay so let's go ahead and we will plug this guy in upload this code and see the result sure enough our servo is moving from zero to 180 degrees I'll beat it a little bit frantically here so whenever we're talking about servo control we often want way more control than just going to zero and 180 degrees and so let's go ahead and take that look back in the code you see if we can't add a little bit more control alright so getting that basic servo movement is not very hard right so we named our servo we tell the servo which pin to go to we say what degrees that we want the servo to be at and for how long we've got a taken care of that thing said you often want way more control than just you know your servo going to a specific degree and so there are a few different ways to do that there are actually some great built-in examples in the servo library so if you go over to examples servo there's knob and sweep so the sweep uses a for loop to move a servo backwards and forwards over and over again and that's really great if you're unfamiliar with for loops and how those function we also have the knob example and that takes a potentiometer reading right it's from a like a potentiometer for a guitar or smaller potentiometers for breadboards or even like a joystick and correlates that movement with the the map command and that's also really really awesome but in this example I'm going to show you a super easy way to get a lot more control using position variables so we're using the ACB our robotic laser cannon as an example I'm going to go ahead and share the whole schematic for this project on the website so there's always a tutorial on our website that goes along with these videos where we can go in a little bit further depth and show pictures and stuff like that so if you're curious about what this actual schematic looks like say you want to build this on a breadboard head over to the website because we've got that information there but in this case I need a few more variables and so I'm gonna go ahead and make a position X variable and a joystick X variable you can name these variables wherever you want right does not really matter let's see you know actually I'm gonna go ahead and give this a value I'm gonna say position X equals 90 because we want our our servo to start off at 90 degrees okay so in the void setup we can do something actually pretty tricky here we can make sure that our servo always starts at 90 degrees so in the void setup right because that runs once at the start of the program we're still using pin 3 we say the very first thing I want you to do is go to position X okay now our void loop here is going to look a lot different so we created two new variables we had position X and we had enjoy X and so in this case I'm gonna first give joy X a value so I'm going to say joy x equals analog read a 0 and a 0 with the pin that I have hardwired in the circuit board but again check out the schematic we have on the website if you want to see this where this would actually be wired up on an Arduino if you were using a breadboard okay so how do sticks work it's actually really really cool so all the joystick is doing is it's taking in voltage in most case five volts and as you move the joystick forward and backward left and right all you're doing is changing the voltage using potentiometer so in reality there's a two potentiometer in there one for X and one for y or one for forward and forward backward one for left and right and as you move the joystick you're changing the voltage making it go up or down and so what's cool is if we use the analog read command on the pin that we've plugged into our joystick we can read that change in voltage as a numeric value and if we were to just run this you'd see that you see the numbers 0 to 1023 whenever you're in the serial monitor and so we can actually use those numbers that we get from the joystick to move our servo okay so I have a value I say joy X is whatever voltage I read on pin a zero okay so we are going to need a conditional statement here so I'm gonna say if that value goes greater than 800 then I want my servo to go to position X else if Joy X is less than 200 I want my servo to go to position X now if we uploaded this code right here absolutely nothing what happened because we've said go to the same position under two different conditions but what we want to do is we want to move our servo based on that joystick position and so I said a few minutes ago that if you as you move the joystick if you were to open up the serial monitor you'd see the numbers 0 to 1023 so 10 10 20 3 would be if you held the joystick in one extreme and 0 in the other and so if I make a condition based on the number 800 then I know that servo must be I'm sorry that joystick must be close to one of the extremes because you get right about 500 or so in the middle and so if I move the joystick to one extreme move my servo again with 500 being in the middle if that value goes less than 200 I want you to also do something with the servo except in this case it's not going to move until we change the value of that position variable and so in this case I'm going to use a little bit of shorthand using plus plus and minus minus now if you're familiar with programming in general this is going to be you know super familiar to you but if you've seen this for the first time it can be kind of confusing and so all plus plus is is shorthand for add one so add 1 to the value of position X minus minus it's just the same it's take away 1 or subtract 1 and so now what's going to happen is that as I move my joystick forward and backward I'm going to increase and decrease the value of my position variable and as that value changes my servos will respond in kind now in reality this is going to move way too fast so I usually put a little delay in there let's say we'll do 20 milliseconds one thing that's really cool too is if you're a few readiness code here and you want your servos to move faster all you need to do is make this number smaller okay so I am now reading some voltage here and I am moving my servo forward and backward based on that there okay so here's we're gonna do we're to go ahead and upload this code again and then we'll see if we can tell their difference and so sure enough as we move the joystick left and right we have our servo moving left and right so for more information on the actual laser cannon itself head to our website we have tons of stuff all about how to program that on there also we have some more videos here on our youtube channel but before we move on to how to do this exact same thing with the SP 32 I want to show you one more thing this isn't exactly necessary but it's always a really good idea to add let's head back over the code really quick and so as we saw in that last example and we had nice steady control right so we had the voltage we were eating from the joystick we move the servo backwards and forwards and that worked really really well but there's one more thing that I always recommend at which is not necessary but I think it's a really good idea so we talked about how all this part of the code here is doing is making the position variable smaller or larger now here's the thing if you move this joystick forward right position variable here is getting smaller so it starts at 90 and that goes to 90 80 70 60 50 all the way down but if you keep holding that joystick down that number is gonna keep counting and so it counts all the way to negative and so obviously the servo can't go to negative 60 degrees right and so the exact same thing can happen on the positive side so the joystick variable goes forward that variable just gets bigger and bigger and bigger and bigger and it'll go past 180 so it goes to 180 190 200 and the servo just won't move and so what can happen is if you keep holding the joystick down it'll seem like the servos not responding anymore but the problem is your range is totally out of whack and so luckily there's actually a really easy solution so we're gonna add in a second condition all of at it is I've said hey if the joystick value goes greater than 800 and the position value is greater than zero then take away from the position variable so this will make sure that we never go past 0 now down here I'm gonna add in the exact same condition with a little bit of a twist here if position X is less than 180 because the servo can't move past 180 and so I've said that hey so long as position X is still less than 180 go ahead and add to the value of position X and so if you would upload this here you would see that you could hold the joystick down for as long as you want in a servo would never try to go out of its normal range and again that's not necessary but I thought I would add that just in case people were curious about the best possible way to use the position variable to control the servo ok so we've got the Arduino side down let's head back over to the RESP 32 so we talked a little bit more about that alright so in order to demonstrate how this works for the ESB 32 I thought I would bust out our brand new ESP 32 robotic arm driver board I am so pumped about this new board actually had this as a kit available up in the shop and right now I have one of our me arms plugged in and we've got this in the shop too now it is a great little robotic arm but it's gonna be perfect for showing us the differences between using servos with the Arduino and servos with the ESP 32 because if you look closely we're actually using the exact same servos for this robotic arm but one thing that's really cool about this driver board here is that we also have the whole patterns you can kind of see it right here for the much larger all-metal robotic arm that we use on a Voyager and Explorer robot so the code that you learn for this small robotic arm will actually be the exact same code if you wanted to use this for the Voyager or Explorer or put that other larger robotic arm on there so let's go ahead and take a look at the code is that what we talked about the differences between how you program the servo for the Arduino versus the ESP 32 alright let's take a closer look at what it takes to the exact same thing that we just did with the Arduino with the ESPE 32 so there are definitely a couple of differences here um but after we break down all the code you'll see that in reality we're using different numbers but there's really only one extra step and that's this guy right here but before we get to that let's talk about the Arduino file so I'm sorry the library file so we do need a library file just like before um now the name is not as simple as servo dot H that being said you know it's it's not that much harder to type in this other library file um so one thing I'll mention though there is an ESP 32 servo library it's an unofficial library that somebody else created that is is really really good and it functions the exact same way is the Arduino library that being said you don't have quite as much control over your servo and the PWM channel that you're creating and so I highly recommend that you try this method first definitely go download that library see how it works there's some also some great example code in that library that's not the method we're gonna show right here I want to show you guys how I would recommend doing it and it's a little bit more complicated but you have a lot more control so all you have to do here is grab this library file and this is included in the ESP 32 board file so whenever you load the ESP 32 into Arduino you have this library built in and so this led see that's referring to the LED channel and that's kind of misleading really all we're doing is creating a PWM channel but that's what that see references there okay so this first line of code right here this is where it starts to be a little bit weird here so we have LED see set up and so what this is doing it this is setting up our PWM channel so there's that C 4 channel and so what this means is we're using GPI up sorry we're doing we're setting up channel 1 we're gonna use a frequency of 50 Hertz and we're gonna use 16 bit resolution now if you don't know what the frequency and the resolution has to do with all this don't worry about that I highly recommend researching it further because it is super super interesting the most important thing to take away from this is that this is the line of code that we need to set up channel 150 Hertz is the frequency that we're gonna be using for our PWM signal and 16-bit resolution so whenever you're using a command an Arduino like analog right 255 you're using 8-bit resolution so that's actually one of the cool things about me SB 32 is it allows you to use a higher resolution for your pwm signals now for smaller servos that's not really necessary but you can do some crazy stuff for that and it's really really cool so this is the line of code that we need to set up our PWM channel number one okay now this next line of code is actually really similar to what we had in the void set up on our last bit of code we are attaching a pin to that PWM channel so in this case we are attaching pin number 23 to Cannell 1 the channel that we just set up so instead of just name dot attached we have led C or LED channel attached pin 23 so it is written differently but saying pretty much the exact same thing okay now if we want our servo to move what we need to do is we need to tell that channel how many microseconds we want our PWM signal to be now in this case 1638 is not as simple as just saying 0 I totally get that that being said once you know this year was the same thing as 1638 for this servo you can repeat that over and over and over again so in this case I'm telling channel 1 to create a PWM signal at 1600 and 38 microseconds now down here I have channel 1 sending out 7000 864 microsecond PWM signal and so that is the exact same thing as saying 180 degrees now for these up micro servos they actually go a little bit further than 180 degrees and that's actually one of the cool things about the using using this method here is that you have way more control so if your servo actually goes over 180 degrees you have that range to use that full range of motion so let's say you want it to go a little bit smaller or your servos a little bit different you can absolutely play around these numbers a little bit so you could like I don't know 7400 as your maximum or you could use the numbers that we use here right so you could also try instead of like 1600 here try 2,000 and see what happens so the numbers that I'm putting in here are directly correlational to the amount of degrees it just happens that this number right here was perfect for us for zero degrees and this number right here was perfect for us for 180 degrees and more than likely those numbers are gonna work pretty well for you guys to no matter what server using doubt that being said you may have to change them a little bit but it's super super easy to play around with so the code isn't that much longer in reality we only have one extra line of code and not setting up that channel let's go ahead and we'll upload this code here to our ESP 32 and see what happens once we have our code uploaded sure enough we see that the servo is moving from roughly 0 to 180 degrees just like we had before so in this case like I said feel free to mess with those numbers a little bit so say you want it to go a little bit further in not be quite so so why didn't either direction you can absolutely do that but you see that the differences between the two codes is not all that crazy so just like before we don't want to just have our servo moving by itself we want to have control over it and so we're gonna do is instead of using an on-board joystick since this guy doesn't have one we're gonna use the ps3 controller because that's a really great way to get joystick data wirelessly and use that data to control our ESP 32 in our servos so I actually have a whole guide on doing exactly this in another video and so if you're interested in that I go through that video it's awesome but I'm also going to show that here just so you guys see those differences in that code let's head back up record we know so you can see that difference all right so let's take a look at what it takes to get that same level of control that we had before except this time wirelessly because we're using a ps3 controller but using that position variable to move our servo backwards and forwards so now this example code does have a lot of extra stuff just for that ps3 controller I'm not gonna go into too much detail on that again I have other more in-depth tutorials on our YouTube channel and our website that go into that but the most important thing I want you guys to see is using those position variables so if you look here at our main conditional statements so I'm getting the ps3 controller data I'm using one of the joystick data channels rx the right joystick on the x-axis and I have different numbers here in terms of what I receive from the controller so in this case instead of eight hundred and two hundred being the joystick values I'm using negative five and five and obviously instead of 180 and zero I'm using 8,000 and 1500 because if you remember our minam's and maximums one was around 1600 the other was around 7800 this is doing the exact same thing so this is just more of a generalization but I'm doing the same thing so if my right joystick goes less than negative 5 therefore moving in one direction add 25 degrees to the value of position 1 if my joystick moves in the opposite direction then take away 25 from that now in this case we're not using plus plus and minus minus because we're dealing with a way wider set of numbers you know from 8,000 to 1500 and so if you used just plus plus or minus - your servos would move incredibly slow so the only difference here is is the numbers we're using but the concept is the exact same we have our same channel setup right we're still using pin 23 in this case our position variable we're using 5000 because it's right around in the middle if you remember in our last code for the art we know we used 90 because 90 degrees is exactly between 0 and 180 other than that with the addition of the ps3 controller code this is the exact same so let's go ahead and upload this here and then we'll see what happens so sure enough if we turn on our robotic arm here and we hold down that middle button to pair the ps3 controller once it is paired you'll see that I have that exact same control and so we've made a couple of steps from that first Arduino code right so in the Arduino code we use the onboard joystick to control servo backwards and forwards and so here we have a wireless joystick doing the exact same thing now admittedly the code is a little bit more complicated but if you break it down it's pretty similar so I hope that was helpful guys I'm planning on having a lot more content for this ESP 32 robotic arm driver board down the road maybe even an ESP now controller a Wi-Fi controller yecchh who knows there's so so so many options here but I hope you enjoyed this tutorial all about the differences between controlling servos using the ESP 32 and the Arduino they are both awesome platforms and both great for creating robots for more tutorials head over to our at stores page and our website we also have tons of really fun stuff here on YouTube so check it out have a great day guys [Music]
Info
Channel: Anyone Can Build Robots!
Views: 12,124
Rating: undefined out of 5
Keywords:
Id: J3Ndp9idxjQ
Channel Id: undefined
Length: 30min 1sec (1801 seconds)
Published: Fri May 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.