I am working on a project on image tracking on MATLAB, by moving the mounted camera on a servo motor controlled by the Arduino Uno board, the data is serially sent to the arduino using USB according to which the servo motor changes its angle, but there a problem at start the motor initializes to its RESET position that is 0 deg, but after passing an angle serially its does not move back to RESET position when I send 0 as a angle again.
#include <Servo.h>
Servo myservo;
int incomingByte=0;
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
if(Serial.available()>0){
incomingByte=Serial.read();
Serial.println(incomingByte,DEC);
Serial.write("readvalue");
myservo.write(incomingByte);
}
}
It also doesn't response to some of the angles which are sent serially. I am using a SG90 servo motor, here is its datasheet.
Thanks.
Best Answer
I tested your code on an Arduino Pro Mini with a Towerpro SG90 servo, and it worked perfectly. Therefore I suspect you have a fault in the wiring or power supply.
The SG90 is a small servo but quite power-hungry - even without a load it draws up to 0.75A. For my test I used a separate 4.8V battery to power the servo, ensuring that the Arduino would not be disturbed by current surges or voltage drop when the servo is operating.
If using a separate power supply doesn't help then you may have flaky wiring or a faulty servo.