MAX485 to Arduino ATmega2560

Hello There
im using max485 and Arduino ATmega2560 and a battery BMS board connected to a huge battery(TP-BMS48100-LT-41_V1)
the A and B come from Rs485 socket on BMS board from a lan cable , unwired and connected to A,B on MAX485
the DE and RE together on breadboard together and wired them to pin pwm 3 now (tried on digital pin, sepeprated them from each and connected them to digital pin and it didnt work, just like now)
connected the Ro to Rx and Di to Tx0(tried Rx1 and Tx1 and 2 did not work)
gnd to gnd on arduino and Vcc to 5v this mmax485 module is receiving data from battery bms which i tried with a usb to rs485 converter(UT-890A USB to RS485 RS422 Converter & USB 2.0 - UOTEK) and showed me data in modboss app but now my arduino does not read any data at all
this is the code :expressionless:

#include <ModbusMaster.h>

// Define RS-485 control pin
#define DE_RE_PIN 3  // DE/RE pin connected to pin 3 (PWM)

// Create ModbusMaster object
ModbusMaster node;

// RS-485 Transmission Control
void preTransmission() {
  digitalWrite(DE_RE_PIN, HIGH); // Enable transmission
  delayMicroseconds(100);        // Allow time for RS-485 transceiver to settle
  Serial.println("DE/RE Pin: HIGH (Transmit Mode)");
}

void postTransmission() {
  delayMicroseconds(100);        // Allow time for RS-485 transceiver to settle
  digitalWrite(DE_RE_PIN, LOW);  // Enable reception
  Serial.println("DE/RE Pin: LOW (Receive Mode)");
}

// Function to check if the RS-485 device is connected
bool isDeviceConnected() {
  // Attempt to read a single register (e.g., register 4096)
  uint8_t result = node.readInputRegisters(4096, 1);

  if (result == node.ku8MBSuccess) {
    Serial.println("Device is connected and responding.");
    return true;
  } else {
    Serial.print("Device not responding. Modbus error: 0x");
    if (result < 16) Serial.print("0"); // Pad single-digit hex values with a leading zero
    Serial.println(result, HEX);
    return false;
  }
}

void setup() {
  pinMode(DE_RE_PIN, OUTPUT);
  digitalWrite(DE_RE_PIN, LOW); // Set to receive mode

  Serial.begin(9600); // Baud rate for RS-485 (using hardware serial)

  // Initialize Modbus communication
  node.begin(1, Serial); // Slave ID = 1
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

  Serial.println("Modbus Communication Initialized on Serial (RX0/TX0)");

  // Check if the device is connected
  if (!isDeviceConnected()) {
    Serial.println("Error: No device connected or device not responding.");
    while (1); // Halt the program if no device is connected
  }
}

void loop() {
  uint8_t result;
  uint16_t data[15];

  // Read 15 registers from address 4096
  result = node.readInputRegisters(4096, 15);

  if (result == node.ku8MBSuccess) {
    Serial.println("Modbus data received:");
    Serial.println("Raw Payload (Decimal):");

    for (int i = 0; i < 15; i++) {
      data[i] = node.getResponseBuffer(i);
      Serial.print(data[i]);
      if (i < 14) Serial.print(", ");
    }
    Serial.println();

    // Decoded values
    Serial.print("Pack Voltage (V): "); Serial.println(data[0] / 100.0, 2);
    Serial.print("Current Pack (A): "); Serial.println(data[1] / 100.0, 2);
    Serial.print("Remaining Capacity (Ah): "); Serial.println(data[2] / 100.0, 2);
    Serial.print("Average Cell Temp (C): "); Serial.println(data[3] / 10.0, 1);
    Serial.print("Environment Temp (C): "); Serial.println(data[4] / 10.0, 1);
    Serial.print("SOC (%): "); Serial.println(data[8] / 10.0, 1);
    Serial.print("SOH (%): "); Serial.println(data[9] / 10.0, 1);
    Serial.print("Full Charge Capacity (Ah): "); Serial.println(data[10] / 100.0, 2);
    Serial.print("Cycle Count: "); Serial.println(data[11]);
    Serial.print("Max Charging Current (A): "); Serial.println(data[12] / 100.0, 2);
    Serial.print("Max Cell Voltage (V): "); Serial.println(data[13] / 1000.0, 3);
    Serial.print("Min Cell Voltage (V): "); Serial.println(data[14] / 1000.0, 3);
    Serial.println("-------------------------");

  } else {
    Serial.print("Modbus error: 0x");
    if (result < 16) Serial.print("0"); // Pad single-digit hex values with a leading zero
    Serial.println(result, HEX);

    // Print additional debugging information
    Serial.print("Last error: ");
    Serial.println(node.getResponseBuffer(0), HEX);

    // Retry mechanism
    static uint8_t retryCount = 0;
    retryCount++;
    if (retryCount >= 3) {
      Serial.println("Max retries reached. Halting program.");
      while (1); // Halt the program after max retries
    } else {
      Serial.println("Retrying...");
      delay(1000); // Wait 1 second before retrying
      return; // Skip the rest of the loop and retry
    }
  }

  delay(500); // Wait 500 ms before next poll
}```

First off don't use Serial because that is used for Serial Monitor.

Error 0xE2 usually means there was something wrong with the comms setup. You need to have the right baud rate, framing (e.g. 8N1) and slave address. You should be able to copy those from your PC app.

the baud rate is 9600 and i made sure of it from time to time, slave id is set to 1

AND E2 comes in rarely , mostly here i receive E0 and i dont know how to make it right

It would be easier to read your post if you fix the code tags

E0 means invalid slave address
E2 means time out

1 Like

and this is the only resault i receive odbus Communication Initialized on Serial1 (RX1/TX1)
DE/RE Pin: HIGH (Transmit Mode)
DE/RE Pin: LOW (Receive Mode)
Device not responding. Modbus error: 0xE0
Error: No device connected or device not responding.
Modbus Communication Initialized on Serial1 (RX1/TX1)
DE/RE Pin: HIGH (Transmit Mode)
DE/RE Pin: LOW (Receive Mode)
Device not responding. Modbus error: 0xE0
Error: No device connected or device not responding.
Modbus Communication Initialized on Serial1 (RX1/TX1)
DE/RE Pin: HIGH (Transmit Mode)
DE/RE Pin: LOW (Receive Mode)
Device not responding. Modbus error: 0xE2
Error: No device connected or device not responding.

this is not working, and it got me stuck, anyone got a insight to help or perhaps lead me ?

this is the kinda data im looking to receive
DE/RE Pin: HIGH (Transmit Mode)
DE/RE Pin: LOW (Receive Mode)
Device is connected and responding.
Modbus data received:
Raw Payload (Decimal):
1234, 5678, 9101, 1121, 3141, 5161, 7181, 9202, 1222, 3242, 5262, 7282, 9303, 1323, 3343
Pack Voltage (V): 12.34
Current Pack (A): 56.78
Remaining Capacity (Ah): 91.01
Average Cell Temp (C): 11.2
Environment Temp (C): 31.4
SOC (%): 92.0
SOH (%): 12.2
Full Charge Capacity (Ah): 32.42
Cycle Count: 5262
Max Charging Current (A): 72.82
Max Cell Voltage (V): 0.930
Min Cell Voltage (V): 0.132

i know that , i dont know how to fix this

…and you haven’t figured out how to use the forum either.

Thanks for adding code tags in the edited first post.

is there guide on how to use forum?
i checked several similar cases that did not find any solution either, and then created this topic here, visually they look the same to me, perhaps focus more on the task that i failing to solve rather going sideways , thank you

So reconnect with that app and observe what parameters and slave address is used there for successful communication.

modbus reads data finally , the trouble was for the wrong timing and delays, thanks everyone