Controlling servos using serial commands

I’m looking to control the position of some servo’s using serial communication between my laptop and an arduino Mega 2560.

My code is shown below:

#include <Servo.h> 
Servo serv; // define latching mechanism servo
Servo act; // define latching mechanism linear act

char incomingByte = 0;
int numBytes;

// the setup routine runs once when you press reset:
void setup() 
void loop() { 
  if(Serial.available() > 0 ) { // wait for something on the serial port.
// get incoming byte:
// P - Lift latch
// L - Lower Latch
// O - Open Latch
// K - Close Latch

incomingByte =; // read the incoming byte
    if(incomingByte == 'p'){
    if(incomingByte == 'l'){
    if(incomingByte == 'o'){
    if(incomingByte == 'k'){

I’m using the Arduino Serial Monitor to send the characters for commands and the Arduino is receiving the commands because the RX light is flashing, but the servo won’t move to the described positions.

Any ideas/suggestions with my code?

Do you see the characters being echoed back to the terminal?

How are the servos powered? Hopefully not from the Arduino - it’s marginal for one, let alone two.

Is anything else using the same RX/TX pins (you should keep those reserved for serial comms) ?

Thanks for the quick response.

At first the characters we're not showing up in the serial monitor, however, after unplugging everything and trying my set-up and code again, everything worked.

And the servo's were powered by the Arduino, they are small hobby servo's that run on 4.8v or 6v. Eventually once the project is complete I will be running off external power.

Servo test code.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
          Serial.print("writing Microseconds: ");
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
          Serial.print("writing Angle: ");
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
         readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString

And the servo’s were powered by the Arduino, they are small hobby servo’s that run on 4.8v or 6v. Eventually once the project is complete I will be running off external power.

Were?.. not any more?- do you have external power now, do you mean?

It’s not the voltage that’s an issue, it’s the current. Even if you have no mechanical load on them, those servos could be drawing 100mA or so each, up to say 500mA if you load them… it’s risking damaging your Arduino, for sure.

As a rule of thumb, any servo “funnies” are best treated by making sure power related problems are off the table by having an external supply, even if it’s just a 4xAA pack for testing. Sometimes the problem goes away when you provide external power; if it doesn’t, well at least you’ve eliminated one possible cause.