Arduino Modbus Server
Arduino Modbus RTU Server Example
Sample project showing how to read Arduino's Analog and Digital using the Modbus Monitor XPF Program. (GitHub)
Source Code of the Arduino Modbus RTU Server Program can be downloaded from GitHub, Modbus-Monitor, ArduinoModbusServer
Start New Arduino Sketch Program or use example Blink program
Program Arduino UNO as Modbus RTU Server using ArduinoModbus Open Source Modbus Library.
#include <ArduinoModbus.h>
Set pins in Arduino's Setup Function:
pinMode(ledPin, OUTPUT); //Pin 13 = LED with 1k Register
pinMode(12, INPUT_PULLUP); //Pin 12 = DI //Connect GND To Turn OFF
pinMode(11, INPUT_PULLUP); //Pin 11 = DI //Connect GND To Turn OFF
pinMode(10, INPUT_PULLUP); //Pin 10 = DI //Connect GND To Turn OFF
pinMode(9, INPUT_PULLUP); //Pin 09 = DI //Connect GND To Turn OFF
pinMode(8, INPUT_PULLUP); //Pin 08 = DI //Connect GND To Turn OFF
pinMode(7, INPUT); //Pin 07 = DI //Connect +5V To Turn ON
pinMode(6, INPUT); //Pin 06 = DI //Connect +5V To Turn ON
pinMode(5, INPUT); //Pin 05 = DI //Connect +5V To Turn ON
pinMode(4, INPUT); //Pin 04 = DI //Connect +5V To Turn ON
pinMode(3, INPUT); //Pin 03 = DI //Connect +5V To Turn ON
pinMode(2, INPUT); //Pin 02 = DI //Connect +5V To Turn ON
Configure Serial Port and ArduinoModbus Server in Setup section
Serial.begin( BaudRate )
ModbusRTUServer.begin( StationID, BaudRate )
ModbusRTUServer.configureCoils(0x00, 14)
ModbusRTUServer.configureDiscreteInputs(0x00, 14)
ModbusRTUServer.configureInputRegisters(0x00, 10)
ModbusRTUServer.configureHoldingRegisters(0x00,14);
Map Digital Inputs, Digital Output, and Analog Output to Modbus Registers.
See Source on GitHub for complete program.
Example Read Digital Pin: coilValue = digitalRead( 2 ); //Read Pin 2
Example Write to Modbus Server Coil: ModbusRTUServer.coilWrite( 0 , coilValue) //Write value to Modbus Server
Example Read Analog Pin: holdingRegisterValue = analogRead( 0 ); //Read A0
Example Write to Holding Register: ModbusRTUServer.holdingRegisterWrite( 0, holdingRegisterValue) //Write to Modbus Address 0 of Holding Register
Connect Arduino UNO to Computer, compile the program, fix any errors, upload your program. Note the COM port used by Arduino from Windows' Device Manager.
Read Modbus using the Modbus Monitor XPF or Modbus Monitor Advanced for Android
Option 1: Pre-configured Modbus Map for this project can be download from Modbus Monitor XPF program's Online window. Click here on how to use the Online option.
Manual Entry: Add Register for each Modbus Address in Arduino Program. For example, your program maps Pin 2 to Modbus Address 3 (one-based) of the Coil. This address translates to "000003" as the Address value and "BIT" as data type. Similarly, program Address as 400001 when your program maps A0 to first Holding Register.
Select and Configure the COM port in the Client (Tab) of the Modbus Monitor Program. Be sure the COM port and Configuration parameters of Arduino and Modbus Monitor XPF matches. The sample program uses 9600, 8N1 as default configuration.
Click Start and note the values updates as Analog and Digital value of the Arduino changes. For example, the BUILDIN_LED PIN 13 should toggle between 1 and 0 at the Modbus Address "13" or "100013".
Please send feedback at help@modbusmonitor.com or fill online form for any suggestions of improvements.