Controlling Servos with the ESP32
atomic14
Hey Everyone,
In our last project, we built a voice-controlled robot - to drive the robot we used a couple of servos.
I thought it might be good to a bit of a look into driving servos from the ESP32.
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 ... https://www.youtube.com/watch?v=GcOlBfT7UkM
64733964 Bytes