Go Down

Topic: MAX 485 & ModbusMaster - Basic Guide (Read 7916 times) previous topic - next topic


Nov 13, 2012, 10:19 am Last Edit: Nov 23, 2012, 03:14 am by zabaat Reason: 1
This is like my 5th post or something, but I wanted to give back. I didn't see a "guides" section and this doesn't really meet many standards in way of a guide, but I thought it could really help someone get started, especially REALLY new people.

Youtube video showing physical hookup:

Excellent post with schematic:

Modbus Master Library:

Code: [Select]
#include <ModbusMaster.h>

// instantiate ModbusMaster object as slave ID 2
// This is how to set the serial port, 3 is serial # and 2 is the SLAVE ID of the node
ModbusMaster node(3,2);
const int txRxPin = 4;
int txState = LOW;

void setup()
  // initialize Modbus communication baud rate
  pinMode(txRxPin, OUTPUT);

void loop()
//creating a counter that transmits "i"
static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];

  txState = HIGH;
  digitalWrite(txRxPin, txState);
  // slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);


Essentially this code shows you how to transmit over modbus a 32bit int


Looks pretty simple, better than I would have thought for Modbus but I guess all the clever stiff is in the library.

Rob Gray aka the GRAYnomad www.robgray.com


Nov 21, 2012, 09:00 am Last Edit: Nov 21, 2012, 09:47 am by zabaat Reason: 1
EDIT : The below could be false... checking...

To add receive capability to MAX485 use this link to modify your modbusmaster.cpp file   (file attached)


Code: [Select]
    // code edited to work with MAX485:
  // transmit request
  UCSR0A=UCSR0A |(1 << TXC0); 
  digitalWrite(3, HIGH);

  for (i = 0; i < u8ModbusADUSize; i++)
#if defined(ARDUINO) && ARDUINO >= 100
    MBSerial.print(u8ModbusADU[i], BYTE);
  while (!(UCSR0A & (1 << TXC0)));
  digitalWrite(3, LOW);
  // --

  u8ModbusADUSize = 0;



I am newbie in arduino. I read this topic but still  I do not know how to use a 0x05 - Write Single Coil function.  Could anyone please explain, how to send (for example 01 05 00 00 FF 00 8C 3A)  the commands using the ModbusMaster library after the condition in the program?


Zabaat good job. Have known tons of info from you .Thanks


Hey, i must send to PowerMeter a command in modbus, but i can't use ModbusMaster Library.

This is the command:
Device Addr : 0x01
Function : 0x03
MSB 1 Word Addr : 0x03
LSB 1 Word Addr : 0x25
Words number MSB: 0x00 
Words number LSB: 0x04
CRC 16 MSB: 0x55
CRC 16 LSB: 0x86

Thank you.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131