DFRobot URM12, RS485 shield for MEGA 2560

DFRobot software code for this is set up for a Leonardo board ,but they also recommend
MEGA2560, using same code, code works with Leonardo but does not with the Mega,
I have downloaded and done everything they recommend, software libraries used are Arduino modbus and Arduino RS485, there are no pins allocated in code to change for RX and TX between these two boards, I have run out of ideas and DFRobot had one request 4 years ago but never answered the person, can anyone please help regads.

1 Like

Hi James,

Welcome to the forum! You’re using the URM12 Ultrasonic Distance Sensor, RS485 Shield, and Arduino Mega. Which part exactly is causing issues here?

The Rx/Tx pins are set by the board so won’t need to be defined in the code. Can you share some images of how you have the RS485 shield on the board?

1 Like

hi Jack the software DFRobot set by them is supposed to work for both boards, i believe that the Leonardo has an inbuilt serial set up in the on board mcu, where the Mega has a separate chip to the main one to control serial comms and i believe this is why the code is not compatible for the Mega.

3 Likes

The Leonardo has 1 serial port and the Mega has 4. The default serial ports should be the same. Can you share where you have gotten the code from?

2 Likes

go to DFRobot for URM12 then WIKI and that will take you to full explanation of sensor and code and pictures.

2 Likes

The line

Serial.begin(9600);

In the setup is what initialises which Serial port is being used. This should be the same for both boards. Can you share an image of your setup?

2 Likes

correct in setup i have ModbusRTUClient.begin(19200); then Serial.begin(9600)

2 Likes

Hi James,

Spot on, the notable difference is that the USB serial doesn’t go through pins 0 and 1 as well.
However the Mega uses those pins for USB as well.

The library looks like its using Serial1 (I spent waay too long looking through all of the libraries to confirm :crossed_fingers: )

If you re-wire the shield to use pins 18 and 19 it should work: Serial - Arduino Reference

Do you need to use the Mega for your project?

1 Like

I will try those pins on the Mega thanks, yes i do need to run this on a Mega as both this code when fixed and the code i already have running on a Mega for the tft lcd display will become 1 program, this whole system will be used on a grain silo and if satisfactory i will build several more for the rest of our silos, kind regards jim.

1 Like

Hi Liam, i have tried changing to serial1 pins on Mega no success, DFRobot sample code to use with the URM12 uses ArduinoModbus.h and ArduinoRS485.h libraries only, there is no code for Serial1 in it, I am going to write code to cover Uno R3 , Mega2560, this will include SoftwareSerial,h and Serial1 and advise on results, this is frustrating when DFRobot code says theirs works on Mega2560s but does not , regards James.

2 Likes

Hi James,

Serial1 won’t require a new library as it is just a function. Serial1 won’t output to your terminal without changing some code further.

What errors were you running into?

1 Like

I have just finished writing a complete new program and have included in it Serial1 commands along with heaps of extra lines of code, I have tried it on my Mega2560 and UNO R3 and it is driving the URM12 now perfectly, had some typing issues but fixed those during compiling, all fixed .Thank you all for your support through this, kind regards James.

2 Likes

Hi,

Update your code to use the correct serial port for the Mega2560. For example, if you are using Serial on the Leonardo, you might need to change it to Serial1 on the Mega2560:

#if defined(__AVR_ATmega32U4__) // Leonardo
    #define MY_SERIAL Serial
#else // Mega2560
    #define MY_SERIAL Serial1
#endif

void setup() {
    MY_SERIAL.begin(9600); // Set baud rate
}

void loop() {
    // Your code here
}

Thanks :slightly_smiling_face:

2 Likes

Hi Babita,

Welcome to the forum! I like this solution of defining both. I’ve done similar for CS pins in past projects but this is a neater way to do it than I do.