Arduino MEGA, RS485, NEMA Stepper Motor, UMC1BDS32

Hi! I am completely new to this field and wanted advice on my setup. I have a motor controller with an RS-485 Interface that I'm hoping to connect to an Arduino Mega to control the speed of the motor. Also, I have two motors that run alternatively, so I have two motor drivers and two RS-485s. I attached my circuit and code below, but I am not too sure if it's correct. If anyone can check it out and lmk that would be great!

Code so far:

/*
  Dual UMC1BDS32 Motor Control via RS485 (Modbus RTU)
  ---------------------------------------------------
  Hardware setup (Arduino Mega):
    - MAX485 #1 → Motor 1 (UMC #1) using Serial1 (TX1=18, RX1=19)
    - MAX485 #2 → Motor 2 (UMC #2) using Serial2 (TX2=16, RX2=17)
    - Each MAX485 has its own DE+RE control pin
    - STO bypassed (J502) on both UMC boards
    - Each UMC powered separately (12–48 V)
  
  Serial Monitor @ 9600 baud:
    Type:
      M1 <Enter>   → select Motor 1
      M2 <Enter>   → select Motor 2
      <number>     → set RPM (e.g. 50, -50, or 0 to stop)
*/

#include <ModbusMaster.h>

// === RS-485 driver enable pins ===
#define MAX485_1_DE_RE 4   // for Motor 1 (Serial1)
#define MAX485_2_DE_RE 5   // for Motor 2 (Serial2)

// Create Modbus objects for each motor
ModbusMaster motor1;
ModbusMaster motor2;

int selectedMotor = 1; // Start with motor 1

// RS-485 control functions for Motor 1
void preTransmission1()  { digitalWrite(MAX485_1_DE_RE, HIGH); }
void postTransmission1() { digitalWrite(MAX485_1_DE_RE, LOW);  }

// RS-485 control functions for Motor 2
void preTransmission2()  { digitalWrite(MAX485_2_DE_RE, HIGH); }
void postTransmission2() { digitalWrite(MAX485_2_DE_RE, LOW);  }

void setup() {
  pinMode(MAX485_1_DE_RE, OUTPUT);
  pinMode(MAX485_2_DE_RE, OUTPUT);
  digitalWrite(MAX485_1_DE_RE, LOW);
  digitalWrite(MAX485_2_DE_RE, LOW);

  Serial.begin(9600);
  Serial.println("=== Dual UMC Motor Control (Mega + Dual RS485) ===");
  Serial.println("Commands:");
  Serial.println("  M1 → select Motor 1");
  Serial.println("  M2 → select Motor 2");
  Serial.println("  [number] → set RPM (e.g. 50, -50, 0)");
  Serial.println("------------------------------------------");

  // Initialize both serial ports for RS485
  Serial1.begin(9600);  // MAX485 #1 (Motor 1)
  Serial2.begin(9600);  // MAX485 #2 (Motor 2)

  // Assign Modbus ports
  motor1.begin(1, Serial1);  // ID can be 1 for both, since separate lines
  motor2.begin(1, Serial2);
  motor1.preTransmission(preTransmission1);
  motor1.postTransmission(postTransmission1);
  motor2.preTransmission(preTransmission2);
  motor2.postTransmission(postTransmission2);

  delay(300);

  // Set both drives to velocity mode
  motor1.writeSingleRegister(0x6060, 2);
  motor2.writeSingleRegister(0x6060, 2);

  // Disable both initially
  motor1.writeSingleRegister(0x6040, 0x0000);
  motor2.writeSingleRegister(0x6040, 0x0000);
}

void enableMotor(ModbusMaster &m) {
  m.writeSingleRegister(0x6040, 0x0006); delay(20);
  m.writeSingleRegister(0x6040, 0x0007); delay(20);
}

void disableMotor(ModbusMaster &m) {
  m.writeSingleRegister(0x6040, 0x0000);
  m.writeSingleRegister(0x6042, 0); // stop motion
}

void setSpeed(ModbusMaster &m, int rpm) {
  int targetValue = rpm * 10; // adjust scaling factor if needed
  m.writeSingleRegister(0x6042, targetValue);
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    cmd.toUpperCase();

    if (cmd == "M1") {
      selectedMotor = 1;
      disableMotor(motor2);
      enableMotor(motor1);
      Serial.println("✅ Motor 1 selected and enabled.");
    } 
    else if (cmd == "M2") {
      selectedMotor = 2;
      disableMotor(motor1);
      enableMotor(motor2);
      Serial.println("✅ Motor 2 selected and enabled.");
    } 
    else {
      int rpm = cmd.toInt();
      if (selectedMotor == 1) {
        disableMotor(motor2);
        enableMotor(motor1);
        setSpeed(motor1, rpm);
        Serial.print("Motor 1 → "); Serial.print(rpm); Serial.println(" RPM");
      } else {
        disableMotor(motor1);
        enableMotor(motor2);
        setSpeed(motor2, rpm);
        Serial.print("Motor 2 → "); Serial.print(rpm); Serial.println(" RPM");
      }
    }
  }

  delay(100);
}

It doesn't look correct. The signals from the Mega to the RS485 interface must have a GND, not only 5 volt.

Please post a link to the datasheet of the stepper driver. Likely it use separate grounds for signal and for motor power.

Please complete the schematic. Show all power sources.

Post links to technical information on these devices, there are many to chose from, and I will probably guess the wrong ones.

Hi! Here’s the link to the driver: https://cdn.robotshop.com/media/u/usw/rbc-usw-06/pdf/umc1bds32_uds1001c.pdf?_gl=1*6qy8d7*_gcl_au*MTUwNjIyOTg3MC4xNzYxMTczMDU4*_ga*MTY5MTA5NjIzNi4xNzYxMTczMDU4*_ga_FXHVC4EYRV*czE3NjEyODEyMDYkbzIkZzAkdDE3NjEyODEyMDYkajYwJGwwJGgw

Hi! Here’s the link to the driver: https://cdn.robotshop.com/media/u/usw/rbc-usw-06/pdf/umc1bds32_uds1001c.pdf?_gl=1*6qy8d7*_gcl_au*MTUwNjIyOTg3MC4xNzYxMTczMDU4*_ga*MTY5MTA5NjIzNi4xNzYxMTczMDU4*_ga_FXHVC4EYRV*czE3NjEyODEyMDYkbzIkZzAkdDE3NjEyODEyMDYkajYwJGwwJGgw

I’m using a MAX485 RS485 module for communication.

According to your wiring image it's not connected to mega 4/5/GND pins

I do not have a clue to which one you are using. I found this and thought I should share it: Connecting the DE (Driver Enable) pin to the RE (Receiver Enable) pin on an RS-485 module is not recommended, as it can cause conflicts in the operation of the transceiver. Each pin serves a different function: DE enables transmission while RE enables reception, and they should be controlled separately for proper communication.

It might be easier to run CAN in place of RS385, two wires will connect all the motor controllers. It will receive and send messages without direct involvement of the processor. It is great for automotive and industrial environments.