Skip to main content

Working with Arduino IDE

This tutorial introduces the installation and usage of the development tools, with a focus on uploading modified programs to the Servo Driver with ESP32 for secondary development based on the ST/SC series servo programs. The development tool used here is Arduino IDE. The following sections describe the installation and usage of the servo dependency libraries and the development environment in Arduino IDE.

1. Arduino Installation and Environment Setup​

1.1 What Is the Arduino IDE​

Arduino IDE (Integrated Development Environment) is an open-source code-based development platform with its own programming language and development environment. Arduino IDE provides a rich set of libraries, greatly simplifying the use of complex hardware and software platforms such as displays and sensors.

1.2 Arduino IDE Installation​

Download the Arduino IDE​

First, go to the Arduino website to download the latest version of the Arduino IDE installer. The official IDE supports downloads for different operating systems; choose according to your operating system. Here we download the Windows version. If already installed, skip to step two. The installation process is very simple; just keep clicking Next.

warning

During installation, you will be prompted to install drivers; just click Install.

Set the Arduino IDE to a Different Language​

Upon completing the initial installation, the Arduino IDE interface will be in English. To change it to a different language, navigate to “File” → “Preferences”.

In the “Language” field, select the desired language (e.g., Simplified Chinese), and click OK.

1.3 Development Environment Installation​

The main control module on the robot's driver board is ESP32, so we need to install the corresponding ESP32 board package in the Arduino IDE development environment. The steps are as follows:

  1. Open Arduino IDE, click "File" → "Preferences".

  2. Add the following link in the "Additional Boards Manager URLs" field, then click "OK" to save the settings.

    https://dl.espressif.com/dl/package_esp32_index.json

    warning

    If you need to add multiple board URLs, do not delete the ESP32 board support URL. You can add other URLs on separate lines; by default they are separated by commas.

    For example, if you need to add the ESP8266 board URL, add it on a new line as shown:

    https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json

  3. Download ESP32 Board Package Archive and extract it. In "This PC", navigate to the following path:

    C:\Users\username\AppData\Local\Arduino15

    Replace "username" with your actual Windows username. Copy the extracted "packages” folder into the "Arduino15" folder.

    Under “Arduino15/packages/esp32/hardware/esp32”, you can check that the installed ESP32 board support package version is 1.0.6.

    warning

    The ST series servo open‑source programs are written based on the ESP32 board support package version 1.0.x. Version 1.0.6 is recommended. If you use version 3.x or later, the ESP‑NOW receive callback interface may be incompatible, and you will encounter function type mismatch errors related to esp_now_recv_cb_t when uploading the program.

1.4 Dependency Library Installation​

Download the ST/SC Servo Dependency Libraries and extract them. Copy the extracted library folders to the Arduino user library directory (libraries). The default path is:

C:\Users\username\Documents\Arduino\libraries

Replace "username" with your actual Windows username.

How to check your library directory?

Open Arduino IDE, click "File" → "Preferences", and look at the "Sketchbook location". For example:

Sketchbook location:

C:\Users\ag\Documents\Arduino

Then the library directory is:

C:\Users\ag\Documents\Arduino\libraries

1.5 Upload the Program​

  1. Download the ST series servo open‑source program. After extracting it, double-click to open ServoDriverST.ino. Note that all files in this directory must be in the same folder.

  2. Click "Tools" → "Port" to see the currently available serial devices. Note the COM port on your PC; no need to click it (different computers may show different COM ports, and there may be none).

  3. Use a USB cable to connect the bus servo driver board to the computer. Click "Tools" → "Port" again, observe the newly added serial device compared with step 2, and then click the newly appeared COM (in this example, the newly appeared COM is COM7).

  4. In Arduino IDE, click "Tools" → "Board" → "ESP32" → "ESP32 Dev Module".

  5. Click "Tools". Other settings are as follows: (Partition Scheme is best set to Huge APP, PSRAM must be set to Enabled)

  6. After all settings are complete, click the upload icon in the upper‑left corner to start uploading.

    warning

    If you encounter issues during upload and need to reinstall or change the Arduino IDE version, you must completely uninstall Arduino IDE. After uninstalling the software, manually delete all contents in the folder C:\Users\username\AppData\Local\Arduino15 (hidden files need to be visible), then download and install again.

2. Advanced Configuration​

