// Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library
#include <ESP32Servo.h>
#define servoPin 13 //pines recomendados 2,4,12-19,21-23,25-27,32-33
// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33
Servo servo1; // create servo object to control a servo
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
servo1.setPeriodHertz(50); // Standard 50hz servo
servo1.attach(servoPin, 500, 2500); // attaches the servo
Serial.begin(115200);
Serial.println("Programa para probar un servo SG90");
servo1.write(0); // tell servo to go to position in variable 'pos'
delay(2000); // waits 15ms for the servo to reach the position
servo1.write(180); // tell servo to go to position in variable 'pos'
delay(2000);
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 15ms for the servo to reach the position
}
for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 15ms for the servo to reach the position
}
}