Chapter 18 Servo
Previously, we learned how to control the speed and rotational direction of a motor. In this chapter, we will learn about servos which are a rotary actuator type motor that can be controlled to rotate to specific angles.
Project 18.1 Servo Sweep
First, we need to learn how to make a servo rotate.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
Breadboard x1
|
|
Servo x1
|
Jumper M/M x3 |
Component knowledge
Servo
Servo is a compact package which consists of a DC motor, a set of reduction gears to provide torque, a sensor and control circuit board. Most servos only have a 180-degree range of motion via their “horn”. Servos can output higher torque than a simple DC motor alone and they are widely used to control motion in model cars, model airplanes, robots, etc. Servos have three wire leads which usually terminate to a male or female 3-pin plug. Two leads are for electric power: positive (2-VCC, Red wire), negative (3-GND, Brown wire), and the signal line (1-Signal, Orange wire), as represented in the Servo provided in your Kit.
We will use a 50Hz PWM signal with a duty cycle in a certain range to drive the Servo. The lasting time of 0.5ms-2.5ms of PWM single cycle high level corresponds to the servo angle 0 degrees - 180 degree linearly. Part of the corresponding values are as follows:
High level time |
Servo angle |
|---|---|
0.5ms |
0 degree |
1ms |
45 degree |
1.5ms |
0 degree |
2ms |
45 degree |
2.5ms |
180 degree |
When you change the servo signal value, the servo will rotate to the designated angle.
Circuit
Use caution when supplying power to the servo, it should be 5V. Make sure you do not make any errors when connecting the servo to the power supply.
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “18.1_Servo_Sweep”. Select “myservo.py”, right click your mouse to select “Upload to /”, wait for “myservo.py” to be uploaded to ESP32-WROVER and then double click “Servo_Sweep.py”.
18.1_Servo_Sweep
Click “Run current script”, the Servo will rotate from 0 degrees to 180 degrees and then reverse the direction to make it rotate from 180 degrees to 0 degrees and repeat these actions in an endless loop.
The following is the program code:
1from myservo import myServo
2import time
3
4servo=myServo(15)#set servo pin
5servo.myServoWriteAngle(0)#Set Servo Angle
6time.sleep_ms(1000)
7
8try:
9 while True:
10 for i in range(0,180,1):
11 servo.myServoWriteAngle(i)
12 time.sleep_ms(15)
13 for i in range(180,0,-1):
14 servo.myServoWriteAngle(i)
15 time.sleep_ms(15)
16except:
17 servo.deinit()
Import myservo module.
1from myservo import myServo
Initialize pins of the servo and set the starting point of the servo to 0 degree.
1servo=myServo(15)#set servo pin
2servo.myServoWriteAngle(0)#Set Servo Angle
3time.sleep_ms(1000)
Control the servo to rotate to a specified angle within the range of 0-180 degrees.
1servo.myServoWriteAngle(i)
Use two for loops. The first one controls the servo to rotate from 0 degree to 180 degrees while the other controls it to rotate back from 180 degrees to 0 degree.
1for i in range(0,180,1):
2 servo.myServoWriteAngle(i)
3 time.sleep_ms(15)
4for i in range(180,0,-1):
5 servo.myServoWriteAngle(i)
6 time.sleep_ms(15)
Reference
- class myServo
Before each use of myServo , please make sure myservo.py has been uploaded to “/” of ESP32, and then add the statement “ from myservo import myServo “ to the top of the python file.
myServo (): The object that controls the servo, with the default pin Pin(15), default frequency 50Hz and default duty cycle 512.
myServoWriteDuty(duty): The function that writes duty cycle to control the servo.
duty: Range from 26 to 128, with 26 corresponding to the servo, s 0 degree and 128 corresponding to 180 degrees.
myServoWriteAngle(pos): Function that writes angle value to control the servo.
pos: Ranging from 0-180, corresponding the 0-180 degrees of the servo.
myServoWriteTime(us): Writes time to control the servo.
us: Range from 500-2500, with 500 corresponding to the servo, s 0 degree and 2500 corresponding to 180 degrees.
Project 18.2 Servo Knop
Use a potentiometer to control the servo motor to rotate at any angle.
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Servo x1
|
Jumper M/M x6 |
Rotary potentiometer x1 |
Circuit
Use caution when supplying power to the servo, it should be 5V. Make sure you do not make any errors when connecting the servo to the power supply.
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “18.2_Servo_Knop”. Select “myservo.py”, right click your mouse to select “Upload to /”, wait for “myservo.py” to be uploaded to ESP32-WROVER and then double click “Servo_Knop.py”.
18.2_Servo_Knop
Click “Run current script”, twist the potentiometer back and forth, and the servo motor rotates accordingly.
The following is the program code:
1from myservo import myServo
2from machine import ADC,Pin
3import time
4
5servo=myServo(15)
6
7adc=ADC(Pin(34))
8adc.atten(ADC.ATTN_11DB)
9adc.width(ADC.WIDTH_12BIT)
10
11try:
12 while True:
13 adcValue=adc.read()
14 angle=(adcValue*180)/4096
15 servo.myServoWriteAngle(int(angle))
16 time.sleep_ms(50)
17except:
18 adc.deinit()
19 servo.deinit()
In this project, we will use Pin(34) of ESP32 to read the ADC value of the rotary potentiometer and then convert it to the angle value required by the servo and control the servo to rotate to the corresponding angle.









