1
\$\begingroup\$

I have interfaced a Tower Pro SG90 servo with a Raspberry Pi and ran sweep.py which takes the servo from 0 to 180° perfectly fine. When I interface the same servo with an ESP8266 and use the <servo.h> library, it won't go beyond 90°.

The servo is fine and is connected to a 5 V supply (I tried the on-board 5V and an external power supply as well).

It's mind-boggling and I can't find any help anywhere on this. The SG90 datasheet says the movement is 0 - 180°.

Here is a sample program:

#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(0);
}

void loop() {
  myservo.write(0);
  delay(1000);
  myservo.write(180);
  delay(2000);
  myservo.write(0);
  delay(1000);
}

I even tried writing in microseconds, but with the same results:

#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(0);
  myservo.writeMicroseconds(1500);
}

void loop() {
  delay(500);
  myservo.writeMicroseconds(0);
  delay(500);
  myservo.writeMicroseconds(3000);
  delay(500);
}
\$\endgroup\$
5
  • \$\begingroup\$ Well, there is no question so hard to answer anything. But if it works with something and not with something else, the code may not work correctly and does not output what you tell it to output. Have you read Arduino manual about limitations on whatever platform you run it? Have you measured and verified the Arduino platform does not output what you request? If you think this is about Arduino then it might be better to ask on Arduino.SE. If this is about electronics, please ask an answerable question. \$\endgroup\$
    – Justme
    Commented Feb 7, 2023 at 22:16
  • 3
    \$\begingroup\$ Try myservo.attach(0, 500, 2400); instead of myservo.attach(0); and let us know what happens. \$\endgroup\$
    – ocrdu
    Commented Feb 7, 2023 at 22:26
  • \$\begingroup\$ github.com/esp8266/Arduino/blob/master/libraries/Servo/src/… \$\endgroup\$ Commented Feb 7, 2023 at 23:22
  • \$\begingroup\$ @ocrdu Seems like its moving beyond 90 degrees, what would be the minimum and maximum values corresponding to 0 degrees and 180 degrees respectively \$\endgroup\$
    – Fenomatik
    Commented Feb 8, 2023 at 0:03
  • 1
    \$\begingroup\$ @Fenomatik: I don't know, see answer, where it says I don't know, but in a more elegant way. It also depends on the servo you use. \$\endgroup\$
    – ocrdu
    Commented Feb 8, 2023 at 1:01

2 Answers 2

2
\$\begingroup\$

The Arduino servo library doesn't play nice with the ESP8266 in combination with most servos. Maybe the ESP8266's PWM timing is different, I don't know. Whatever is different in the ESP8266, the servo library doesn't compensate for it.

You can get around this problem by changing the minimum and maximum timings in μs by using
myservo.attach(0, 500, 2400);
instead of
myservo.attach(0);

You may have to tweak the numbers for your particular servo by using trial-and-error.

BTW, this problem has been discussed in several Arduino forums, but I haven't seen a fix of the library on GitHub yet.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It worked , going from 500 to 300 is giving me full 180 rotation \$\endgroup\$
    – Fenomatik
    Commented Feb 8, 2023 at 4:10
0
\$\begingroup\$

esp8266 Arduino servo library file servo.h says this:-

// The following values are in us (microseconds).
// Since the defaults can be overwritten in the new attach() member function,
// they were modified from the Arduino AVR defaults to be in the safe range
// of publicly available specifications. While this implies that many 180°
// servos do not operate the full 0° to 180° sweep using these, it also prevents
// unsuspecting damage. For Arduino AVR, the same change is being discussed.
#define DEFAULT_MIN_PULSE_WIDTH      1000 // uncalibrated default, the shortest duty cycle sent to a servo
#define DEFAULT_MAX_PULSE_WIDTH      2000 // uncalibrated default, the longest duty cycle sent to a servo 
#define DEFAULT_NEUTRAL_PULSE_WIDTH  1500 // default duty cycle when servo is attached
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Grin. That almost makes it sound like a feature. The library's functional results' being different for different processors, where it is supposed to abstract away from that for Arduino IDE users, is a bug, I think. \$\endgroup\$
    – ocrdu
    Commented Feb 8, 2023 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.