9.1.6 9.1.6 Test Result
After uploading the code, the servo rotates from 0° to 90° and delays for 1s, and then rotates to 180° and also delays
for 1s. At last, it back to 0° to repeat these actions.
9.1.7 9.1.7 Extension
We have learned how to set the servo to a certain angle without pause.
In this extension experiment, we program to control the servo to slowly rotate from 0° to 180° and then slowly back to
0° degree by degree.
9.1.7.1 Code
Download codes: Codes
Download and unzip the file. Open the corresponding code file. Code file for Chapter 9.1.7.1 is named 9-1-7-1.
/*
Keyestudio ESP32 Robot Arm
9-1-7-1 tutorial code
Function: servo slowly rotates from 0° to 180°, and then slowly back to 0°
http://www.keyestudio.com
*/
#include
<ESP32Servo.h>
// create a servo objects Customizable name
Servo servo;
int
servoPin
=
16;
//Connect the servo to pin IO16
int
pos
=
0;
// position in degrees
void
setup()
{
servo.attach(servoPin);
}
void
loop() {
for
(pos
=
0; pos
9.1. 9.1 Control Servo
<=
180; pos
+=
1) {
// sweep from 0 degrees to 180 degrees
keyestudio WiKi
(continues on next page)
123
Need help?
Do you have a question about the ESP32 and is the answer not in the manual?
Questions and answers