Hi,
I am a newbie to Modbus and RS-485.
I am trying to setup a connection between an Arduino Nano and an USB port at the PC.
The Arduino Nano shall act as slave, the PC as master (for testing).
I tried several Modbus Master Simulation tools, with all possible connection config options, but I always get a timeout. I cannot figure out if this is a problem with the wires, of if the modbus sketch or the configuration is wrong.
The first file in the attachment is showing how I wired the components. The arduino is powered via the USB port.
Red LED at MAX485 adapter is always on.
The other file contains the demo sketch I am using.
I am using "Simple Modbus lib" (GitHub - angeloc/simplemodbusng: Modbus RTU Slave/Master for the Arduino Platform).
I tried a few other libs, but also without success.
On the PC (Master) site, I tried a few different Modbus Tester/ Master Simulation tools and tried to read the holding register.
I get the timeout error everytime, see screenshot.
It would be great if you could point me in the right direction.
Regards, Chris
SimpleModbusSlaveExample.ino (4.33 KB)
Hello,
RS485 operates in half-duplex mode, so you need to control the data flow,

Or use RS422,
You can test the system with the help of a serial terminal program, and a test program running on Arduino,
You can for example use the example program, serial event, and have the led (D13) toggle the state when the Arduino receives a character.
In the same way, you can program the Arduino to send data, then in the terminal you can read this data.
The secret is the control pin of the MAX485 (or SN75176)
SerialEvent alternating LED on D13:
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(13, OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
digitalWrite(13, !digitalRead(13));
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Arduino sending data:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(500); // delay in between reads for stability
}
rtek1000, thanks for your thoughts & code.
It helped me to figure out that one cable was broken.
Regards,
Chris