2.1 Modify the Maximum Scan ID​

To detect connected servos, the program sends Ping commands sequentially starting from ID 0 up to the set maximum ID. If the servo IDs are large or many servos are connected, you can increase the MAX_ID value. If you know the servo IDs are in a smaller range, you can decrease this value to shorten the scan time.

Just modify the following parameter in ServoDriverST.ino:

// Set the maximum ID to scan
int MAX_ID = 20;

The default MAX_ID is 20, with a maximum supported value of 253. Larger values increase scan time.

2.2 ESP-NOW Communication Configuration​

Driver boards communicate wirelessly via ESP‑NOW. The default program supports one‑to‑one control, and you can extend it to one‑to‑many or many‑to‑many communication as needed.

In ESP‑NOW communication, each device has a unique MAC address. Before establishing communication, the Leader (controller) needs to know the MAC address of the Follower (controlled device) in order to send control data to the designated device.

The driver board supports the following three operation modes:

ModeDescription
NormalNormal mode, does not participate in ESP‑NOW
LeaderController, sends control data to other devices
FollowerControlled device, receives and executes commands

When using one driver board to control another, configure one as Leader and the other as Follower.

2.2.1 Get the Follower's MAC Address​

First, connect the board that will act as the Follower. After power‑up, the board automatically obtains its own MAC address and displays it on the first line of the OLED screen. Record this MAC address; it will be needed when configuring the Leader later.

2.2.2 Set Follower Mode​

Open ServoDriver.ino and change DEFAULT_ROLE to 2:

// set the default role here.
// 0 as normal mode.
// 1 as leader, ctrl other device via ESP-NOW.
// 2 as follower, can be controled via ESP-NOW.
#define DEFAULT_ROLE 2

Compile and upload the program to the Follower board. After upload, the board will automatically enter Follower mode on every power‑up.

tip

This step is not mandatory; you can also switch the working mode manually via the Web configuration page.

2.2.3 Configure the Target MAC Address and Leader Mode​

Connect the board that will act as the Leader, open ServoDriver.ino. Change broadcastAddress[] to the Follower's MAC address, remembering to prefix each byte with 0x.

// the MAC address of the device you want to ctrl.
uint8_t broadcastAddress[] = {0x94, 0xB5, 0x55, 0x1D, 0xD1, 0x0C};
tip

The above is an example; each device has a different MAC address, so modify it according to your actual situation.

Change DEFAULT_ROLE to 1:

// set the default role here.
// 0 as normal mode.
// 1 as leader, ctrl other device via ESP-NOW.
// 2 as follower, can be controled via ESP-NOW.
#define DEFAULT_ROLE 1

Compile and upload the program to the Leader board. After upload, the Leader will automatically establish ESP‑NOW communication with the specified Follower.

After the two devices are each connected to servos with the same ID, you can refer to the Web Usage Tutorial to control the servos on the Leader board. The Leader will send the ID and position data of the currently selected servo (Active Servo) to the Follower. Upon receiving the data, the Follower will control the servo with the corresponding ID to move synchronously.

Precautions
  1. This only works in Servo Mode. If the servos are in Motor Mode, position synchronization is not possible because target angles cannot be directly controlled.

  2. It is recommended to use different Wi‑Fi names. When using multiple driver boards simultaneously, set a unique Wi‑Fi SSID for each board. Otherwise, when searching for and connecting to Wi‑Fi from phones, computers, etc., it may be difficult to distinguish between boards, leading to accidental connections. For how to modify the SSID, refer to 2.3 Wi‑Fi: AP and STA Modes.

2.3 Wi‑Fi: AP and STA Modes​

The driver board supports two Wi‑Fi operation modes: AP (Access Point) and STA (Station).

ModeDescription
APThe board creates a Wi‑Fi hotspot; phones or computers can connect directly to it to access the Web interface
STAThe board connects to an existing Wi‑Fi network and joins that local network

2.3.1 Configure AP Mode​

In AP mode, the board creates a Wi‑Fi hotspot. You can modify the hotspot name (SSID) and password in ServoDriver.ino:

// WIFI_AP settings.
const char* AP_SSID = "ESP32_DEV";
const char* AP_PWD = "12345678";

After modification, set DEFAULT_WIFI_MODE to 1:

