Uncontrollable Servo Motors

I'm trying to control two Tower Pro MG996R servo motors to so that I can send an X,Y coordinate to point a laser at that location. I had the code and hardware working in conjunction with Processing. Something has gone wrong and now the servo motors go haywire and try to break themselves. I've tried every debugging method I can and have not come any closer to a solution. I've attached the Arduino code below if anyone can spot any issues with it. I'm running it with a 7V 1A supply common ground using an Arduino nano.

#include <Servo.h>

Servo servoX; // create servo object for X-axis
Servo servoY; // create servo object for Y-axis

int posx; // initialize variable for X-axis servo position
int posy ; // initialize variable for Y-axis servo position
int ledPin = 4; //define the LED pin the LED can be replaced with the laser, same layout in bread board too
int servoXPin = 6;
int servoYPin = 5;
int ledval; //stores LED command from serial port

void setup() {

pinMode(ledPin, OUTPUT); //initialize LED output
Serial.begin(9600);
servoX.attach(servoXPin);
servoY.attach(servoYPin); // attaches the servo on pin 6 & 5 to the servo object

posx = 0; // initialize variable for X-axis servo position
posy = 0; // initialize variable for Y-axis servo position]

//Reset their location
servoX.write(0);
servoY.write(0);

}

void loop() {

if (Serial.available() > 2) { //if some data is available of in the serial port

// Reads in x-direaction position value from serial
posx = Serial.parseInt();


// The motor can only go from 0 to 180 and at these boundaries it continues to try move so to prevent it map the values to 3 and 179
// map(value_you_want_to_map, Lower_value_to_be_mapped, Upper_value_to_be_mapped, desired_lower_value, desired_upper_value)
posx = map(posx, 0, 255, 3, 179);
//    write position to servo
servoX.write(posx);

// Reads in Y-direaction position value from serial
posy = Serial.parseInt();

// as the 0 on the y motor arm is too high up still we are using the reverse side of it, hence 90 to 160
// at 90 the arm is pointing towards the sky and and at 160 the arm is pointing down to the ground
posy = map(posy, 0, 255, 90, 160);
//    write position to servo
servoY.write(posy);

// reading in LED value and turnign it on or off depending on input
// this is where you can control the laser turning on and off
ledval = Serial.parseInt();
if (ledval == 1) {
  digitalWrite(ledPin, HIGH);        //turn ON the LED
}
else {
  digitalWrite(ledPin, LOW);
}

//Uncomment the following lines if you are using the serial monitor for testing to see if the values are being recieved and mapped correctly 
//    Serial.println(posx);
//    Serial.println(posy);
//    Serial.println(ledval);

// delay to get the motors time to get into position, lower this if you want it changing positions faster
delay(2000);

}

// turnign the LED off again after 2 seconds, REMOVE THIS IF YOU WANT TO KEPP THE LASER ON ALWAYS
digitalWrite(ledPin, LOW);

}

Have you connected the grounds?

Yes, I have common grounds for everything, two servos, arduino and power supply are all connected.

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.

BTW

if (Serial.available() > 2)
Change this to:
if (Serial.available() > 0)
and change the timeout to what’s needed.

There are a few things that could be causing the issue with the servo motors going haywire:

  1. Delay: There is a delay of 2 seconds at the end of the loop, which means that the servo motors will not update their position for that amount of time. This delay can be removed or reduced to improve the responsiveness of the system.
  2. Serial communication: The code is relying on serial communication to receive the X,Y coordinates and LED commands from Processing. If there is a problem with the serial communication, or if the data is not being sent correctly, this could cause the servo motors to move unexpectedly. You could try adding some error handling code to check for valid input and to handle any communication errors.
  3. Servo power: The servo motors require a significant amount of power to operate correctly, and if they are not receiving enough power, this could cause them to behave unpredictably. Make sure that your power supply is providing enough current to drive both servo motors.
  4. Servo wiring: Make sure that the wiring for the servo motors is correct and that they are connected to the correct pins on the Arduino. If the wiring is incorrect, this could cause the servo motors to move unexpectedly.
  5. Servo calibration: It is possible that the servo motors are not calibrated correctly, which could cause them to move unpredictably. Try resetting the servo motors to their default position and recalibrating them.

Overall, there are several potential causes for the issue with the servo motors, and it may take some trial and error to identify the root cause.

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