Arduino Controlled Modbus Relay Board
by GeorgeA70 in Circuits > Arduino
11376 Views, 12 Favorites, 0 Comments
Arduino Controlled Modbus Relay Board
This is a dull subject but I couldn't find anyone else that had posted anything about controlling this type of relay board from an Arduino. I spent a lot of time trying to get this to work, trying to find documentation for the relay board and figuring out how the Arduino Modbus libraries work. My hope is that this Instructable will save you some time.
I'm working on a project for a friend that has a house that was wired with low voltage touch switches throughout the house that turn on the lights and some switched outlets. The system was install by him and his father in 1957. It was state of the art at the time. After 60 years the relays used are failing. At $60 each we decided to replace them with a different setup. While looking for relays and controllers we decided on an Arduino Mega and an assortment of relay boards.
Choosing the relays
We needed about 32 relays and also also up to 32 switch inputs.
The relay boards we looked at were of 4 types
1. 1 Arduino pin controlled one relay on the board
2. Relays could be controlled via the serial port, RS232 requiring one serial connection for each relay board
3. Relays could be controlled via the internet connection on the relay board
4. Relays could be controlled via RS485 using the Modbus protocol.
Number one above required nearly all the available pins on the Mega without room for expansion. Number two would require fewer pins than number one but two pins for each board used. Number three required adding an ethernet board for the Arduino Mega and made the system dependent on the internet working. Ethernet blackout = a dark house. Number four using Modbus allows 2 wires to be used to control up to 127 relay boards. There seemed to a wide variety of these boards sold by quite a few vendors. Modbus is an industrial standard which is likely to be around for awhile. Arduino has several Modbus libraries so most of the work has already been coded.
The relay arrived with no manual, no instructions, no datasheet and no tech support. I found no web pages or videos on controlling this board from an Arduino.
I eventually found this listing for the relay board. https://www.amazon.com/Akozon-Module-Channel-MODBU...
In a reply to the second question this link was provided to a .rar compressed file which has a manual in it.
https://1drv.ms/u/s!AjrAGEbCBLlsvjyxRyoh2fLtzg20. If someone knew the Modbus protocol inside and out this might have been enough. It did verify how the dip switches were used to set the Modbus slave address. It did give examples of codes to be sent to the relay board including checksums. It did indicate that codes were sent as HEX and the size of those codes being either 8 or 16 bits.
Supplies
Relay board that uses RS485 Modbus controls. These are available in 4,8,16 and 32 relay boards.
Here is one example
https://www.amazon.com/Akozon-Module-Channel-MODBU...
The Arduino does not have a serial port that talks RS485. One serial to RS485 board is required.
I used one like this
https://www.amazon.com/Adapter-Module-Signal-Conve...
An Arduino Mega
Assemble Your Hardware
Use the image above to hook up your hardware. Power to the relay board is not shown.
Arduino Mega to RS485 board.
5V to VCC
Gnd to Gnd
RX (pin 0) to TX
TX (pin1) to RX
Connections from RS485 to Relay board.
A+ to A+
B- to B-
GND is not connected unless the distance between devices is long, where it would be connected to earth ground
The relay board needs its own power supply that matches the board ordered. 5VDC, 12VDC or 24VDC
Additional Modbus devices with different addresses can be added. All A+ are hooked together and all B- are hooked together.
DIP switch settings on the relay board.
Switch one should be in the On position and the remaining 5 in the off position for the provided code. If adding additional relay boards adjust the dip switches according to the manual.
Example Code
This code is run and uploaded to the Arduino Mega from the Arduino IDE program.
Instructions on downloading and getting started with this software can be found here. https://www.arduino.cc/en/Guide
This code also requires the ArduinoModbus library. This can be installed from within the Arduino IDE software.
https://www.arduino.cc/en/guide/libraries
Replace the Bridge library in the example with ArduinoModbus. If asked to install other required libraries click yes. This will add the RS485 library too.
Download the example code from this page.
In the Arduino software choose File>>Open and find the downloaded code.
In the Arduino software select Tools>>Board and find Arduino Mega
Plug in the Arduino Mega by connecting a USB cable from your computer to the Mega
In the Arduino software select Tools>>Port and select the port for the Mega
At this point you should be able to compile and upload the example program to the Mega.
Important Note.
When uploading to the Mega the RS485 board needs to be disconnected.
The ArduinoModbus library shares the serial port with the built in serial port and there is a conflict when uploading. There are a number of other hardware serial ports on a Mega but I was not able to get the ArduinoModbus library to use them to avoid this problem.
Explanation of the code.
#include // This line adds the code in the library to our code
byte gSlaveID = 0x01;
// sets the HEX code address of the relay board in our code. This matches what is set on the DIP switches on the relay board and can be a number between 1 and 127. Address zero is reserved for the master which in this case is the Arduino Mega
// start the Modbus RTU client in the setup() function of the code
if (!ModbusRTUClient.begin(9600)) {
Serial.println("Failed to start Modbus RTU Client!");
}
The board I used was set up to talk at 9600 baud. That might be different for a different Modbus device. Arduino uses 8 bit, parity none, stop bit 1 by default and so does this relay board. If your board is different refer to this page for other configs. https://www.arduino.cc/reference/en/language/funct...
The rest of the code in the loop() just turns on each of the 16 relays one at a time then turns them on one at a time then toggles 16 randomly selected relays turning them on if they are off and off if they are on.
Note that this board does not respond to writing to the coils directly as is common with the Modbus protocol but instead writes a value to a holding register. I have used just three values in this example to turn a relay on, off or toggle it. There are several other options listed in the manual. The holding registers can also be read but I had no need to do this and have not included doing that in this example.
ModbusRTUClient.holdingRegisterWrite(gSlaveID, i, 0x0100)
In the line above gSlaveID was set to one, i is a number between one and 16 and refers to the relay to be controlled. The 0x0100 is the code to tell the board to turn the relay on, 0x0200 is used to turn the relay on and 0x0300 toggles the relay to the opposite of what it is currently.
I did try this example code using an Arduino UNO and it did compile but had a low memory warning. After uploading it printed just a few letters to the serial monitor and died. My guess is that the ArduinoModbus library is too large for the memory available on the UNO.
An additional Arduino sketch has been added that uses the modbus code to control lights and outlets in a home built in the 1950s that has low voltage switches. The relays were failing so the entire system was replaced using this code.