Hi, I'm trying to use a couple of SG90 Servo for pan and tilt with ESP32-cam. Since when I ran the example suggested in the tutorial from Random Nerd, the mount with Servos was not working properly I just connected a single Servo to a generic ESP32 Dev module and tried to run a very simple sketch suggested in one of the forum discussions on this issue . The Servo is powered by a separate source ensuring 5V and it is duly grounded to the same GND of the ESP32. This is the code that is supposed to make alternating sweeps along a 180 degrees arc.
`#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0;
int servoPin = 13;
void setup()
{
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
In my hands, however when I run this code the servo makes at least three complete rotations in one direction then reverts repeating the same number of rotations and keeping on this way. I've read that one has to adapt the values of pulses to the servo that is used by I do not know which are the correct values for the SG90.
Before asking for your help I tried also other codes suggested in different tutorials and with one of these I met a very weird behaviour of the servo ( at least for me, a newbie).
The sketch allows for typing in the message line of the Serial monitor a value in degrees for the desired rotation angle and the servo is supposed to move according to this value.
If I type a value between 75 and 110 the Servo stands still, but if I type values above and below this range, the servo rotates withou stopping and the speed increases the more the values are distant from this range, with opposing rotation directions when increasing as compared with decreasing.
I think that this could related to a wrong pwm command but, since I do not have an oscilloscope (I'm a newbie) I do not know how to verify this hypothesis.
However, if I try to fade a LED connected to the same ESP32 by using pwm it seems to work fine.
Thanks for your help