Arduino's ArduinoModbus library with Mega?

Hello everyone!

I am working on a industrial project where we plan on using an Arduino together with RS-485 communication in our final solution.
We need the Arduino to be able to act as a both a slave and a master.

I have already used a few different libraries and gotten the Smarmengol modbus library (Modbus-Master-Slave-for-Arduino/ModbusRtu.h at master · smarmengol/Modbus-Master-Slave-for-Arduino · GitHub) to work quite well for our purpose.
But there is a lack of engagement with this library and some workaround solutions have been required.

It would be nice to use standard Arduino software since updates are dependable, support is available, etc.

Looking at the ArduinoModbus official library it says that it's made for the MKR boards.
On this thread some people say similar things: Arduino Modbus Library - #3 by sterretje.

So what I'm wondering is...

  • Can the ArduinoModbus library be used with a Mega with some sort of shield?
  • Can the RS485 library (the one ArduinoModbus is based on) for Arduino be used with a Mega with or without a shield?

Basically I'm wondering if someone has some experience of these libraries and can inform me what types of setups are possible.

Any info would be greatly appreciated

/ Einar

I've used a library called ModbusMaster with both an UNO (software serial port using AltSoftSerial) and a MEGA (hardware serial port).

This code is for an UNO using a software serial port (AltSoftSerial library) and was used to help forum members that wanted to talk to an NPK soil sensor:

// Attempt to access a JXCT NPK sensor to read Nitrogen, Potassium & Phosphorus
// values using an Arduino UNO clone.
//
// This attempt uses the AltSoftSerial & ModbusMaster libraries.
// Get AltSoftSerial at https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
//
// RS485 module wired up as:
// RS485 DI signal to pin 9
// RS485 RO signal to pin 8
// RS485 RE signal to pin 7
// RS485 DE signal to pin 6
// RS485 VCC to 5V
// RS485 GND to GND
//
// NOTE: I do not have this sensor, so I simulated it using the evaluation version of WinModbus.

#include <ModbusMaster.h>
#include <AltSoftSerial.h>

#define MAX485_DE      6
#define MAX485_RE_NEG  7

AltSoftSerial swSerial;
ModbusMaster node;

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

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

void setup() {
  Serial.begin( 9600 );

  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 9600 baud
  swSerial.begin(9600);

  // Modbus slave ID of NPK sensor is 1
  node.begin(1, swSerial);

  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop() {
  uint8_t result;

  // NITROGEN
  result = node.readHoldingRegisters(0x1E, 1);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("   Nitrogen: ");
    Serial.print(node.getResponseBuffer(0x0));
    Serial.println(" mg/kg");
  }

  // PHOSPHORUS
  result = node.readHoldingRegisters(0x1F, 1);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Phosphorous: ");
    Serial.print(node.getResponseBuffer(0x0));
    Serial.println(" mg/kg");
  }

  // POTASSIUM
  result = node.readHoldingRegisters(0x20, 1);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("  Potassium: ");
    Serial.print(node.getResponseBuffer(0x0));
    Serial.println(" mg/kg");
  }
  Serial.println();

  result = node.readHoldingRegisters(33, 2);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("  Test: ");
    Serial.print(node.getResponseBuffer(0x0));
  }
  Serial.println();
  delay(2000);

}

From memory, the above code works just as well on a MEGA with one of the hardware serial ports.

I believe that there are some RS485 shields out there for both the UNO and MKR series. However, I think most users go with one of these:


It gives you the freedom to use whatever pins you want, unlike a shield.

A MEGA is probably where you should be looking as it has 3 additional hardware serial ports.

You would need to experiment to see if you can have 2 instances of the ModbusMaster library - one for the slave node and the other for the master node.

Hello Mark,
I have already gotten modbus working with a similar setup to what you suggest (Mega + RS485 / TTL 485 MAX) and can succesfully use the Mega as both a master/client and slave/server.

What I'm wondering is if there is a way to use the standard ArduinoModbus or standard RS485 library with the Mega(?).
Basically I'm looking for a library that is...

  • Capable of allowing both slave and master function
  • Has an active user base (for questions)
  • Not a dead library (updates occur, development is ongoing)

Thanks for your reply

/ Einar

ModbusMaster gets a fair bit of use from what I can see, and you're likely to get support on the forums for it too if you get stuck.

Ok, however the library description seems to say that it's meant for the Master function only.
Can this be worked around?
/ Einar

Hmm. I must confess to only using it as a master, and you are probably right.

Off the top of my head, ArduinoModbus and the RS485 library should both work providing you can instruct them as to which serial port you are using and which pins the RE and DE control signals are connected to.

Ah ok, I remember reading somewhere else that the ArduinoModbus / RS485 libraries are not suitable for AVR boards due to some RAM issues... mayb this is incorrect or can be solved? :thinking: