Arduino Example Code – Two Motors in Parallel

The N20 UART motor supports bus parallel connection, with a maximum of 4 motors on a single bus. This example demonstrates that ESP32-E uses Modbus-RTU serial port to simultaneously control two parallel-connected N20 UART motors, realizing synchronous speed adjustment, forward/reverse rotation and stop for dual motors.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR1277-Wiring Diagram

Note: A maximum of 4 motors can be connected in parallel. Due to the large instantaneous current of the motor drivers, an external 6V DC power supply is required to power the motors separately. The motors, external power supply and main controller board must share a common ground.

Sample Code

#include "DFRobot_N20SerialMotor.h"
/* -----------------------------------------------------------------------------------------------------------------------
  *    Hardware Pin Definition
  *     VCC    |              5V
  *     GND    |              GND
  *     RX     |              ESP32‑E TX pin 25
  *     TX     |              ESP32‑E RX pin 26
  * ----------------------------------------------------------------------------------------------------------------------*/
/* Serial baud rate, default 9600 for motor */
#define MOTOR1_ADDR 1
#define MOTOR2_ADDR 2
#define MOTOR_BAUD 9600
const uint8_t motorCount = 2;
// ESP32‑E uses hardware Serial1, TX=25, RX=26
DFRobot_N20SerialMotor motors[] = {
  DFRobot_N20SerialMotor(MOTOR1_ADDR, &Serial1),
  DFRobot_N20SerialMotor(MOTOR2_ADDR, &Serial1)
};
/**
 * Note: Motor address mapping
  * 1. Motor1: Address 1
  * 2. Motor2: Address 2
  *       ...:...
  * n. Motorn: Address n
  *
  * Usage:
  * 1. motors[0] corresponds to Motor1
  * 2. motors[1] corresponds to Motor2
  *       ...:...
  * n. motors[n‑1] corresponds to Motorn
  */
void setup()
{
  uint8_t i = 0;
  Serial.begin(115200);
  // ESP32‑E hardware Serial1 init: baud 9600, 8N1, TX=25, RX=26
  Serial1.begin(MOTOR_BAUD, SERIAL_8N1, 25, 26);
  delay(1000);
  Serial.println();
  Serial.println(F("========================================"));
  Serial.println(F("  DFRobot N20 Serial Motor"));
  Serial.println(F("  Dual‑motor Control Example"));
  Serial.println(F("========================================"));
  const uint8_t motorAddrs[] = { MOTOR1_ADDR, MOTOR2_ADDR };
  for (i = 0; i < motorCount; i++) {
    if (motors[i].begin() == 0) {
      Serial.print(F("[OK] Motor "));
      Serial.print(i + 1);
      Serial.print(F(" initialization complete (Modbus address: "));
      Serial.print(motorAddrs[i]);
      Serial.println(F(")"));
    } 
    else {
      Serial.print(F("[ERROR] Motor "));
      Serial.print(i + 1);
      Serial.println(F(" init failed. You can check whether the address is correct via address scan function."));
    }
  }
  Serial.println(F("All motors are ready."));
  Serial.println(F("Start dual‑motor control loop..."));
  Serial.println();
}
void loop()
{
  Serial.println(F("-> Motor1 speed 180, Motor2 speed -180"));
  motors[0].setSpeed(180);
  motors[1].setSpeed(-180);
  delay(2000);
  Serial.println(F("-> Motor1 speed -120, Motor2 speed 120"));
  motors[0].setSpeed(-120);
  motors[1].setSpeed(120);
  delay(2000);
  Serial.println(F("-> Both motors stop"));
  motors[0].setSpeed(0);
  motors[1].setSpeed(0);
  delay(2000);
  Serial.println();
}

Result

Compile and upload the code, then open the Serial Monitor (Baud rate: 115200). After both motors complete initialization, they alternately perform reverse speed adjustment and stop. Dual-motor control commands are printed in real time via serial port. The log output is shown in the figure below.

DFR1277-Result

Was this article helpful?

TOP