ESP32 Tutorial - Using Servo Motors ( NEW Arduino IDE )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome back to my YouTube channel in this new tutorial I'm going to show you how to control a Servo motor from any sp-32 board so this Servo is called sj90 and it's like a motor that we can rotate but it can't rotate around itself basically we can rotate it to a specific angle and create some cool projects for example you could make a clock and use this Servo to indicate the hours the minutes and the number of seconds if you want in this video we'll be able to control it from the serial monitor if we write 90 and hit enter the servo goes to the 90 degrees we can move it to a precise value like 45 so before we get started make sure to smash the Subscribe button and the notifications Bell so that you don't miss my next videos and without any further Ado let's Dive Right In [Music] so in this video you will need an esp32 board and a Servo motor this is called sj90 but if you have another one you could use it with the same way all of the servos have three wires and we have to hook them up to the esp32 board so to hook up the servo to the esp32 board let's look at this diagram each Servo has a ground wire we have to connect it to the gnd pin of the esp32 board for me it is the brown wire then we have the middle wire which is red goes to the VCC or the five volt pin of the esp32 board and that will power up the servo this Servo is a little bit small you could power it up with 3 volts that's why I'm going to connect a dvcc to the 3.3 volt pan and it is this one on top and the last wire which is yellow goes to one of the gpao pins so that we can control it from the esp32 board for example you could use the gpio pin number 19. 4 and so on as you can see it's very simple to hook up I highly recommend you to pause the video and connect this circuit then let's move on to the Arduino IDE so that we can control the servo from the esp32 board first you have to install a library and that's because Servo Motors are a little bit complicated it's not like an LED that we can control using the digital write command we have to use one of the libraries that makes it easier for you to control it let's go to tools then manage libraries or you could click on this icon then let's search for the name and it is called esp32 then servo and it is this Library by Kevin I've already installed it make sure to install it using the install button then let's close this window first of all we have to include it using hash then include and let's enter the name make sure to write it with the same way the S here is uppercase then we can use this library to create a Servo object that represents our Servo motor the type is servo then we can give it a name like my servo but we have to tell this object that we are using the pin number 18 to control the servo and the way we do that is under this setup function using my servo which is the name of the object then Dot and we have a function that is called attach which takes the pin number I'm using 18. if you are using another number make sure to enter it or let's create a variable on top using int I'm going to call it Servo pen equals 18. and let's pass in the name instead Servo pin finally we can change its angle using my servo Dot and the function is called write which takes an angle between 0 and 180 degrees but this range can change according to the servo motor that you have this one is a little bit small it can go from 0 to 170 degrees let's start with the value zero and add the semicolon and that's pretty much it so let's test our code before that you have to select the board from this drop menu I've already selected the node mcu32s and com6 of the USB cable and if you haven't watched my first video about the esp32 board make sure to check it out I've talked about this board and how to change the Arduino IDE so that we can program it we can hit upload and if you get the message connecting you have to hold down the boot key and yeah it starts let's try with another one like 90 degrees the same thing you have to hold down the boot key and yeah it's about 90 degrees and before I finish this video Let's make a simple project we are going to control the servo angle from the serial monitor that you could open up from this button basically we're going to send the value between 0 and 180 degrees or 170. then we are going to write it to the servo if you haven't watched my video about the serial monitor make sure to check it out the link will be under the description so first we have to begin this real monitor and let's remove this line of code we don't need it let's write serial dot begin this takes double trade usually we use 115 200 for an esp32 board and under the loop function which is called over and over again we can check if we have received a message which is the angle using IF serial dot available so this is going to be true if we have a message in such case we can read it using serial dot read string but this returns a message or a string between double quotes to read a value or a number we use parse and then we can store it in another variable using int and let's call it angle equals the value that this function returns and finally we can write it to the servo using my servo dot right and let's pass in the value angle and that's simply our project but it is recommended to add a little bit of delay under the loop function because the function Loop is called so fast we can wait 20 milliseconds then let's set upload again and once it's done let's open up the serial monitor you could also go to tools then serial monitor before we send the value make sure to select it no line ending because we don't have to add a new line once we send the angle let's try with 45 and yeah it's working so I think that's pretty much it guys for this video I hope you like it if you have any question or comment make sure to write it under the comment section down below and I will see you in the next one
Info
Channel: Enjoy Mechatronics
Views: 80,312
Rating: undefined out of 5
Keywords: Arduino, tutorial, enjoy mechatronics, how to, electronic, microcontroller, getting started, esp32 beginners, esp32, esp8266, beginner, learn, sketch, basic tutorial, gpio, pin, howto, basics, arduino, course, espressif, chip, esp32 tutorial, ESP32, arduino ide, serial monitor, read from serial monitor, arduino serial monitor, Serial Monitor, servo motor tutorial, servo motor code, ESP32 Servo, sg90, Servo, Servo motors, NEW Arduino IDE, control servo, servo write, servo library
Id: e7D_n1LeyKk
Channel Id: undefined
Length: 8min 19sec (499 seconds)
Published: Fri Dec 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.