Unable to communicate with Arduino Uno using RS 485 to TTL module

Hi there,

I am trying to communicate with this, amplifier WDT 11, using RS485 and Arduino Uno but it doesn't work.
I have gone through multiple examples but i can't get this to work.

Any help will be greatly apprieciated!!!@!@

Connection setup:
WDT11 outputs:
A -> A on RS 485
B -> B on RS 485

RS 485:
B-> as above
A-> as above
VCC -> 5V on Arduino Uno
GND -> GND on Arduino Uno

DI -> TX on Arduino Uno
DE: -> 3
RE: -> 3
RO: -> RX on Arduino Uno.

Code

#include <ModbusMaster.h>

#define MAX485_DE      3

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{

  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{

  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode

  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200);

  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
  uint8_t result;
  
  // Read 16 registers starting at 0x3100)
  result = node.readInputRegisters(0x03, 1);
  if (result == node.ku8MBSuccess)
  {
    Serial.print(node.getResponseBuffer(0x00));
    Serial.println();
 
  }

  delay(1000); 
}

WDT 11 datasheet: Sorry only in PL: https://wobit.com.pl/download/wdt11_instrukcja.pdf.
baudrate 115200, 8 bits, 1 stop bit, no parity.

What i would like to do with my code is to extract data from register 0x03 under adress 0. Thats all.

Could someone more experienced take a look at this and make some suggestions?
I have visited plenty of other tutorials but no joy.

how does that fit togeter:

What i would like to do with my code is to extract data from register 0x03 under adress 0

and

  // Modbus slave ID 1
  node.begin(1, Serial);

Thanks for having a look at that, but please be more precise. I am not an expert and i don't understand.

you have written you want to read from Device 0, but in the code you are addressing address 1. 0 and 1 don't fit together. If your device Address ist really 0, you should use 0 as address in the code to get response from your device 0.

clear enough?

I noticed a mistake that i made in my code. Register adress that i want to read is 0.
Device ID: 1, and it is written that way in the code.

DE and RE are now connected to pin 7..

Hopefully it is all clear now.

Here is the updated code

#include <ModbusMaster.h>


#define MAX485_DE      7

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
 
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200, SERIAL_8N1);

  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  //uint16_t data[6];
  
  // Toggle the coil at address 0x0002 (Manual Load Control)
  //result = node.writeSingleCoil(0x0002, state);
 // state = !state;

  // Read 16 registers starting at 0x3100)
  result = node.readHoldingRegisters(0x00, 1);
  
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Vbatt: ");
    Serial.println(node.getResponseBuffer(0x00));

  }

  delay(1000);
}

Code is not working, message that i am gettin is like: three squares followed by 1

THANKS A LOT FOR TRYING TO HELP!@!!@

for me it looks like as you want to use the modbus communication on Serial and do debug messages on Serial.
This will not work.
Which example are you following? Please link to the project or to the source you are using.

I have been using these two: TUTORIAL: How To Use RS-485 TTL MODBUS - Arduino Controller Module (Part 2/2 - Wire Up) Solar - YouTube
Error in communication: Modbus RTU and Arduino - Networking, Protocols, and Devices - Arduino Forum

Guy in the forum had almost the same problem, but he figured it out and i can't

so much trash out there but this comment is true:

ernando Tanco
vor 9 Monaten
@Antony Cartwright You are using the SAME port for debugging and modbus communication!!!!! The Arduino serial terminal y receiving the modbus packet and shows that boxes messages. And... the EPSolar 4215BN is receiving the Serial.print commands…. To solve this you shoud use another serial port for transmit and receive modbus. Arduino nano has got only 1 hardware serial port, you shoud use a "software serial port", like SoftwareSerial.h

Do you understand that?

And provide more information:
Which controller are you using?
post a schematic how you have wired everything.
show a clear picture of your setup or as many as necessary clearly each wire?

yes i think i got it. For proper communication between these 3 elements i need 2 serial ports while UNO has only one.

How these guys managed to make it all happen with using one serial port?

RS 485: https://core-electronics.com.au/ttl-uart-to-rs485-converter-module.html

Pictures are attached: raw schematic and real, test, setup.

If you are limited to the existing UNO, I propose you read about Soft Serial and try to connect Modbus on a soft serial connection. that should work. I hope you also find some example sketches for this library using soft serial...

by the way, nice schematic, proper images ... well done.

Thanks a lot for spending time on checking my setup @@@

I managed to make new sketch with use of Soft Serial but still no joy.

It seems to me that void loop doesn't work at all.

Should you have any ideas, please share :slight_smile:

Code using SoftSerial is down below (based on post #4 from here Help regarding MODBUS MAX485 code - Networking, Protocols, and Devices - Arduino Forum):

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

#define MAX485_TX_ENABLE  7  // EIA-485 transmit control pin
#define ENABLE485_RX         2  // EIA-485 serial receive pin
#define ENABLE485_TX        3 // EIA-485 serial transmit pin

ModbusMaster node;
SoftwareSerial RS485Serial(ENABLE485_RX, ENABLE485_TX); // RX, TX

void preTransmission() {
  digitalWrite(MAX485_TX_ENABLE, 1);
}

void postTransmission() {
  digitalWrite(MAX485_TX_ENABLE, 0);
}

void setup() {
  pinMode(MAX485_TX_ENABLE, OUTPUT);
  digitalWrite(MAX485_TX_ENABLE, 0);

  Serial.begin(115200);
  RS485Serial.begin(115200);

  node.begin(1, RS485Serial);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

  Serial.print("asd");
}


void loop() {
  uint8_t result;

  result = node.readHoldingRegisters(0x00, 1);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Force: "); 
    Serial.println(node.getResponseBuffer(0x00));
    
  }
  else {
  Serial.print("Error getting data\n");
  }

  delay(1000);
}

check the soft serial documentation - I assume 115200 ist much to fast. Bring it down to 9600 (and your device) or consider to use a Mega which provides 4 hardware serials.

I've just ordered Arduino Mega in order to get rid of any problems with multiple serials and RS 485 just to make sure the one that i am using is working properly.

Thanks a lot for your help and please visit this topic every now and then :), just in case I need some help with Arduino Mega :slight_smile:

Finally @!!@@!

Everything is working.

I replaced Arduino Uno with Arduino Mega and RS458 with new one RS485.

Working code is down below. Hopefully someone can find this helpful.

BIG thanks to noiasca for helping me out!.

#include <ModbusMaster.h>


#define MAX485_DE      3

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_DE, 0);
}

void setup()
{

  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200);

  Serial1.begin(38400);
  
  // Modbus slave ID 1
  node.begin(1, Serial1);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  
   result = node.readHoldingRegisters(0x0000, 10);

    //digitalWrite(MAX485_DE, 0);
     
    Serial.println(node.getResponseBuffer(0x00)/1.00);

  delay(500);
}
1 Like

thumbs up!