I thought it might be good to a bit of a look into driving servos from the ESP32.
In our last project, we built a voice-controlled robot - to drive the robot we used a couple of servos.
Servos come in all sorts of shapes and sizes with some very high precision industrial versions available.
We’re going to be looking at the low-end hobby RC Servos. These are readily available and very cheap.
Typically these consist of a DC motor, reduction gearing and a potentiometer.
The potentiometer is used as a position sensor to feedback to the embedded controller and motor driver to tell it what position the servo is in.
Wiring is very straightforward - though confusingly the colour-coding depends on the manufacturer with three dominant schemes.
Most servos that you’ll encounter will require a 5-6V supply.
Though there are some that work on 12 volts and more.
For the demonstrations in this video, I’m using the same power supply as the ESP32, but you may want to use a separate power supply if you are driving multiple servos or have some high torque servos you want to use.
Controlling servos is pretty straightforward, you need to provide a 50Hz PWM signal.
Sending pulses with a width of 1ms will put the servo at 0 degrees.
Sending pulses with a width of 1.5ms will put the servo in its middle position - which would be 45 degrees for servos that have a 90-degree range.
And sending pulses with a width of 2ms will put the servo at its extreme range - 90 degrees or 180 degrees.
You can go outside the 1ms to 2ms pulse with range and overdrive your servo - but this can cause damage and strip the gears in the mechanism or even burn out the motor.
Let’s have a look at a servo in action. I’ve wired up my oscilloscope to the control line. You can see that we are sending a signal at 50Hz with 20ms period.
If we zoom in on the pulses we can see that the as the pulse width changes the servo changes position.
We also have available continuous servos - these servos don’t have the potentiometer and can spin 360 degrees continuously.
Setting the pulse with to 1.5ms will cause no movement to happen.
Setting it between 1ms and 1.5ms will cause the servo to rotate counterclockwise.
And setting it to between 1.5ms and 2ms will cause the servo to rotate clockwise.
The speed of the rotation is determined by how far away from 1.5ms you set your pulse width.
Continuous servos also have a trim pot to let you fine-tune the dead zone pulse width.
Let’s have a look at one of these servos in action.
As you can see when the pulse with is at 1.5ms the servo stops and as we move away from 1.5ms the servo speeds up.
Controlling servos from the ESP32 is really easy, the ESP32 has 16 led channels that can output PWM signals to the GPIO pins.
To set things up we call the “ledcSetup” function. We pass in the frequency of 50Hz and we pass in the resolution we want to use for setting the duty cycle. In this example, I’m using 16 bits which will let us set a value between 0 and 65535. With 0 being 0% duty cycle and 65535 being 100% duty cycle.
Once we called the setup function we just connect the LED channel to a GPIO pin.
You can actually connect multiple GPIO pins to the same LED channel if you want.
To set the angle of the servo we need to calculate the duty cycle.
We can work out the ms value for the duty cycle - this calculation assumes that you want to go from 0 to 90 degrees. If you have a 180-degree servo then change the 90 to 180.
We can then work out the value to send to the LED controller.
Finally, we just send the value using ledcWrite specifying the LED channel that we want to update.
So, that’s really it, It’s really easy to control servos from the ESP32.
There are 16 led channels so potentially we could control 16 independent servos.
There are several libraries out there to make this all a bit easier.
You should be careful about overdriving servos - you can get more movement range, but you might strip the gears or burn out the motors.
It’s also worth bearing in mind that there are more sophisticated servos available. Some servos provide a feedback wire so you can monitor the servo position, there are programmable servos and there are industrial servos.
Anyway, that’s it for this video, I hope you found it useful and enjoyable. If you did, please subscribe and hit the bell so you never miss out on a video.
ESP32 Dev board: https://amzn.to/3gb6fyc Standard Servo: https://amzn.to/3fQJi4j Continuous 360 degrees Servo: https://amzn.to/3imwx35
Thanks for watching and I’ll see you in the next video!
Want to help support the channel? I’m accepting coffee on https://ko-fi.com/atomic14