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
|
Sketch
How to install the library
If you haven’t installed it yet, please do so before learning. The steps to add third-party Libraries are as follows: open arduino->Sketch->Include library-> Manage libraries. Enter “ ESP32Servo” in the search bar and select “ESP32Servo” for installation. Refer to the following operations:
Use the ESP32Servo library to control the servo motor and let the servo motor rotate back and forth.
Sketch_18.1_Servo_Sweep
Compile and upload the code to ESP32-WROVER, 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:
1/**********************************************************************
2 Filename : Servo Sweep
3 Description : Control the servo motor for sweeping
4 Auther : www.freenove.com
5 Modification: 2024/06/21
6**********************************************************************/
7#include <ESP32Servo.h>
8
9Servo myservo; // create servo object to control a servo
10
11int posVal = 0; // variable to store the servo position
12int servoPin = 15; // Servo motor pin
13
14void setup() {
15 myservo.setPeriodHertz(50); // standard 50 hz servo
16 myservo.attach(servoPin, 500, 2500); // attaches the servo on servoPin to the servo object
17}
18void loop() {
19
20 for (posVal = 0; posVal <= 180; posVal += 1) { // goes from 0 degrees to 180 degrees
21 // in steps of 1 degree
22 myservo.write(posVal); // tell servo to go to position in variable 'pos'
23 delay(15); // waits 15ms for the servo to reach the position
24 }
25 for (posVal = 180; posVal >= 0; posVal -= 1) { // goes from 180 degrees to 0 degrees
26 myservo.write(posVal); // tell servo to go to position in variable 'pos'
27 delay(15); // waits 15ms for the servo to reach the position
28 }
29}
Servo uses the ESP32Servo library, like the following reference to ESP32Servo library:
1#include <ESP32Servo.h>
ESP32Servo library provides the ESP32Servo class that controls it. ESP32Servo class must be instantiated before using:
1Servo myservo; // create servo object to control a servo
Set the control servo motor pin, the time range of high level.
1myservo.attach(servoPin, 500, 2500); // attaches the servo on servoPin to the servo object
After initializing the servo, you can control the servo to rotate to a specific angle:
1myservo.write(posVal); // tell servo to go to position in variable 'pos'
Reference
- class Servo
Servo class must be instantiated when used, that is, define an object of Servo type, for example:
Servo myservo;
The function commonly used in the servo class is as follows:
setPeriodHertz(data): Set the frequency of the servo motor.
attach(pin,low,high): Initialize the servo,
pin: the port connected to servo signal line.
low: set the time of high level corresponding to 0 degree.
high: set the time of high level corresponding to 180 degrees.
write(angle): Control servo to rotate to the specified angle.
Project 18.2 Servo Knop
Use a potentiometer to control the servo motor to rotate at any angle.
Component List
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
|
Sketch
How to install the library
If you haven’t installed it yet, please do so before learning. The steps to add third-party Libraries are as follows: open arduino->Sketch->Include library-> Manage libraries. Enter “ ESP32Servo” in the search bar and select “ESP32Servo” for installation. Refer to the following operations:
Use the ESP32Servo library to control the servo motor and let the servo motor rotate back and forth.
Sketch_18.2_Servo_Sweep
Compile and upload the code to ESP32-WROVER, 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:
1/**********************************************************************
2 Filename : Control Servo by Potentiometer
3 Description : Use potentiometer to control the rotation of servo motor.
4 Auther : www.freenove.com
5 Modification: 2024/06/19
6**********************************************************************/
7#include <ESP32Servo.h>
8#define ADC_Max 4095 // This is the default ADC max value on the ESP32 (12 bit ADC width);
9
10Servo myservo; // create servo object to control a servo
11
12int servoPin = 15; // GPIO pin used to connect the servo control (digital out)
13int potPin = 34; // GPIO pin used to connect the potentiometer (analog in)
14int potVal; //variable to read the value from the analog pin
15
16void setup()
17{
18 myservo.setPeriodHertz(50);// Standard 50hz servo
19 // attaches the servo on servoPin to the servo object
20 myservo.attach(servoPin, 500, 2500);
21 Serial.begin(115200);
22}
23
24void loop() {
25 // read the value of the potentiometer (value between 0 and 4095)
26 potVal = analogRead(potPin);
27 Serial.printf("potVal_1: %d\t",potVal);
28 // scale it to use it with the servo (value between 0 and 180)
29 potVal = map(potVal, 0, ADC_Max, 0, 180);
30 // set the servo position according to the scaled value
31 myservo.write(potVal);
32 Serial.printf("potVal_2: %d\n",potVal);
33 delay(15);// wait for the servo to get there
34}
In this experiment, we obtain the ADC value of the potentiometer and store it in potVal. Use map function to convert it into corresponding angle value and we can control the motor to rotate to a specified angle, and print the value via serial.









