Arduino Nano as a Modbus RTU Slave

Hello everyone,

Having trouble connecting Arduino Nano (ATmega 328P) to master using XY-017 board (here is schematics)

Master reads positive integer value on holding register 0x3001
I use ModbusRTU library, here is the code:

#include "ModbusRTU.h"

ModbusRTU::ModbusRTUSlave<4> slave;

unsigned short analogOut[3];

void setup()
{
  slave.begin(19200);  
  slave.update();

  slave.addHoldingRegister(&analogOut[1], 0x3001);
}

void loop() 
{ 
  analogOut[1] = 10;

  slave.update();
}

Also I tried Modbusino , here is the code:

#include <Modbusino.h>

/* Initialize the slave with the ID 1 */
ModbusinoSlave modbusino_slave(1);
/* Allocate a mapping of 10 values */
uint16_t tab_reg[10];

void setup() {
    /* The transfer speed is set to 115200 bauds */
    modbusino_slave.setup(19200);
}

void loop() {
    /* Initialize the first register to have a value to read */
    tab_reg[0] = 0x3001;
    /* Launch Modbus slave loop with:
       - pointer to the mapping
       - max values of mapping */
    modbusino_slave.loop(tab_reg, 10);
}

Both sketches work perfectly fine on Arduino Uno R3 (with 10 and 0 as values on registers). However, when I try Nano, some weird thing happens: when I connect RX pin, the LED on XY-017 indicating RX activity turns off. TX pin does not affect the LEDs. However, while trying on Uno both LEDs (TX and RX) blink in sync. I tried to swap the wires - no result.

There is a configurator for my device where I can see the values on registers and redefine addresses, so there is no problem to change address. Arduino is the only slave in the network.

Maybe I just don't know how to google, but this thing is bothering me for a week or so. Please help me, or show me that I just don't search enough. Thank you in advance!

P.S. I also tested with old Nano (which is using old bootloader) - when I do all the things as above, the RX LED on Arduino starts to blink. No reaction for TX LED, though

Is your question about Rx/Tx LEDs or communication problem?
Same code should work for classic nano and for uno.

Hello! I'm interested in communication, obviously. Nano don't send any information at all (not even blink).
I heard there are some differences in the serial between Uno and Nano

Then they should work also with Arduino Nano 328P.

That's not the case, tried a lot of times

Hard to understand why.
How is your nano powered?

5v outer power/USB-C from computer

Try with phone charger (or with power only cable, not data cable).
Are you sure your rx/tx wiring is solid?

Yes, have checked the wiring
Tried without computer connection, from 12/5 volts converter

Is nano clone or genuine?

Original one

Really strange...
Anyway, since your baud rate is so low, you can use softwareserial on spare digital pins to get your problem solved.

I'd like to, but libs use only HardwareSerial...
Could you suggest some for my case?

Here you have slave example and one examples for using softwareserial.

1 Like
// data array for modbus network sharing
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };

Is this the values of the registers? Are they starting from 0?

Good question.
I have never used any slave library with software serial.
I had a quick look at the library I linked, and I have to tell that it's very confusing how to setup the registers. Sorry for that.

The library that I have been using for slave is this:

Never tried with SS, but you could try.

I'm still surprised if original Nano has a problem using UART pins. There is a difference between USB-serial approach of nano and uno, but what you experiment is quite weird and I have never heard of it.

1 Like

Update: I have CH340C chip on my Arduino Nano, so, my board seem to be analogue. I heard that there should be separate driver for this kind of thing

Afaik original Nano comes only with FT232RL.
If you were able to upload sketch, you have CH drivers installed.


Oh, I'm sorry, it is FTDI FT232RL actually

Mystery remains. It should work without problems.

Are you sure you have TX to TX, RX to RX and 5V/GND to RS485?