// set the default wifi mode here.
// 1 as [AP] mode, it will not connect other wifi.
// 2 as [STA] mode, it will connect to know wifi.
#define DEFAULT_WIFI_MODE 1

Upload the program; the board will automatically create the corresponding Wi‑Fi hotspot on power‑up.

2.3.2 Configure STA Mode​

In STA mode, the board connects to a specified Wi‑Fi network. In ServoDriver.ino, enter the target Wi‑Fi name and password:

// WIFI_STA settings.
const char* STA_SSID = "OnePlus 8";
const char* STA_PWD = "40963840";

Then set DEFAULT_WIFI_MODE to 2:

// set the default wifi mode here.
// 1 as [AP] mode, it will not connect other wifi.
// 2 as [STA] mode, it will connect to know wifi.
#define DEFAULT_WIFI_MODE 2

3. Secondary Development Tutorial​

3.1 Servo Initialization​

Every program that controls a servo needs to initialize the servo. It can only be used after initialization.

#include <SCServo.h>
SMS_STS st;
void setup() {
Serial1.begin(1000000); // Serial port initialization. If using a device such as ESP32, you can also choose a custom serial port
// Serial1.begin(1000000, SERIAL_8N1, RX, TX); // Custom serial port
st.pSerial = &Serial1;
while(!Serial1) {}
}

3.2 Change the Servo ID​

In daisy-chained servos, each ID can correspond to only one servo. Otherwise, the servo feedback information cannot be obtained correctly. When changing the servo ID, try to ensure that the driver board is connected to only one servo. The ID will be permanently saved in the servo.

#include <SCServo.h>
SMS_STS st;
int ID_ChangeFrom = 1; // The original ID of the servo whose ID is to be changed. The factory default is 1
int ID_Changeto = 2; // The new ID
void setup() {
Serial1.begin(1000000);
st.pSerial = &Serial1;
while(!Serial1) {}

st.unLockEprom(ID_ChangeFrom); //Unlock EPROM-SAFE
st.writeByte(ID_ChangeFrom, SMS_STS_ID, ID_Changeto);//Change ID
st.LockEprom(ID_Changeto); // EPROM-SAFE locked
}

void loop() {
}

3.3 Ping​

Used to test if a servo is properly connected.

#include <SCServo.h>
SMS_STS st;
int TEST_ID = 3; // Servo ID to be tested
void setup()
{
Serial.begin(115200);
Serial1.begin(1000000, SERIAL_8N1, RX, TX); // Custom serial port
st.pSerial = &Serial1;
while(!Serial1) {}
}

void loop()
{
int ID = st.Ping(TEST_ID); //Ping the servo with the specified ID; return -1 if ping fails
if(ID!=-1){
Serial.print("Servo ID:");
Serial.println(ID, DEC);
delay(100);
} else {
Serial.println("Ping servo ID error!");
delay(2000);
}
}

3.4 Write Position​

Can be used to control an individual servo's rotation.

#include <SCServo.h>
SMS_STS st;
void setup()
{
Serial1.begin(1000000);
st.pSerial = &Serial1;
while(!Serial1) {}
}

void loop()
{
st.WritePos(1, 1000, 1500, 50); // Control the servo with ID 1 to rotate to position 1000 at a speed of 1500, with a start-stop acceleration of 50.
delay(754);//[(P1-P0)/V]*1000+100

st.WritePos(1, 20, 1500, 50); // Control the servo with ID 1 to rotate to position 20 at a speed of 1500, with a start-stop acceleration of 50.
delay(754);//[(P1-P0)/V]*1000+100
}

3.5 Synchronous Write​

Can be used to control multiple servos at the same time (rotating to different positions and at different speeds).

#include <SCServo.h>
SMS_STS st;

// the uart used to control servos.
// GPIO 18 - S_RXD, GPIO 19 - S_TXD, as default.
#define S_RXD 18 //Custom serial port IO. If you do not use a custom serial port, the Serial1.begin(1000000, SERIAL_8N1, S_RXD, S_TXD); below needs to be changed to Serial1.begin(1000000);
#define S_TXD 19

byte ID[2];
s16 Position[2];
u16 Speed[2];
byte ACC[2];

