Servo Board and Mega

Hi,
I have a problem connecting Mega with Servo control 32 New version 3.2 for 32 servos. I need to control 20 servos via UART. I don't know how to proceed. I have +5V, GND, RX on pin 0, TX on pin 1.
Servo shield does not communicate.
Any advice?

Thanks

As the Mega has 4 UARTs I suggest that you use Serial1, Serial2 or Serial3 instead of Serial to avoid conflict with the Serial interface

Please post your sketch and details of the servo control board that you are using

Are they standard PPM R/C servos, or servos with a serial control (UART) interface ?

The control strategy is completely different.

What ‘shield’ are you referring to ?

Servos are standard SG90.
Shield is :


New Version 3.2 for 32 servos.

As requested previously

sorry i forgot to send

#include <Servo.h>
#include <SoftwareSerial.h>
//Servo servoMotor;
Servo myservo3;
#define RX 0    // or pin 10
#define TX 1    // or pin 11 

  SoftwareSerial mySerial(RX,TX  ); // RX, TX


void setup() {
  //Serial.begin(9600);
  mySerial.begin(9600);

myservo3.attach(12);        // servo 

}

void loop() {
  // put your main code here, to run repeatedly:

for (int i=20;i<150;i++)
{
myservo3.write(i);
delay(20);
}

You don't use the servo library with that board.
You need to send commands over the serial port.

If you downloaded the software, it's all ecplained in the handbook

Well, that's news to me. I am a beginner. So far I have controlled the servos using a library.
Is this a command? for example:
#11P1500T500D500
This one is put into Serial.print ?

Thanks

I assume that this is the board https://www.aliexpress.com/i/1005001580961242.html

If so, have you visited rtrobot.org and downloaded the software ?

It contains example sketches for various boards and a manual

ok i downloaded the software.
Why are RX and TX on pins 2 and 3? Mega has pins 0 and 1.

Because the sketch uses SoftwareSerial on those pins ?

The Mega has 4 hardware UARTs so there is no need to use SoftwareSerial. Just use Serial1, Serial2 or Serial3 on their associated Tx and Rx pins and keep Serial on pins 0 and 1 for debugging using the Serial monitor and uploading the code to the Mega

I have tried several commands and the servo is starting to respond.
I just don't know how to enter a variable instead of a constant.

I think:

int P=1500;
int T=100;

SoftSerial.print("#14P(P)T(T)\r\n");

this doesn't work

Firstly, why are you still using SoftSerial ?

As to getting the value of a variable into the print command, assuming that you want to send #14 followed by the value of P then the value of T followed by Carriage Return and Linefeed, you could either print the values separately like this

Serial.print"#14");
Serial.print(P);
Serial.print(T);
Serial.print("\r\n");

or use sprintf() like this

char buffer[30]; //allow for 30 characters.  Adjust as required
sprintf(buffer,"#14%d%d\r\n",P,T);
Serial.print(buffer);

If that is not what you want to send then adjust the values and/or text to suit what you need

For a safer alternative consider using

char buffer[30]; //allow for 30 characters. Adjust as required
snprintf(buffer, sizeof(buffer), "#14%d%d\r\n",P,T);
Serial.print(buffer);

This prevents the number of characters placed in the buffer array exceeding its size

I need to control 20 servos for a robotic spider (hexapod).

Either method would work for that

Thank you, but this is complicated for me. I do not understand it at all.
Original command:

SoftSerial.print("#32P1500T100\r\n");

P- position, T- speed, 32 - servo
I don't know how to replace 32,1500 and 100 with variables.

An example

//need to print #32P1500T100\r\n

char buffer[50];  //plenty of room for the outtput string (this is NOT a String !)

int servoNum = 32;  //values to put in the string
int position = 1500;
int speed = 100;

void setup()
{
    Serial.begin(115200);
    snprintf(buffer, sizeof(buffer), "#%dP%dT%d\r\n", servoNum, position, speed);
    Serial.print(buffer);

    //let's change the values
    servoNum = 10;
    position = 1400;
    speed = 200;
    snprintf(buffer, sizeof(buffer), "#%dP%dT%d\r\n", servoNum, position, speed);
    Serial.print(buffer);
}

void loop()
{
}

thank you very much. I rewrote the code into a loop but it doesn't work.

#include <SoftwareSerial.h>

 SoftwareSerial SoftSerial(14, 15); // RX, TX

char buffer[50];  //plenty of room for the outtput string (this is NOT a String !)

int servoNum=11;  
//int position=1500;  
int speed=1500;  

void setup() {
  SoftSerial.begin(9600);
delay(100);

for (int i=500;i<=1500;i++)      
{
    snprintf(buffer, sizeof(buffer), "#%dP%dT%d\r\n", servoNum, i, speed);
    Serial.print(buffer);

delay(50);
}
}
void loop() {
}

What do you mean by this ?

  • Why are you still using SoftwareSerial particularly when pins 14 and 15 are used by Serial3 on the Mega
  • Have you tried printing the output to the Serial monitor to check it ?
  • What baud rate does the interface board ise ?
  • Are you sure that the Mega and board are connected correctly, ie Tx to Rx and vice versa and that they have a common GND ?
  • Have you tried sending a fixed string with a known value to the board and did it work ?

Control servos without SoftwareSerial? I don't know how.
So far I have only controlled 2 servos and a few motors. Always through libraries.

fixed chain - turns the servo - ok
GND - ok, TX and RX test even after crossing
Baud can be 9600 - 115200 (I think)