ESP32Servo
examples
Sweep
Sweep.ino
Go to the documentation of this file.
1
/* Sweep
2
by BARRAGAN <http://barraganstudio.com>
3
This example code is in the public domain.
4
5
modified 8 Nov 2013
6
by Scott Fitzgerald
7
8
modified for the ESP32 on March 2017
9
by John Bennett
10
11
see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code
12
13
* Different servos require different pulse widths to vary servo angle, but the range is
14
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
15
* sweep 180 degrees, so the lowest number in the published range for a particular servo
16
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
17
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
18
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
19
* degrees.
20
*
21
* Circuit: (using an ESP32 Thing from Sparkfun)
22
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
23
* the ground wire is typically black or brown, and the signal wire is typically yellow,
24
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
25
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
26
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
27
*
28
* We could also connect servo power to a separate external
29
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
30
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
31
* connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.
32
*
33
* In this example, we assume a Tower Pro MG995 large servo connected to an external power source.
34
* The published min and max for this servo is 1000 and 2000, respectively, so the defaults are fine.
35
* These values actually drive the servos a little past 0 and 180, so
36
* if you are particular, adjust the min and max values to match your needs.
37
*/
38
39
#include <
ESP32Servo.h
>
40
41
Servo
myservo
;
// create servo object to control a servo
42
// 16 servo objects can be created on the ESP32
43
44
int
pos
= 0;
// variable to store the servo position
45
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
46
int
servoPin
= 18;
47
48
void
setup
() {
49
// Allow allocation of all timers
50
ESP32PWM::allocateTimer
(0);
51
ESP32PWM::allocateTimer
(1);
52
ESP32PWM::allocateTimer
(2);
53
ESP32PWM::allocateTimer
(3);
54
myservo.
setPeriodHertz
(50);
// standard 50 hz servo
55
myservo.
attach
(
servoPin
, 1000, 2000);
// attaches the servo on pin 18 to the servo object
56
// using default min/max of 1000us and 2000us
57
// different servos may require different min/max settings
58
// for an accurate 0 to 180 sweep
59
}
60
61
void
loop
() {
62
63
for
(
pos
= 0;
pos
<= 180;
pos
+= 1) {
// goes from 0 degrees to 180 degrees
64
// in steps of 1 degree
65
myservo.
write
(
pos
);
// tell servo to go to position in variable 'pos'
66
delay(15);
// waits 15ms for the servo to reach the position
67
}
68
for
(
pos
= 180;
pos
>= 0;
pos
-= 1) {
// goes from 180 degrees to 0 degrees
69
myservo.
write
(
pos
);
// tell servo to go to position in variable 'pos'
70
delay(15);
// waits 15ms for the servo to reach the position
71
}
72
}
73
ESP32PWM::allocateTimer
static void allocateTimer(int timerNumber)
Definition:
ESP32PWM.cpp:25
Servo::write
void write(int value)
Definition:
ESP32Servo.cpp:131
Servo::setPeriodHertz
void setPeriodHertz(int hertz)
Definition:
ESP32Servo.h:138
ESP32Servo.h
loop
void loop()
Definition:
Sweep.ino:61
pos
int pos
Definition:
Sweep.ino:44
servoPin
int servoPin
Definition:
Sweep.ino:46
setup
void setup()
Definition:
Sweep.ino:48
Servo::attach
int attach(int pin)
Definition:
ESP32Servo.cpp:73
myservo
Servo myservo
Definition:
Sweep.ino:41
Servo
Definition:
ESP32Servo.h:121
Generated by
1.8.13