void setup()
{
Serial1.begin(1000000, SERIAL_8N1, S_RXD, S_TXD);
st.pSerial = &Serial1;
delay(1000);
ID[0] = 1; // Save the servo ID that needs to be controlled to the ID[].
ID[1] = 2; // Save the servo ID that needs to be controlled to the ID[].
Speed[0] = 3400; // Set the servo speed, Speed[0] corresponds to the servo of ID[0] above.
Speed[1] = 3400; // Set the servo speed, Speed[1] corresponds to the servo of ID[1] above.
ACC[0] = 50; // Set the start-stop acceleration, the smaller the number, the smaller the acceleration, and it can be set up to 150.
ACC[1] = 50;
}

void loop()
{
Position[0] = 3000; // Set the target position of servo ID[0] (which is ID 1 here), ranging from 0 to 4095
Position[1] = 3000; // Set the target position of servo ID[1] (which is ID 2 here), ranging from 0 to 4095
st.SyncWritePosEx(ID, 2, Position, Speed, ACC); //servo(ID1/ID2) speed=3400,acc=50,move to position=3000.
delay(2000);

Position[0] = 100;
Position[1] = 100;
st.SyncWritePosEx(ID, 2, Position, Speed, ACC); //servo(ID1/ID2) speed=3400,acc=50,move to position=100.
delay(2000);
}

3.6 Get Servo Information Feedback​

#define S_RXD 18
#define S_TXD 19

#include <SCServo.h>

SMS_STS sms_sts;

void setup()
{
Serial1.begin(1000000, SERIAL_8N1, S_RXD, S_TXD);
Serial.begin(115200);
sms_sts.pSerial = &Serial1;
delay(1000);
}

void loop()
{
int Pos;
int Speed;
int Load;
int Voltage;
int Temper;
int Move;
int Current;

if(sms_sts.FeedBack(1)!=-1){
Pos = sms_sts.ReadPos(-1);
Speed = sms_sts.ReadSpeed(-1);
Load = sms_sts.ReadLoad(-1);
Voltage = sms_sts.ReadVoltage(-1);
Temper = sms_sts.ReadTemper(-1);
Move = sms_sts.ReadMove(-1);
Current = sms_sts.ReadCurrent(-1);

Serial.print("Position:");
Serial.println(Pos);
Serial.print("Speed:");
Serial.println(Speed);
Serial.print("Load:");
Serial.println(Load);
Serial.print("Voltage:");
Serial.println(Voltage);
Serial.print("Temper:");
Serial.println(Temper);
Serial.print("Move:");
Serial.println(Move);
Serial.print("Current:");
Serial.println(Current);
delay(10);
} else {
Serial.println("FeedBack err");
delay(500);
}

Pos = sms_sts.ReadPos(1); // Get position feedback
if(Pos!=-1){
Serial.print("Servo position:");
Serial.println(Pos, DEC);
delay(10);
} else {
Serial.println("read position err");
delay(500);
}

Voltage = sms_sts.ReadVoltage(1); // Get voltage feedback
if(Voltage!=-1){
Serial.print("Servo Voltage:");
Serial.println(Voltage, DEC);
delay(10);
} else {
Serial.println("read Voltage err");
delay(500);
}

Temper = sms_sts.ReadTemper(1); // Get temperature feedback
if(Temper!=-1){
Serial.print("Servo temperature:");
Serial.println(Temper, DEC);
delay(10);
} else {
Serial.println("read temperature err");
delay(500);
}

Speed = sms_sts.ReadSpeed(1); // Get speed feedback
if(Speed!=-1){
Serial.print("Servo Speed:");
Serial.println(Speed, DEC);
delay(10);
} else {
Serial.println("read Speed err");
delay(500);
}

Load = sms_sts.ReadLoad(1); // Get load feedback
if(Load!=-1){
Serial.print("Servo Load:");
Serial.println(Load, DEC);
delay(10);
} else {
Serial.println("read Load err");
delay(500);
}

Current = sms_sts.ReadCurrent(1); // Get current feedback
if(Current!=-1){
Serial.print("Servo Current:");
Serial.println(Current, DEC);
delay(10);
} else {
Serial.println("read Current err");
delay(500);
}

Move = sms_sts.ReadMove(1); // Get motion status feedback
if(Move!=-1){
Serial.print("Servo Move:");
Serial.println(Move, DEC);
delay(10);
} else {
Serial.println("read Move err");
delay(500);
}

Serial.println();
}