ESP32Servo
Multiple-Servo-Example-Arduino.ino
Go to the documentation of this file.
1 /*
2  * ESP32 Servo Example Using Arduino ESP32 Servo Library
3  * John K. Bennett
4  * March, 2017
5  *
6  * This sketch uses the Arduino ESP32 Servo Library to sweep 4 servos in sequence.
7  *
8  * Different servos require different pulse widths to vary servo angle, but the range is
9  * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
10  * sweep 180 degrees, so the lowest number in the published range for a particular servo
11  * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
12  * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
13  * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
14  * degrees.
15  *
16  * Circuit:
17  * Servo motors have three wires: power, ground, and signal. The power wire is typically red,
18  * the ground wire is typically black or brown, and the signal wire is typically yellow,
19  * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
20  * considerable power, we will connect servo power to the VBat pin of the ESP32 (located
21  * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
22  *
23  * We could also connect servo power to a separate external
24  * power source (as long as we connect all of the grounds (ESP32, servo, and external power).
25  * In this example, we just connect ESP32 ground to servo ground. The servo signal pins
26  * connect to any available GPIO pins on the ESP32 (in this example, we use pins
27  * 22, 19, 23, & 18).
28  *
29  * In this example, we assume four Tower Pro SG90 small servos.
30  * The published min and max for this servo are 500 and 2400, respectively.
31  * These values actually drive the servos a little past 0 and 180, so
32  * if you are particular, adjust the min and max values to match your needs.
33  * Experimentally, 550 and 2350 are pretty close to 0 and 180.
34  */
35 
36 #include <ESP32Servo.h>
37 
38 // create four servo objects
44 // Published values for SG90 servos; adjust if needed
45 int minUs = 1000;
46 int maxUs = 2000;
47 
48 // These are all GPIO pins on the ESP32
49 // Recommended pins include 2,4,12-19,21-23,25-27,32-33
50 int servo1Pin = 15;
51 int servo2Pin = 16;
52 int servo3Pin = 14;
53 int servo4Pin = 32;
54 int servo5Pin = 4;
55 
56 int pos = 0; // position in degrees
58 void setup() {
59  // Allow allocation of all timers
64  Serial.begin(115200);
65  servo1.setPeriodHertz(50); // Standard 50hz servo
66  servo2.setPeriodHertz(50); // Standard 50hz servo
67  servo3.setPeriodHertz(330); // Standard 50hz servo
68  servo4.setPeriodHertz(200); // Standard 50hz servo
69  //servo5.setPeriodHertz(50); // Standard 50hz servo
70 
71 
72 }
73 
74 void loop() {
75  servo1.attach(servo1Pin, minUs, maxUs);
76  servo2.attach(servo2Pin, minUs, maxUs);
77  pwm.attachPin(27, 10000);//10khz
78  servo3.attach(servo3Pin, minUs, maxUs);
79  servo4.attach(servo4Pin, minUs, maxUs);
80 
81  //servo5.attach(servo5Pin, minUs, maxUs);
82 
83 
84  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
85  // in steps of 1 degree
86  servo1.write(pos);
87  delay(1); // waits 20ms for the servo to reach the position
88  }
89  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
90  servo1.write(pos);
91  delay(1);
92  }
93 
94  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
95  // in steps of 1 degree
96  servo2.write(pos);
97  delay(1); // waits 20ms for the servo to reach the position
98  }
99  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
100  servo2.write(pos);
101  delay(1);
102  }
103 
104  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
105  // in steps of 1 degree
106  servo3.write(pos);
107  delay(1); // waits 20ms for the servo to reach the position
108  }
109  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
110  servo3.write(pos);
111  delay(1);
112  }
113 
114  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
115  // in steps of 1 degree
116  servo4.write(pos);
117  delay(1); // waits 20ms for the servo to reach the position
118  }
119  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
120  servo4.write(pos);
121  delay(1);
122  }
123  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
124  // in steps of 1 degree
125  servo5.write(pos);
126  delay(1); // waits 20ms for the servo to reach the position
127  }
128  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
129  servo5.write(pos);
130  delay(1);
131  }
132  servo1.detach();
133  servo2.detach();;
134  servo3.detach();
135  servo4.detach();
136  pwm.detachPin(27);
137 
138  delay(5000);
139 
140 }
141 
static void allocateTimer(int timerNumber)
Definition: ESP32PWM.cpp:25
void write(int value)
Definition: ESP32Servo.cpp:131
void detachPin(int pin)
Definition: ESP32PWM.cpp:250
void setPeriodHertz(int hertz)
Definition: ESP32Servo.h:138
void attachPin(uint8_t pin)
Definition: ESP32PWM.cpp:231
void detach()
Definition: ESP32Servo.cpp:120
int attach(int pin)
Definition: ESP32Servo.cpp:73