Problem when configuring Serial port and Servos at the same time

Hi everyone,

I am working on a project building a drone where an Arduino Nano sends a message throught UART to another Arduino Nano (TX from arduino sender to RX on arduino receiver), and according to the message, I move the servos connected to the arduino receiver.

To do this, first I tried to communicate both arduinos nano throught Serial, and it works perfectly. This is the code used for this first part of the project:

First try. Arduino sender sends throught UART a message to the arduino receiver:
Codes attached: “Arduino_sender.ino” and “Arduino_receiver.ino”

Everything works perfectly here, so I tried the second part: move the 4 servos connected to my drone according to the order sent from an arduino. To do this I just used one arduino nano, 4 standard servos of 1000KV with a 30A ESC attached to each one of them, and the arduino nano is connected to the PC via USB to send the orders throught the Serial monitor. Here is the code used in this part:

Arduino move servos sending the orders throught the Serial monitor:
Code attached: SetupAndMoveMotors.ino

What I am doing here is very simple. In the setup() I configure the motors to the minimum speed, and I am able to listen the “beep,beep,beep” of all the motors. When I open the Serial monitor, I only have to write first the value of the motor I am going to move, for example “1” for motor1, and after I have to write the speed of the motor1, for example “1500”. To move another motor, for example, motor2, I only have to write again “1” to disable the orders to motor1, and I write “2” to enable the orders to motor2, after I just write again the speed of the motor2, for example “1500”, and the motor2 starts moving. By reading the code is very intuitive to understand I think.

This code also works perfectly, so I only need to merge both parts of the project to be able to do what I said at the beginning. This is the merged code:

Arduino ender sends via UART to arduino receiver a message. According to this message, the arduino receiver sends the order to the motors:
Code attached: 1_Complete_code_UART_and_motors.ino

Here is the problem. If I comment the setup of the motors in the setup(), the message is received properly, and the value is printed, although the motors are not moving because their setup is commented. If the setup of the motors are not commented, it seems that everything goes perfectly during the setup() and the motors sound correctly because of their configuration, but when I send something from the arduino sender, nothing is received.

The key point here is to comment or not the setup of the motors. If it is commented, the message is received correctly. If it is uncommented, the setup() goes ok but when a message is sent from the arduino sender, nothing happens in the arduino receiver, the code Serial.available() in the loop is always false, so the order is never sent to the motors.

What could be the problem here? I am getting crazy with this because commenting the setup of the motors in the setup() is the only way to be able to read what I am receiving throught the UART.

Has this any sense? I am not able to see what I am doing wrong here…

Any comments or help would be much appreciated!!

Thanks a lot.
Daniel.

Arduino nano emisor.ino (708 Bytes)

Arduino nano receptor.ino (1.29 KB)

SetupAndMoveMotors.ino (3.48 KB)

1_Complete_code_UART_and_motors.ino (2.28 KB)

You're not talking about servos. What you are using is DC motors (probably brushless) and ESCs. Not the same thing at all.

You have SoftwareSerial.h in there but you never use it for anything. Servo.h and SoftwareSerial.h are not compatible. If you remove SoftwareSerial it will probably start working.

Steve

slipstick: You're not talking about servos. What you are using is DC motors (probably brushless) and ESCs. Not the same thing at all.

You have SoftwareSerial.h in there but you never use it for anything. Servo.h and SoftwareSerial.h are not compatible. If you remove SoftwareSerial it will probably start working.

Steve

Thank you very much Steve. I really thought that talking about servos and DC motors was quite similar so I'll check the difference between them, (new things to learn :) ). You're right, I am using brushless motors and ESC in this case.

Let me ask you about about SoftwareSerial.h and Servo.h.. You say that are not compatible, can you explain me why or give me some references about this? I've searched a lot but I'm not able to find anything related..

If it is not compatible, what can you suggest to me to achieve something similar like what I am trying? there is not other ways to send a message to the arduino and turn on the motors according to that message?

Thank you for your help and for your time reading this quite long post :) :)

Simple servo test code that you should be able to use to verify the motor/ESC setup is operating as desired.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

zoomkat:
Simple servo test code that you should be able to use to verify the motor/ESC setup is operating as desired.

// zoomkat 7-30-10 serial servo test

// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
 Serial.begin(9600);
 myservo.attach(9);
}

void loop() {

while (Serial.available()) {

if (Serial.available() >0) {
     char c = Serial.read();  //gets one byte from serial buffer
     readString += c; //makes the string readString
     delay(3);
   }
 }

if (readString.length() >0) {
   Serial.println(readString);
   int n = readString.toInt();
   Serial.println(n);
   myservo.writeMicroseconds(n);
   //myservo.write(n);
   readString="";
 }
}

Thanks for your reply zoomkat, but I already have this. The code where I check if the motors are running as desired is already uploaded in my question, specifically the SetupAndMoveMotors.ino program.

My problem here is not to send a message throught Serial to move the motors. My problem here is to receive the order throught UART from the arduino sender to the arduino receiver and move the motors according to this information.

As slipstick says here:

slipstick:
You’re not talking about servos. What you are using is DC motors (probably brushless) and ESCs. Not the same thing at all.

You have SoftwareSerial.h in there but you never use it for anything. Servo.h and SoftwareSerial.h are not compatible. If you remove SoftwareSerial it will probably start working.

Steve

