Servo SG80 weird behaviour connected to an ESP32

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

Maybe you should tell more about your servos. Standard Sg90 does not make several complete rotations.

Sounds like you have a modified "continuous rotation" version of the SG90. If so, the output shaft cannot be positioned like a normal servo.

Instead, it will rotate at various speeds in either direction, depending on the pulse width applied by servo.write().

Hi, thanks all for your posts.
I think that your hypotheses are correct .
However I bought the two Servos SG90 9g TOGETHER with a pan & tilt mount for ESP32cam : they were sold from an Aliexpress vendor as a kit. So it's amazing for me that they 're selling servos that cannot properly work for the goal they are supposed to be used. This is the servo

Is there any workaround to use this type of servos to rotate for a desired angle ? Which library and code could be used ?
or is it just better to buy other servos ?
And, in this case, how can i be sure that the new servos won't suffer from the same problem ?
Thanks for your suggestions

Depends what you are trying to do exactly. If it's ok, that your servos are rotating only 180deg, you can just replace them with regular Sg90.

Otherwise you can try to run them "time based", calibrating certain time to certain movement.
There are also 360deg servos with position control, but they probably don't fit to your existing mechanical setup "out of the box"

Buy from a reputable seller. Aliexpress is not one of them.

Neither is Amazon, but at least Amazon honors returns. I recommend hobby suppliers like Adafruit, Sparkfun, Pololu, Conrad, etc.

OK
Thanks
I'll follow your advice

Thanks for your suggestions but I'm afraid that they are above my limits.
I'll try to buy a ouple of "regular" servos.
Have anice day

SG90 are regular servos, they turn about 180deg.
You bought modified SG90's probably because they came with that kit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.