Using Servo Motors with ESP32

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're controlling servo motors with the esp32 we'll examine several ways of connecting and using analog servo motors directly and with i2c we'll also build a web-based interface to control a servo over wi-fi we're in the right position today so welcome to the workshop well hello and welcome to the workshop and today we are back working with servo motors again now we have worked with simple analog servo motors in many of our videos and i did a video on using servo motors with the arduino in which i explained servo motor operation in depth so if servos are brand new to you you might want to check that video out first and then meet us back here but for most of you i think we are aware of what a servo is it's a small dc motor that has an internal servo mechanism that measures the shaft position and you can tell the servo motor to go to a specific angle and it'll move the shaft into that angle now most servo motors do not travel a full 360 degrees common motors travel 180 degrees you can also get 270 degree servo motors there are continuous rotation servo motors but we won't be dealing with those ones today now analog servo motors are pretty easy to drive and you can drive them with any microcontroller we've driven them with the arduino you can drive them with a micro computer essentially anything that can make pulse width modulation in fact for a simple operation you don't even need a microcontroller you could use a couple of 555 timers to drive a servo motor and provide the pwm signal that an analog servo needs so why would you use an esp-32 after all it's more expensive than your average arduino well the answer would be the extra capabilities that the esp32 has the esp32 of course has a wealth of i o ports including some specialized ports like touch switches that you might want to use in your project it has both analog inputs that are more sensitive than the ones on the arduino and it has analog outputs but the feature of the esp32 that i think most people are the most interested in when controlling things like servo motors are the fact that it has built-in bluetooth and wi-fi capabilities so we can remote control our servo motors as well and we will be doing that a little later on in this video but first of all we're going to start off with a brief recap of servo motors and how they relate to the esp30 the esp32 is actually a good match for a servo motor servo motors require pulse-width modulation control signals and the esp-32 has multiple pwm outputs the position of a servo motor shaft is controlled by the pulse width and the esp-32 has several precision timers allowing very precise control over pulse width small low-cost servos are very suitable for battery-powered designs the esp-32 is also suitable for battery-powered designs having several low power and standby modes there are many practical applications for servo motors ranging from robotics to toys with a wide variety of i o ports sensors and communications options the esp32 is also suitable for many different applications one typical use of servo motors is in radio controlled hobby projects with integrated wi-fi and bluetooth the esp32 is ideal for creating remote control projects a typical analog servo motor has a three wire connection the first connection is the control input where the pulse width modulation signal is set to control the servo on most servo motors this is an orange wire next the power wire typically most servos use 5 to 6 volts dc but you should check your servo motor specifications to be certain of this and finally the ground wire which on most servo motors is brown the color coding however is not standard many servo motors use a black wire for the ground wire and many servos use a white wire for the control input most hobby servo motors use 5 to 6 volts dc for powering making them ideal for battery powered applications most of these servo motors will work properly with 3.3 volt logic but again you should check the specifications of your servo to be certain it is a good idea to power your servo motors independently from the esp-32 it's not a very good plan to share the power line between any motor and a microcontroller many of the esp32 pins have multiple functions so make certain when you're picking a port to use for pulse width modulation that that port isn't used for another function so now let's start working with servo motors and the esp32 and so here's an example of some servo motors that you could use in these experiments now this one over here is the most common type of servo you're going to find for hobbyists it's called an sg90 and it's available just about everywhere on ebay on amazon and probably your local electronics store as well now there also is an mg90 which is functionally identical but uses metal gears instead of the plastic ones used on the sg90 so if you have something that is going to have a risk of having some load applied against it you're probably better off using the mg90 now this one looks a lot like an sg90 but actually this isn't an sg90 it's a modified one and these are commonly available and they're called continuous rotation servo motors these are not the type of servos we're going to be using today a continuous rotation one can continually go in 360 degrees and the pulse width modulation signal determines the speed not the position on these motors so because they look alike and because they have the same connectors be aware that these motors which are marked as continuous servos are not the same as these servos which only travel from 0 to 180 degrees another very common servo motor here is the high tech hs 422 and this as you can see is a larger servo motor with more torque than the sp90 but it is driven the same way and can use the same control signals now over here i've got a little device that i picked up i think this was literally about two or three dollars they're very inexpensive and they're called servo testers again amazon and ebay are great places for them and they allow you to test out servo motors you apply power on this side connect your servo on that side and you can use this knob to determine the position it's also got a couple of other tests you can do like homing the servo etc and these are very popular with radio controlled hobbyists who use boats or aircraft that have a lot of servo motors they carry one of these with them just to make certain their servo motors are okay and finally this package of goodies over here is a package of things called horns and arms for servos let's just rip the plastic over here to get this off and as you can see it's got all sorts of attachments for the servo motor and these of course allow you to attach devices to your servo and you're going to end up with a whole bunch of extras never throw these out because you will eventually find a use for them and so there you go a few examples of servo motors that we can use in our esp32 experiments so now let's begin using the esp32 to drive our servo motors and as you'll see coding for it is very similar to the way it would work with an arduino but there is one caveat the servo library that comes with the arduino ide is incompatible with the esp32 so that gives you a couple of choices as to how to work with servos you can work with them manually because all you're doing is creating a pulse width modulation signal and you can certainly do that in code but that can get a little bit messy so you're better off using a library in most cases now there is an led library that comes with the esp32 and many people have used that to drive servo motors because that library drives led was pulsed with modulation signals and that's pretty similar to the type of a signal that we need for our servo so by adjusting a few parameters like the pwm frequency you can use the led libraries to drive servo motors but there are also replacement servo motor libraries that replace the ide servo library and that's the route that i'm going there are a couple of really popular ones and the one that i've chosen is called esp32 servo and this library i like because it is updated frequently in fact as of this recording period it was updated nearly two months ago and some of these other libraries haven't been updated for a few years so this means to me that this is an active product that is going to be continually looked for bugs and things like that and you can install this library from your arduino ide so let's go and take a look at the esp32 servo library and we'll run a couple of the examples just to see how we drive servos with the esp32 for our first experiment you're going to need an esp32 module based upon the esp wrong 32 chip now you do not have to use the identical module that i am doing but please note that the pin outs will be different on your module if this is the case and go by the i o connection numbers that i'm using not the actual pins on my module when you're hooking yours up you'll also of course need a servo motor i'm using a small sg90 servo but really any servo motor will suffice you'll need a power supply for this servo most servo motors work well on five to six volts dc so you could even use battery power if you wished finally we're going to need a potentiometer i used a 10k linear taper pot we'll begin by connecting the ground connection on the servo to the negative or ground connection on the power supply note that on many servo motors this is a brown wire and not a black wire we'll connect the servo's power wire which is generally a red wire to the positive connection on the power supply the servo's control wire which is generally orange but can also be white will be connected to the io18 pin on the esp32 module we're also going to tie the ground from the esp32 to the negative or ground side of the power supply this is a critical connection so don't forget it we will also ground one side of the potentiometer the other side of the potentiometer will be connected to the 3.3 volt pin on the esp-32 now you can power the esp-32 with this 3.3 volt pin but if you are powering it with 5 volts or using your computer's usb port to power it this will become a 3.3 volt output finally the wiper of the potentiometer is connected to the io-34 pin on the esp-32 and so now that we've hooked everything up let's go and take a look at some code we'll use to drive our servo motor in order to work with servo motors on the esp32 using the arduino ide we're going to have to make a couple of modifications to our ide now first of all you want to make certain that you're using a relatively recent version of the arduino ide i'm currently using version 1.8.13 which as of this filming point july 2020 is the most current version for linux so make certain you've got a current release of the ide next you're going to need to modify the ide in order to accept all the esp32 boards and i've covered that in my previous video on the esp32 you can also check out the article accompanying this video on the dronebotworkshop.com website and that will tell you how you can modify your ide in its board manager once you've done that we are going to need to install the library for the servos on the esp32 but before we do that i want to show you something i'm going to go down into file over here and go into examples and go down to the very bottom and you'll notice i've got a section called incompatible now these are incompatible examples with the board that i've currently selected which is an esp32 board and you'll notice i've got four of them i know they're kind of tiny and near the bottom of the screen but if you can see the third one on that list is the servo library this is the arduino servo library which is incompatible with the esp32 so when you're using the esp32 board manager this library and a number of other ones that i have are not available to you so we will need to install a library for the esp32 so what we are going to do is go under sketch and go into include library and go into our library manager by clicking manage libraries once your library manager is open go to the top and type in esp32 servo the library that comes up is the esp32 servo library by kevin harrington and john k bennett now i've already installed mine but if you haven't you'll have an install button over here and you can click it to install the library once you've done that we can take a look at one of the examples that comes with the library so we'll go to file we'll go to examples and we'll go to examples and customs libraries and we have esp32 servo here and as you can see there's a number of examples including strangely enough a couple of ones don't have to do with servo motors we're going to concentrate on the ones that do and the first one we're going to use is the sweep example now the sweep example is a clone of the arduino sweep example and all it does is it takes the servo and rotates it from zero to 180 degrees and then returns it to the other end and goes back and forth back and forth and you probably use this with the arduino in a servo if you've worked with servos before now we'll go over the code for this live for this example it's very simple really first we include our library so we start by including the esp32 servo library then we create an object called myservo and that represents our servo motor we can represent as it says here up to 16 servo motor objects using this now then we give it an integer that we're going to use to represent the position in degrees and so we're going to start off at 0 degrees and then we have something called servopin an integer that represents the gpio pin that we are using and as we're using gpio18 we're using that but as it says over here you can include a number of other different gpios that are pwm compatible now in the setup we need to allocate all the timers you will need to do this every time that you're using this library and so we do that over here and we can also set the period that we are using for the servo and we set it to 50 hertz which gives us period pulse widths of 20 milliseconds and that's exactly what we want now after that we do a my servo attach and this again is very similar to the original servo library we pass it the servo pin and we also pass it two different parameters and i'm going to explain those parameters in detail in a few moments so we'll be coming back to that then we go into the loop and the loop is very simple we just do two for loops essentially the first one counts from zero to 180 in increments of one and it writes that position out to the servo so it's going to step up in increments of one from zero to 180. there's a 15 millisecond delay that's applied after that and then it goes back through the for loop until it's up to the 180 degree point and then after that it goes through another for loop which is exactly backwards it goes from 180 to the position of zero and it decrements by one and does exactly the same thing so this will cause the servo motor to sweep back and forth so let's load that up to our esp32 and remember in a lot of esp-32 modules there's a button you're going to have to press in order to make the upload work i'm going to load that and we'll take a look at how it works now here's my esp32 module on a solderless breadboard you can see the servo motor over here and the potentiometer that we're going to be using in our next experiment is also connected now i've also connected one of these breadboard power supply units up to the breadboard and this is a very convenient way of powering the servo motor and eventually the esp-32 itself now the esp-32 right now is being powered by the usb connector from my computer so i've disconnected the 3.3 volt line from the 3.3 volt rail these rails are set to 3.3 and these are set to 5 volts the 5 volt line here is being used exclusively to power my servo motor and so it also has a power switch on it and the code is already running so all i need to do is hit this power switch and we should see our servo motor go through the sweep function and it is indeed going back and forth however if you're observant you will notice that we seem to have a little problem over here the sweep was supposed to sweep at a full 180 degrees and i think you can see from that this is clearly not going a full 180 degrees in fact it's probably going not a lot more than 90 degrees so there's a bit of a problem but there is a way to fix that so let's go back to the code and i'll show you what we need to do in order to get the full 180 degree sweep from our servo motor let's take a look at the code and see what we can do to resolve the issue of the servo motor not going a full 180 degrees during the sweep the answer to the problem is right down here on this line and it's where you do the attach now when you attach you attach the pin that the servo motor is going to be connected to the gpio pin there are also two other parameters that you're probably not that familiar with because they aren't used in the arduino implementation of the servo library these parameters determine the pulse width for the minimum and maximum position of the servo so in this case 1000 microseconds is the width of the pulse sent down the control line in order to bring the servo to the zero position and 2000 microseconds is the pulse width descent down the line in order to put the servo at the 180 degree position and it's pretty linear so you can infer from this at 1500 microseconds to put the servo in the 90 degree position etc etc now these values can be altered for different servo motors and that's the key the first one as i said represents the zero position so instead of 1000 i'm going to reduce that to 500 and instead of 2000 i'm going to bump that up a bit to 2400 and now we're going to upload this to the esp32 runner sweep example again and see what effect it has had on the movement of the servo motor so let's try the sweep again this time with our adjusted parameters in the code i'll start this and as you can see this is much much better i think it is going now a full 180 degrees and of course you can fine tune those parameters for your servo motor to get an exact 180 degree turn over here and every servo motor will be a little different even two servo motors of the same model number can require slight adjustments and by being able to do this you can actually get some very accurate positioning using this code so now that we've seen this let's go and move on to another code example now the next example sketch from the esp32 servo library that we're going to look at is the knob example and the knob example just emulates the knob sketch that comes as an example with the arduino servo library and it's a very simple sketch it takes the value from the potentiometer and in turn relates that to the position of the servo motor shaft so as you move the pot the servo motor shaft will move accordingly now we start off again by including our library the esp32 servo library and creating an object to represent the servo motor and defining the pin that we have it connected to gpio pin number 18. we also define another integer called pot pin and that represents the pin that we are connecting the analog input in we're going to use analog input gpio pin 34. the next parameter adc max is the maximum value that we can get from the analog to digital converter now the analog to digital converter in the esp-32 is a 12-bit converter as opposed to the 10-bit one that you would be used to in an arduino and so with a 12-bit converter we have 4096 possible values as opposed to 1024 that we would with a 10 bit however on the esp32 by using low level code you can reduce that to as little as 9 bits and so if you do you would need to change this number but by default it's set to 12 bits and you would leave it at 4096. we also have another integer called vowel and that's just simply the value that we are reading off the potentiometer now we have the setup over here that starts off exactly the same way that we did in our last sketch we allocate all of our timers we set the period to 50 hertz and then we do an attack and if you notice this i haven't changed this they've actually set this up for an st90 and they use the same values i did of 500 and 2400 for the minimum maximum position of the servo motor then we go into the loop and the loop is extraordinarily simple we do an analog read of the value on the pot pin and assign that to the valve variable and then we map that in order to change its value from 0 to the adc max which in our case is 4096 to 0 to 180 so a very simple use of the map command we write that value to the servo and then we add a 200 millisecond delay and we have to add a longer delay because we need to account for the fact that if we move the pot very fast the servo probably has a bit of time it's going to take to get to that position so a very simple sketch we'll upload it to the esp-32 and we'll look at it in action so here i am about ready to demonstrate the knob sketch and again the same breadboard setup now one thing to note is that i currently am powering the esp32 from the connection to my computer from the usb cable so i have its 3.3 volts disconnected from the power supply rail these are at 3.3 volts however that is what i'm using for the potentiometer because we want it to go from 0 to 3.3 and not 0 to 5 volts 5 volts is still powering the servo of course and if i were to disconnect this cable from the computer i would need to connect the power that's this red line to the 3.3 volt rail so having said that let's go and see if this works and it does as i'm moving the pot i can put the servo into different positions a very simple sketch and it just emulates the original arduino knob sketch but it'll give you a good idea of how to work with servo motors with the esp32 so now that we've seen that let's move on to the next step now sometimes we have a need to drive quite a few servo motors and the esp32 can drive a good number of servos through its i o pins but occasionally you'll need to drive even more than the esp32 or any other microcontroller can handle and for this a good component to use is the pca 9685 module now we have used these modules before with the arduino it's an i2c module that has 16 pwm outputs and can drive 16 servo motors it is also used for leds to drive 16 led strips and so it's very very versatile but if 16 isn't enough you can cascade these modules because you can select the i2c address in fact in theory you can have up to 62 of these modules and as each of them can support 16 servos that will give you 992 servo motors which should be enough for pretty well any project but remember this is an i2c device and the esp32 has more than one i2c bus so if you really need more than 992 there's an option for that as well by the way if you do have a project that needs more than 992 servo motors please post a picture of it up on the dronebot workshop forum and perhaps a picture of that 200 ampere power supply that you're using to drive all your servo motors now i don't have anywhere near that number of servo motors to drive in fact i'm going to limit my demonstration to this two servo motors but the principle i'm using can be used for any number of servo motors with the pca 9685 so let's go and take a look at this module and see how we can use it with the esp32 now here are a couple of pca 9685 modules i've got one here that is completely assembled and one that comes unassembled so to speak because all of the headers and the terminal for the power are not connected to it you'll also see there's a large capacitor over here that isn't on this board this board can actually have some advantages if you want to mount this for example onto a printed circuit board you could take the edge connectors and these edge connectors are actually duplicated on both sides so you can daisy chain these modules and you could put the pins underneath the board if you wanted to like that if you wanted to solder it down onto let's say a printed circuit board the capacitor is used sort of as a current sink for the motors and it's a good idea to have that in if your motors are chattering this can sometimes fix the problem although that's not the only reasons turbo motors will chatter it also is good if you have a sudden need for a lot of torque this can hold a lot of energy and this has a 1000 microfarad capacitor already installed in it and there's room for it on this board as well but the main connections as i said are repeated on both sides and so the i2c and the power connections etc and this terminal over here is where you would attach the power for your servo motor so you're going to want to use a power supply that has enough capability to power all of your motors and here of course is where you attach servos now this board can also be used for purposes other than servo motors it's quite common to use this with leds as well and use pwm to drive them so you can attach leds or led strips onto this as well so a very versatile and useful board let's go and see how we use it with the esp32 for this experiment once again we'll require an esp32 module and we'll be using a pca 9685 16 channel pwm controller we're also going to need a couple of servo motors i'm just using two servo motors but the pca 9685 can support up to 16 of them we'll need a power supply for those servo motors again you could use battery powers for a supply that is generally 5 to 6 volts dc you're going to need a couple of pull-up resistors as well for the i2c lines these should be approximately 2.4 k as we are using 3.3 volt i2c we'll begin by connecting the io21 pin on the esp32 to the sda connection on the pca 9685 module i o pin 22 will be connected to the scl line on the pca 9685 we'll connect the 3.3 volt connection on the esp32 to the vcc connection on the module make sure not to use the v plus connection as that's just another connection to the servo power supply we will connect the ground from the esp32 to the ground connection on the module next we'll connect both of our pull-up resistors from the 3.3 volt line to the sda and scl lines respectively now we can connect the power supply up to the power connector on the pca 9685 module and finally we can attach our servo motors to the module i'm showing my motors in position 0 and 12 but that's just because it was easier to illustrate here and you can actually use any of the connections that you want to and just account for them in code so now let's take a look at the code we can use to run our servo motors with the esp32 and a pca 9685 now to work with the pca 9685 you're going to need to install one additional library this is the adafruit pwm servo driver library and you can get this library through your library manager and install it in the identical fashion that we did earlier with the servo library for the esp32 now this is the same pwm servo driver library that is used at the arduino so if you've already used this device with the arduino you'll already have this library in your arduino ide so our sketch starts off by including the wire library which of course is the library that allows communications on i2c the pwm server library is installed next and it's dependent upon that wire library for its communications then we're going to create an object to represent our pca 9685 module and you can create as many of these objects as you wish in order to represent different modules the modules just need to have a different i2c address so i'm calling mine pca9685 and i'm using this address hexadecimal four zero which is the default i2c address of these modules if you don't use any of those jumpers to change it now we're going to define a couple of constants over here and these constants determine the number of ticks we are going to use now what's a tick you might ask well what happens with the pca 9685 is first you set it to a frequency and the frequency determines what the pulse width is going to be and then if you want to determine how much of the pulse is high and how much of the pulse is low the pulse is divided up into 496 sections and they are each called a tick and you can tell it when you want to start going high at what point so you can say i want to start going high let's say at tick number 100 and i want to come back low at tick number 1000 it will produce a small pulse and so we need to define the minimum and maximum values that we're going to be using in ticks and this will determine the pulse width now this is identical to what we're doing earlier with the servo library to define the minimum and maximum pulse width so that we knew where the server was at the zero point and the 180 point now you're going to need to experiment with these values over here these are the values that i'm currently using for my servo motor but every servo's going to be a bit different and i don't even know if these are necessarily perfect but you can start off with these values then we're going to define a constant that just represents each of the servo motors now remember you can have up to 16 of these in the pca 96 85. i've attached mine to connection number zero and to connection number 12. so i've done scr 0 is connected to 0 ser 1 is connected to 12 of course you can change this around and add or subtract as many of them as you wish we'll also need a variable to hold the speed for each of those motors and i'm calling mine pwm0 and pwm1 now in this setup it's pretty simple we set up our serial monitor which is going to use that to monitor the actual angle that we're driving our servo to we're going to print onto the serial monitor we're going to initialize the pca 9685 and then we're going to set its frequency now this is important because it can determine a pulse width frequency of a great range and we're going to set ours to 50 hertz and 50 hertz is pretty well averaged for a servo motor but again you can experiment with this if you're finding you're getting chatter or noise in your motors now in the loop i have to confess i got a bit lazy i should have actually written a function because i'm basically doing the same thing here four times over and what i've done actually is i've just blatantly stolen the code from the sweep sketch that we looked at earlier and if you recall the sweep sketch took the servo from 0 to 180 and then it took the servo from 180 to 0 when it kept repeating itself i'm doing that except i have four sections now because i have two servo motors so this is pretty simple way and again not necessarily the best coding but it gets the job done so i'm starting off by going from 0 to 180 and i'm using a map command to determine the pwm value from those servo min and servo max that i had before now this is the key to working with the pca 9685 over here i've got my pca 9685 object and i'm using the setpwm method and this is the way that you actually address one of the servos so i'm addressing servo 0 which happens to be on connection number zero and then this is the start point and the end point of the number of ticks you are using so we always start at the zero point and then the end point will be this pwm value and that will determine the width of the pulse that we are sending then we're going to also print that pulse width out to the sorry the position out to the serial monitor and we'll add a little delay to allow the motor to actually move into position then we go to motor one and do it in the opposite direction go back to motor zero move it the other way and motor one the other way and just repeat it and so it's a relatively simple sketch let's go and take a look at it in action now and so here's our pca 9685 setup with the esp32 as you notice i've got it onto a breadboard uh because it had a right angle connector i just mounted it directly to the breadboard it's being powered from the 3.3 volts on the esp-32 the esp-32 itself is being powered by the computer back over here i've just got four aa batteries which is a really good way of powering servo motors and those are connected to the power terminal on the pca 9685 and as you can see my servo motors are indeed going back and forth and if you look at my serial monitor you will also see that you can see the position of the motors being displayed on the serial monitor the position in degrees and so it does seem to work now we're just controlling two motors over here but this device is capable of controlling 16 and because you can cascade them you can control basically as many servo motors as you want with these modules and distant i2c connections so another great way of controlling servos with the esp32 now as i mentioned at the beginning of the video one of the main reasons someone would elect to use an esp32 over an arduino for controlling a servo is the built-in wi-fi and bluetooth capabilities and this would allow us to remote control our servo and we're going to do exactly that over a web page now in order to get the code for this what i did is i visited probably the most popular if not the best resource for esp32 on the internet and that is the random nerd tutorials website it's run by a couple rui and sarah santos out in portugal and they have excellent information about the esp32 and for that matter the esp8266 and so i'd highly advise that you check out their website and their youtube channel for some great information about the esp32 now i've taken some code that ruby has written i've changed it a bit because i'm using a different library and i styled my interface a little bit differently but otherwise it's basically the same code so let's go and take a look at what we need to do to control a servo motor over the esp32 with a web-based interface for our remote controlled servo experiment we can use the identical hookup we used in our initial tests as the potentiometer isn't actually required in this experiment you can remove it if you wish now let's take a look at the code we'll use to remotely control our servo motor through the esp-32 so here's our remote wi-fi control sketch and i'll go over it bit by bit just to show you how this actually works it's a good example of how to do any type of web server control with the esp32 actually we start off by including the wi-fi library this is the built-in library of course to attach the wi-fi and it comes with the esp32 so there's nothing to install there and we've already installed our esp32 servo library which we call over here as well again we create an object for the servo called my servo and we set up a constant for the gpio pin that we're using for the servo and in our case we're using pin 18. then you'll need to add your network cadentals over here your ssid and the password for your network we're going to start off a web server on port number 80 that's standard http we'll need a variable to store the http request header information and we'll also need variables to get the get values now the get values are the values that are going to be returned via ajax to our web servers so when someone moves the slider on the web page it's going to change the value and we're going to store them into these variables we're also going to use the current time and previous time using the millis function and the millis function tells you the amount of time that has elapsed in milliseconds since the device has been started and this is a way by taking the current and previous time that we can measure time delays without having to use a delay function and there are a lot of problems with using delay so this is actually a much better way of doing time delays etc now in the setup we'll go once again and allocate the timers for our servo motors and we'll set the frequency to it as well to 50 hertz as we did before and again we'll attach to the servo now i've used the values that i came up with for my sg90 servo motor and you can change these for the minimum and maximum travel of your servo if necessary we're also going to start the serial monitor and we'll connect the wi-fi and we'll get the ssid and password sent to the wi-fi over here and printer local ip address onto the serial monitor and this is very important of course because we're going to need to know on our web client where it is we have to look for our web server so we're going to open the serial monitor at the beginning of this and get the ip address that's been assigned to us now we're going to go in the loop in the loop basically we do two things we build a web page and we listen for incoming get requests from the client and so that's basically what our loop is doing over here i'm not going to go through it step by step because there's a bit to it but basically if you're looking for the web page itself it starts to build over here and we use a client print ln ln of course being with a carriage return to print out the actual html that's going to be our web page and so you can see over here this is actually the web page i've got it divided into the headers the html headers the css etc and i've got the page displayed a little bit easier to read on the article that accompanies this video in standard html so if you want to go through and change some of the colors or change the way the controls work etc etc you can take a look at that and then you can edit it over here because it's a little hard to read the actual html over here there's also javascript at the end here as well and then we end the page now we look for get data if there's data in the header this is what we want this means somebody has moved the control and has sent us back some data so we're going to get that data we're going to combine these two bits together and get something we call the value string and that's the string of the position we want to put the motor in there and then we're going to write that to the servo now we have to use a 2 inch to convert that to an integer because it's not coming in as an integer it's coming in as a string and then we can go and print it to the serial monitor just to see what the what the value actually is and that's an important uh troubleshooting technique if you can't get the motor to move you can take a look at your serial monitor and see if you're getting a value back over here and then after that we break out of the http and end everything and stop the client we'll print client disconnected and we'll go and do it over again and wait for a client to connect again so this is basically how you build a web server with an esp32 and in our case how you get instructions back that we can pass on to control the servo motor so now let's go and take a look at it in action all right well i have everything hooked up on the breadboard it's essentially the same arrangement i had in the initial experiments i've removed the potentiometer and i'm using the five volt from the breadboard power supply to power the servo motor the esp32 is being powered by the connection to the computer via the usb and i've just reset my esp32 and if you look up on the screen where we see our serial monitor you'll see the wi-fi is connected and i have an address over here 192.168.1.121 and i can copy that address and go over to a web browser and i can paste that address into here and we have our servo control screen over here and so this is the web page that's being generated by the esp32 and if i move this as you can see my servo motor is moving now if we go back over to the serial monitor what we can see is we keep seeing client disconnected after every page but notice these value numbers the 180 up over here the 80 over here the 33 over here let's go back onto our web page and you can see 33 is where we are here so if i move this again i get to 99 i'll go back to my serial monitor and there's my value of 99. so we're passing values back to the web server and these values are being used to move the servo motor so this is a very simple yet effective way of controlling a servo over a web server using an esp32 right well that about wraps up everything for today i hope that you enjoyed our look at using servo motors with the esp32 and given that you could use the wi-fi and bluetooth as a remote i'm sure it's opened your eyes to a number of different project possibilities now if you want some more information about the stuff that we covered today or perhaps you'd like to get the code samples that i used you'll find all of that on the article that accompanies this video on the dronebotworkshop.com website and there's a link below the video to that article there's also a link below the video to the dronebot workshop forums which is a great place to talk with like-minded individuals about all sorts of electronic projects be the esp32 arduino raspberry pi or anything to do with this wonderful world of diy electronics so i'd be thrilled if you would join the forum and join in on the conversation also speaking of a conversation i do send out a newsletter and there's a link to that below it's not a sales letter or anything just a way of keeping in touch with you and letting you know what is going on here in the workshop and if you have not subscribed to the youtube channel please do so i would be very appreciative if you would do that just click on the subscribe button below the video or on the robot in the corner of the video and after you do also click on that bell notification so that you'll be notified every time that i make a new video so until we meet the next time please take care of yourselves please stay safe and we will see you again very soon here in the dronebot workshop goodbye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 135,494
Rating: undefined out of 5
Keywords: ESP32, ESP32 Servo Motor, servo motor control, esp32 projects, esp32 servo motor control, esp32 servo motor web server with arduino ide
Id: zxBC1ivOVfM
Channel Id: undefined
Length: 47min 50sec (2870 seconds)
Published: Thu Jul 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.