Learnwithsteem : Teaching robotics using the ESP32 board - Fifth lesson
Hi steemians,
A servomotor is a device whose purpose is to reproduce a movement. In this article we deal with everything that revolves around this device such as their operating principle, the torque, how to control it with an ESP32 microcontroller.
What is a servo motor?
A servomotor is a system controlled by a measurement feedback. It can automatically correct its position according to this, which means that it will be able to maintain its position or correct it if it were to move over time.
There are several types of servomotors, a model like the one in the photo is a so-called analog model. Nothing very complicated, it simply means that it has a potentiometer (a variable resistor) to measure its position. Other models are digital, which means that the information feedback is made by pulses or an encoder. In this context, the electronic cards responsible for controlling the axes of a digital control (milling machines for example) are also called servo motor cards if they have an encoder.
To summarize if a motorized system has feedback allowing a correction of its position, it is a servomotor!
How to use a servo?
To connect your servo motor, you will most of the time have 3 wires as below:
- Brown Ground (-)
- Red Power supply (+ 5 V)
- Orange Signal PPM
There is on this connector, a power supply and a signal. We will therefore have to connect a + , a – , and the signal wire which will of course be driven with the – . Some servos do not have the same wire colors, here is a method to find your way around, take as – the color wire closest to black, as + the wire closest to red and the last as the signal wire. Many of these servos are only supplied with 5 volts, be sure to find out about its supply voltage before mounting it definitively, if in doubt 5 Vdc will generally do the trick.
The signal
You will need a square signal with a period of 20 mS, a servo like the one you see in the photo is made to perform a movement from 0° to 180°, the command will be from 1 ms to 2 ms most of the time. time 1 ms for 0° and 2 ms for 180° between the two 1.5 ms for 90° for example as in the diagram below.
The SG92R servo motor:
The SG92R model is one of the most popular because of its low price. It is a very small servo (23×12.2×27 mm) and it is often used in robot building or educational level. The gear system of this servo is made of plastic, which is why it degrades quickly. it is ideal for small projects or for testing. It is fully compatible with ESP32.
The main characteristics of a servo motor are: torque and speed. The torque is the force that is able to do on its axis, and is measured in Kg/cm. Proportionally if the torque increases, the energy consumption also increases. Velocity is angular or rotational speed.
The characteristics of the SG90 are as follows:
– Dimensions: 23 x 12.2 x 27mm.
– Weight: 11 g.
– Supply voltage: 4.8v.
– Speed: 0.1 s / 60° under 4.8v.
– Torque: 2.5 Kg / cm under 4.8v.
– Amplitude: from 0 to 180°.
Components Required :
To complete the assembly, you will need the following parts:
• Connecting wires
• Breadboard
• ESP32 development board
• Servomotor (SG90)
How to perform the assembly on the breadboard?
To carry out the assembly, we connect:
The connection is almost always the same and will be done at using 3 threads of which here are the main colors you can meet:
- Red: power supply wire to be connected to the 5V pin of the
ESP32 board - Brown: wire to connect to the GND pin of the ESP32 board
- Orange : positioning signal wire connected to the pin
D23 of the ESP32 board
Servo motor connections:
| Servo motor | ESP32 |
|---|---|
| Power (red) | 5V |
| Ground (black) | GND |
| Signal (yellow) | Pin 23 |
Program in micropython:
Here is the program that allows you to control a servomotor by the ESP32 card.
from time import sleep
from machine import Pin,PWM #PWM (Pulse Width Modulation) for servo motor operation
servo = PWM(Pin(23),freq=50) #Set Pin23 for servo motor with 50Hz of PWM
whileTrue:
servo.duty(20) #Turn the servomotor to 0°
sleep(2)
servo.duty(70) #Turn the servo motor 90°
sleep(2)
How does the code work?
Importing PWM and Pin classes
We start by importing the PWM class from the MicroPython machine module, to be able to create a PWM pin in order to interact with the input/output GPIOs. The time module is also imported for use in timelines.
from time import sleep
from machine import Pin,PWM
Creating the PWM pin object and setting the frequency
For this we will create a variable which will receive the PWM function with its parameters which indicates where the pin is connected in our case GP23 and the frequency of the PWM signal.
servo = PWM(Pin(23),freq=50)
For a frequency of 50Hz, and a pulse width between 1 and 2 milliseconds, the duty value is theoretically between 50 and 100, but in practice the servo motor must be tested to deduce the interval. between 40 and 115, between 25 and 125, ...
servo.duty(20) #Turn the servomotor to 0°
sleep(2)
servo.duty(70) #Turn the servo motor 90°
sleep(2)
The realization of assembly and the execution in image:
- Prepare all the necessary equipment:
- Start by connecting the SG92R servomotor to the breadboard and then use the wires to link its pins with the pins of ESP32 like this :
- Then connect the breadbord to the computer to inject the program :
- Open The python editor Thonny and write the appropriate code and save it into the microcontroller :
- Finally execute the code and we obtain this result :
Thank you for your follow and see you in the next part.
Thank you so much for sharing this great lesson on robotics. It had been interesting all along as i have actually learned a lot from this. Greetings sir!
Thank you.
Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @ripon0630 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.
Regards,
Team #Sevengers
The #learnwithsteem tag focuses on teaching through tutorials and lessons some knowledge, skill or profession that you have. Please avoid using it if it's not about that. Thank you!
Thanks.
Hello, @kouba01. Your article has been supported using the @steemcurator05 account.