the problem is that SoftwareSerial.h and Servo.h is not compatible. I don’t know why this could be not compatible and how to achieve my purpose doing something similar.

Anyway, despite of SoftwareSerial.h and Servo.h are not compatible, if I’m not using SoftwareSerial.h, why is this happening? Why just commenting the setup lines of the motors, the message received from the arduino sender if correctly received in the arduino received and printed but uncommenting these lines Serial.available() is not turning to true when a mmesage is received?

You both think that just deleting the lines corresponding to the SoftwareSerial.h, the message will be properly received from the arduino sender throught UART and move the motors? It is not necessary the SoftwareSerial.h to setup the TX and RX pins of the arduino receiver to receive the message? I’ll do this test when I get home today anyway :slight_smile:

Servo.h and SoftwareSerial.h both use the same timer which is what causes problems.

But my point is that you are sending and receiving messages through Serial (the built-in hardware Serial on pins 0 and 1). You are not doing anything at all with mySerial which is SoftwareSerial on pins 30 and 31.

Just comment out all references to SoftwareSerial.h and mySerial and your program should still work.

Note: these comments are based on the program 1_Complete_code_UART_and_motors.ino you posted in the original message. If that's not what you're using then you're on your own.

Steve

slipstick: Servo.h and SoftwareSerial.h both use the same timer which is what causes problems.

But my point is that you are sending and receiving messages through Serial (the built-in hardware Serial on pins 0 and 1). You are not doing anything at all with mySerial which is SoftwareSerial on pins 30 and 31.

Just comment out all references to SoftwareSerial.h and mySerial and your program should still work.

Note: these comments are based on the program 1_Complete_code_UART_and_motors.ino you posted in the original message. If that's not what you're using then you're on your own.

Steve

Great explanation slipstick, thanks a lot! I didn't figure out that both SoftwareSerial.h and Servo.h uses the same timer! And you're right, I'm working on the 1_Complete_code_UART_and_motors.ino program :)

Let me ask you, if you don't mind, just two things about this. First, about the timers, I've read something related with change the timers programmatically on the PWMs or something like that.. probably I'm wrong with this but, could be possible, programmatically, to change the timer that one of them is using to another timer of the arduino nano? Just to know..

And about deleting all SoftwareSerial references, I'll do it at the moment I get home and will let you know if that change works or not. Now I have connected the TX pin of the arduino sender to the RX pin of the arduino receiver. I thought that I need to set up the SoftwareSerial to let the arduino receiver know that the information of the arduino sender is going throught that pin, it is not neccesary? I mean, do I need to change the wiring from the arduino sender to the arduino receiver, or it is actually well connected and it is just a matter of code?

Thanks so much for your explanation again!

Daniel.

If you're using a Nano then you're definitely NOT using SoftwareSerial because your code says it's connected to pins 30 and 31 and there are no such pins on a Nano. The pins marked TXD and RXD are pins 1 and 0 and if they the ones you have connected then you're fine. Just try it!

To change the timers that library code uses you would need to dig about inside the library C++ code and be very careful because not all timers can be used for everything. I think that's probably a bit much for you just now, maybe later when you've learned a bit more. If you really get stuck then someone has already done it for Servo and you can use an alternative called ServoTimer2.h...but you shouldn't need it.

Steve

slipstick: If you're using a Nano then you're definitely NOT using SoftwareSerial because your code says it's connected to pins 30 and 31 and there are no such pins on a Nano. The pins marked TXD and RXD are pins 1 and 0 and if they the ones you have connected then you're fine. Just try it!

To change the timers that library code uses you would need to dig about inside the library C++ code and be very careful because not all timers can be used for everything. I think that's probably a bit much for you just now, maybe later when you've learned a bit more. If you really get stuck then someone has already done it for Servo and you can use an alternative called ServoTimer2.h...but you shouldn't need it.

Steve

slipstick you're absolutely right! By commenting all references to the SoftwareSerial library in the code, the motors started t move when I send the order from the arduino sender. Thank you very much for you time and patience!

Let me ask you one last question. You said this before: "Servo.h and SoftwareSerial.h both use the same timer which is what causes problems."

Where did you read this? Can you give me some references or links where this is explained? I'm very curious about this and reading about it for sure will make me not to fall in this type of problems.

Once again, thank you very much! Finally its working :) :) :)

Daniel.

"Where did you read this? Can you give me some references or links where this is explained? I'm very curious about this and reading about it for sure will make me not to fall in this type of problems."

If you use the forum search feature in the upper right of this page and search for "SoftwareSerial servo", you will probably find many discussions concerning this issue over many years.

zoomkat: "Where did you read this? Can you give me some references or links where this is explained? I'm very curious about this and reading about it for sure will make me not to fall in this type of problems."

If you use the forum search feature in the upper right of this page and search for "SoftwareSerial servo", you will probably find many discussions concerning this issue over many years.

Yes, you're right zoomkat, I was thinking about some official documentation where they explain this issue, but I think with the discussions about this is enough to start to understand this.

Thank you very much!

Daniel.

As zoomkat says you have to rely on this forum or perhaps Google. Since libraries are mainly produced by volunteers and therefore have very variable standards of documentation and there are thousands of them there will never be anything official that describes all possible problems between them.

It's just a good job that there are many people round here who are willing to investigate problems and document them for the rest of us.

Steve