RP2040 - RS485 modbus RTU

I am using the RP2040 pico pi with Arduino, on the board I have implemented Modbus TCP and RTU. The TCP works seamlessly while I am not able to get the Modbus RTU on RS485 to work.

I am using the MAX3485 chipset with TX connected to GP4 and RX (DO) connected to GP5 (DI) , the RE/DE is connected to GP14. GP4/GP5 is Uart1.

In the Library that I am using I am only able to change the TX pin GP14.

/*
  Modbus-Arduino Example - Lamp (Modbus Serial)
  Copyright by André Sarmento Barbosa
  http://github.com/andresarmento/modbus-arduino
*/
 
#include <Modbus.h>
#include <ModbusSerial.h>

// Modbus Registers Offsets (0-9999)
const int LAMP1_COIL = 100; 
// Used Pins
const int ledPin = 13;

// ModbusSerial object
ModbusSerial mb;

void setup() {
    // Config Modbus Serial (port, speed, byte format) 
    mb.config(&Serial, 38400, SERIAL_8N1, 14);
    // Set the Slave ID (1-247)
    mb.setSlaveId(10);  
    
    // Set ledPin mode
    pinMode(ledPin, OUTPUT);
    // Add LAMP1_COIL register - Use addCoil() for digital outputs
    mb.addCoil(LAMP1_COIL);
}

void loop() {
   // Call once inside loop() - all magic here
   mb.task();
   
   // Attach ledPin to LAMP1_COIL register     
   digitalWrite(ledPin, mb.Coil(LAMP1_COIL));
}
© 2022 GitHub, Inc.

modbus example

How do I validate that GP4 and GP5 are being used for RX/TX ?

I have printed to the Serial and am able to read the output.

I am still not able to get the Modbus RTU to work.

I'm also handling a Modbus RTU issue. I'm using the Arduino Modbus Library, and I can verify my code on UNO and Mega, but when I try to compile the same code in RPI PICO, the RS485 driver comes out and gives several errors; I tried to change the .h file in RS485 lib but failed...

The problem underneath is the difference between PICO and UNO handles UART? Or maybe there are some assumptions when Arduino writes RS485 driver that PICO doesn't follow?

If you have fixed your problem, please do share. Thanks!

I may have some progress here. I looked up the Arduino MEGA board's 'pins_arduino.h' file; in the last part, there are definitions about 'SERIAL_PORT_HARDWARExxxx,' copy and paste these to RPI PICO's 'pins_arduino.h' file and change these pins according to your designs.

I didn't test how things work here because I don't have my rs485 adaptors right now, but I made Arduino IDE compile successfully after this modification.

If you ever met some issue with the I2C device when using PICO, consider using the 'wire.h' from rp2040 libraries, which should resolve some compile issues.

Did you manage to get the Slave Modbus RTU to work on the RP2040?

Yes, it works, the main issue you have to deal with is modifying the pind_arduino.h, I can connect the pico as a RTU slave now.

1 Like

I am able to set the serial using,


Serial2.setRX(4);
Serial2.setTX(5);

Before Serial2.begin...

I also tried to modified the pins_arduino.h file for the actual serial 2 pins,

#define PIN_SERIAL2_TX (4u)   //8u
#define PIN_SERIAL2_RX (5u)   //9u

I am still getting the following errors,

 error: no matching function for call to 'RS485Class::RS485Class(int, unsigned int, int, int)'
  189 | RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
note:   no known conversion for argument 1 from 'int' to 'arduino::HardwareSerial&'
no known conversion for argument 1 from 'int' to 'arduino::HardwareSerial&'
   22 | RS485Class::RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin):
note: candidate: 'constexpr RS485Class::RS485Class(const RS485Class&)'
   45 | class RS485Class : public Stream {

I also met your problem; I can use it on UART 1 (@GP4/5), but I didn't make it run on GP0/1; since I have spear pins, I used this configuration for my application.

@dj1zulu , Did you manage to get the slave modbus RTU working on Serial1 with GP4 and GP5 ? I am facing a similar issue. I do not have any compilation errors but the communication is not happening with the Master node

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.