I have a MicroServer 9g SG90 attached to pin D3 of an UNO R4 Wifi. I am trying to step through each degree between 0 and 180 one by one with 1sec delay, but it fails to move on every increment, skipping roughly 9 out of every 10 degrees on average. Am I expecting too much from this component? My (debugging) sketch includes Servo library by Michael Margolis v1.2.1 :
#include <Servo.h>
int servoPin=3;
int servoAngle=0;
int delayTime=1000;
int baudRate=115200;
int x;
Servo myServo;
void setup() {
Serial.begin(baudRate);
myServo.attach(servoPin);
}
void loop(){
for ( x=0; x<181; x++) {
myServo.write(x);
delay(delayTime);
Serial.println(x);
}
}
Thank you so much. It will sound silly but I was following along with this youtube tutorial which recommended using the same version of Servo.h as he was to avoid a potential clash:
Thanks xfpd. x represents a specific angle writen to the servo, and I wanted to start at 0° and end at 180° so I guess that is 181 steps as intended.
I initially coded:
for (x =0: x < 180: x++) {
but then noticed the serial monitor was only counting up to 179 before returning to 0:
Thanks DaveX. Yes... I hadn't noticed that. They're definitely not 1° increments are they. Now that my sketch is working the change in movement is quite slight and very smooth.
Thanks also for introducing me to Wokwi... will definitely be having a play with that!
The problem may not lie in the microcontroller. The electronics within the servomotor may be designed with a certain dead band to prevent the motor from vibrating with a constant input.
Thanks stitech. I wasn’t sure what to expect from such a cheap plastic-geared component so I was unsure if it was even capable of performing accurate 1 degree stepping. Turns out it is.