I have ESP32 connected to TZT servo SG90. This is the code
#include <ESP32Servo.h>
#define TURN_TIME 175
Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32
int pos = 0; // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
#define servoPin 17
#define MOTOR_1_PIN 22
void setup() {
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
}
void loop() {
myservo.attach(servoPin, 700, 2500); // attaches the servo on pin 18 to the servo object
myservo.write(0);
delay(TURN_TIME);
for(int i =0; i<180; i++)
{
myservo.write(i);
delay(TURN_TIME);
}
delay(3000);
for(int i =180; i>0; i--)
{
myservo.write(i);
delay(TURN_TIME);
}
myservo.detach();
delay(3000);
}
My servo starts rotating one way and reverse for few degrees, then after 5 seconds the servo starts rotating at one way forever..
I had no problems with arduino and nodemcu board. What should I change in the code?
EDIT: I have connected the servo with battery, they have common ground. The pin on esp32 that I am using is G17. "#define servoPin 17" in the code