Hi! Welcome to ESP32tutorials from Robojax. My name is Ahmad Shamshiri presenting this tutorial
from Canada. in this tutorial we are going to see how we can control the servo motors
using ESP32 microcontrollers Wi-Fi and Bluetooth module. you can get the code
for this by clicking at the link below the video in the description which will take you
to Robojax.com/learn/arduino let's get started with this
Let me briefly explain the servo motors. servo motor is a type of motor that the shaft can be
controlled to move only 180 degrees or a half something like that and
then you can control precisely where it should be between zero and hundred and eighty degrees .
there are also other types of servo motors that for example there is something called
continuous servo (they are not real servo) which I have the link below it will rotate 360 degrees and you can control it
but these are the most common one and also you might get this type of servo motor which is very
heavy with high torque is the same way it can go degree stops so this is mg996R version
and this is the metal gear SG90this is all metal and this is a plastic gear so this will be
much cheaper like three or four or five times cheaper than this. but in general this is the
operation and you can control it so the servo motor has three wires the darker wire is the
ground GND. the five volts is always at the middle in case we make mistake in terms of connection and
RC car or airplane so the middle is always 5 volts. and this is a signal you might see it as
a black wire and this side red is always red and this side you might see white. but doesn't
matter so they're the same configuration is exactly the same ground five volts and signal
if you need to work with more (than 1) servo motor, make sure to provide (separate power) to servo.
do not user your ESP32 board to power them. they will damage your board.
also I have a video explaining how to control servo motors using potentiometer
and ESP32 the link for that video is below this video in the description. also
I have another video explaining how to control more than one servo motors
using esp 32 the link for the video is also below this video in the description.
Wiring explained
let me show you the wiring. the VCC or five is at the middle and this is a signal where
the signal goes to the servo to control it and I've extended using these male connectors to
female on this side and I have if you look at the USB connector I have connected the ground
here where it says ground GND and the five volts is connected to v5 and your board if the five
volt is somewhere else just find that pin and even if you have this type of word it
will work just connected to five volts of the pin here for example in here and then
the signal pin is connected to pin 14 here it says G 14 and the code we will just type 14.
Preparing Arduiono IDE to work with ESP32
if you have not prepared it (your Arduino IDE) you have to click on File-> preferences and then and this section
click and this line that you see I will provide the link, you have to paste it and include it here and click
OK and click OK I will provide you for that link. after that go to tools-> board-> manager
wait for this to complete it takes a while until you can type here. now type esp32 once you type
that in in your case you will see here Install. click install. in mine it has already - also I
have the "remove" so once you click on install it takes few seconds or maybe a minute everything
will be installed then click on Tools and select the port that your device is connected
Now your Arduino IDE is ready to work with ESP32
Code explained
first you need to download the library I've taken the library from this address the credit goes to this
jarick parel who have created this content but I've updated this for this purpose I've provided
the link to this page and provide you the link that I have modified and works this will not work
for you if you install it immediately because the server library in your Arduino will have
conflict with this and it will not work. you need to download the library as a zip file once you
downloaded the zip file that I've provided click on a sketch-> include library-> add dot zip library
and then and the screen pointed to where you have saved it in my case for example if it was inside
download and they say this is a library select it and click open. after that make sure to close
all instances of Arduino ide close all of them because you won't be able to see the examples
on the menu. once you close it reopen it then go file->examples scroll down and here you will see
servo ESB 32 and here click simple servo. I've provided the link to the original author but
I have updated the library so it can work easily. and this line is says include Servo_ESP32.h
this is the header file from library and then we define pin 14 and many cases it might
be G 14 printed on your port or just 14 that's a pin (GPIO pin) and then from this class we create an instance
of it as an object we call servo1 here we define angle this is the angle where the serval
goes so initial value will be here which will start at this value angleStep how often
or at what step you want to increment it if you put it 5 degree, at from 0 5 10 15 degrees and so forth if
you want to put 10 20 whatever you want. angleMin angleMax I put it because sometimes in
some application people need the servo not to go all the way from 0 to 180 and they want to put
certain boundary. so you will be able to change it if you don't want to change it yes leave it as 0
and maximum 180. inside the setup we initialize the serial monitor with 115200 this is the value
that if you click on tools' serial monitor you will see it the on the right side 115200 if you
select the wrong value for example 9600 you will see something like this. so make sure to select
the right value and then from the object that we created here
with a servo1.attach and servoPin as if we type 14 so this is attaching the servo to that
pin. but inside the loop it just gets position in degree stars from 0 it's being incremented
until it reaches 180 so this variable will go 0 1 because it's a for loop 0 1 2 3 up to 180 and
then servo 1 dot right and we write the position degree here the servo moves to the whatever
angle the self-evident 0 it goes to 0 degree if it's hundred it goes hundred and so forth. and this
also prints it (on serial monitor) and we give 20 millisecond delay so the servo can move after this is when it goes to
180 this is the opposite of that starts from 180 and decrements this minus minus means decrement
and unill it is above zero which means hundred and eighty it goes to 179, 178, 177
and so forth one degree at other time until it reaches zero and it will move the servo to that
location prints it on the screen (serial monitor) 20 millisecond delay and the loop will continue to take the same action.
Demonstration
Here I've uploaded the code as you can see the service moving and it
goes from zero to 180 degrees and then it goes back to zero.
I would like to thank my Patrons from Patreon.com
Subtitle typed on Nov 26, 2020 by Ahmad Shamshiri in Canada
I did my part to help you learn. not it is your turn.
You can subscribe to my channel 🔔 and 👍 the video and say simple thank you in the comment section.
If you watched this in language other than English, please put your comment and say which language you watched this video in.
If you want to donate cash, the link to my PayPal account is below this video in the description.
thank you for watching the tutorial from Robojax. if you liked this video and learned
something please thumb up 👍 as this will help my video in the search algorithm of YouTube.
if you have comment or question posted at the comment section below I try to answer
and reply. if you want to get updates of my upcoming videos make sure to subscribe.