šŸŒˆ ESP32-S3 Rainbow: ZX Spectrum Emulator Board! Get it on Crowd Supply ā†’
View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

This tutorial explores driving hobby RC servos using the ESP32, covering wiring, control methods, continuous servos, and more! Read on to master controlling servos with ease.

Related Content
Transcript

[0:00] Hey Everyone,
[0:01] In our last project, we built a voice-controlled robot.
[0:05] To drive the robot we used a couple of servos.
[0:08] I thought it might be good to a bit of a look into driving servos from the ESP32.
[0:14] Servos come in all sorts of shapes and sizes with some very high precision industrial versions available.
[0:21] Weā€™re going to be looking at the low-end hobby RC Servos.
[0:25] These are readily available and very cheap.
[0:28] Typically these consist of a DC motor, reduction gearing and a potentiometer.
[0:34] The potentiometer is used as a position sensor to feedback to the embedded controller and
[0:39] motor driver to tell it what position the servo is in.
[0:43] There are plenty of breakdowns online on whatā€™s inside a servo. Just google ā€œwhatā€™s inside a servoā€.
[0:50] Youā€™ll find some quite good articles.
[0:52] Wiring a servo is pretty straightforward.
[0:55] Though confusingly the color-coding depends on the manufacturer with three dominant schemes.
[1:03] Most servos that youā€™ll encounter will require a 5-6V supply.
[1:07] For the demonstrations in this video, Iā€™m using the same power supply as the ESP32,
[1:13] but you may want to use a separate power supply if you are driving multiple servos or have
[1:17] some high torque servos you want to use.
[1:21] Controlling servos is pretty straightforward, you need to provide a 50Hz PWM signal.
[1:27] Sending pulses with a width of 1ms will put the servo at 0 degrees.
[1:33] Sending pulses with a width of 1.5ms will put the servo in its middle position
[1:39] - this will be 45 degrees for servos that have a 90-degree range.
[1:43] And sending pulses with a width of 2ms will put the servo at its extreme range - 90 degrees or 180 degrees.
[1:52] You can go outside the 1ms to 2ms pulse with range and overdrive your servo
[2:01] but this can cause damage and strip the gears in the mechanism or even burn out the motor.
[2:08] Letā€™s have a look at a servo in action.
[2:10] Iā€™ve wired up my oscilloscope to the control line.
[2:13] You can see that we are sending a signal at 50Hz with 20ms period.
[2:19] If we zoom in on the pulses we can see that as the pulse width changes the servo changes position.
[2:28] We also have available continuous servos
[2:32] these servos donā€™t have the potentiometer and can spin 360 degrees continuously.
[2:37] Setting the pulse with to 1.5ms will cause no movement to happen.
[2:44] Setting it between 1ms and 1.5ms will cause the servo to rotate counterclockwise.
[2:50] And setting it to between 1.5ms and 2ms will cause the servo to rotate clockwise.
[2:56] The speed of the rotation is determined by how far away from 1.5ms you set your pulse width.
[3:02] Continuous servos also have a trim pot to let you fine-tune the dead zone pulse width.
[3:09] Letā€™s have a look at one of these servos in action.
[3:12] 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.
[3:22] So, how is this done on the ESP32?
[3:26] Controlling servos from the ESP32 is really easy
[3:30] The ESP32 has 16 led channels that can output PWM signals to the GPIO pins.
[3:37] To set things up we call the ā€œledcSetupā€ function.
[3:41] We pass in the frequency of 50Hz and we pass in the resolution we want to use for setting the duty cycle.
[3:48] In this example, Iā€™m using 16 bits which will let us set a value between 0 and 65535.
[3:54] With 0 being 0% duty cycle and 65535 being 100% duty cycle.
[4:02] Once weā€™ve called the setup function we just connect the LED channel to a GPIO pin.
[4:08] You can actually connect multiple GPIO pins to the same LED channel if you want.
[4:13] So you can drive multiple servos from the same LED channel.
[4:17] To set the angle of the servo we need to calculate the duty cycle.
[4:22] We can work out the ms value for the duty cycle - this calculation assumes that you
[4:27] want to go from 0 to 90 degrees.
[4:29] If you have a 180-degree servo then change the 90 to 180.
[4:35] We can then work out the value to send to the LED controller.
[4:40] Finally, we just send the value using ledcWrite specifying the LED channel that we want to update.
[4:49] So, thatā€™s really it, Itā€™s really easy to control servos from the ESP32.
[4:54] There are 16 LED channels so potentially we could control 16 independent servos.
[5:01] There are several libraries out there to make this all a bit easier.
[5:06] You should be careful about overdriving servos - you can get more movement range, but you
[5:10] might strip the gears or burn out the motors.
[5:13] Itā€™s also worth bearing in mind that there are more sophisticated servos available.
[5:18] Some servos provide a feedback wire so you can monitor the servo position, there are
[5:22] programmable servos and there are industrial servos.
[5:26] Anyway, thatā€™s it for this video, I hope you found it useful and enjoyable.
[5:31] If you did, please subscribe and hit the bell so you never miss out on a video.
[5:36] Thanks for watching and Iā€™ll see you in the next video!


HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts