(Power Meter) How to use RS485 sensor with IRIV PiControl through Modbus RTU Protocol

(Power Meter) How to use RS485 sensor with IRIV PiControl through Modbus RTU Protocol

  • Qayyum
  • 11 Oct 2023
  • Tutorial
  • Advanced
  • 584

Note: This tutorial is not meant for beginners. This tutorial is for industries that want to use IRIV PiControl with RS485 sensors, alongside any other industrial-grade equipment. This tutorial also assumes that you are already familiar with Raspberry Pi OS, Raspberry Pi Compute Module 4 (CM4), how to flash Raspberry Pi OS into CM4, understand and are able to do SSH remote access. You should also understand and be able to do proper electrical wiring.

 

Note: CAUTION! Some of the components you might use may involve AC Power. This is very dangerous if not handled correctly. Please make sure you are able to understand the schematic diagram and able to do the electrical wiring properly.

 

NoteIRIV PiControl comes with Raspberry Pi OS flashed into the eMMC of the CM4. So you don't need to do any OS flashing unless it is necessary.

 

RS485 Protocol is one of the well-known Serial Communication methods which is widely used in industries. It is because of the ease of use which only requires 2 2-pin connection, and flexibility which multiple RS485 components can be daisy-chained together on the pin. In this tutorial, we will use the ADL 200 Power Meter to measure the voltage, current, and power consumption of a load. ADL 200 Power Meter can measure a lot more parameters, you can refer to the manual booklet. Unfortunately, the ADL 200 Power Meter currently is not available in our store.

 

The components that will be used in this tutorial are:

 

 

For the first-time setup, you may need a Monitor, Keyboard, and Mouse. You need to configure the WiFi network for the IRIV PiControl to connect. Boot your IRIV PiControl. On the desktop, click on the network icon on the top right corner of the screen, choose your WiFi, enter the password of your WiFi, and connect.

 

 

When you successfully connect to the WiFi, the IP Address of the IRIV PiControl will appear on the OLED screen on the side of the IRIV PiControl.

 

 

Now you can remove the Monitor, Keyboard, and Mouse from the IRIV PiControl. It will automatically connect to the same WiFi network every time it is available. You can also view the IP Address on the OLED screen instead of needing to use a Monitor to look up the IP Address if it's ever changed.

 

You can proceed to access your IRIV PiControl remotely through SSH. Now with everything ready, we can proceed with the tutorials.

 

1. Install 'pymodbus' library.

pip3 install -U pymodbus

 

2. Next, let's connect the sensor (Power Meter) to the IRIV PiControl. First, we need to set the Unit ID / Device Address for the sensor. This ID will be used in our code later to get the readings. To do this, we need to power up the power meter. Connect the Live and Neutral wire from your AC power supply to the power meter. You should connect to the L and N terminals of the power meter. 

 

Note: CAUTION! Some of the components you might use may involve AC Power. This is very dangerous if not handled correctly. Please make sure you are able to understand the schematic diagram and able to do the electrical wiring properly.

 

Note: CAUTION! Please turn off any power supply before doing electrical wiring.

 

 

Next, connect L' and N' terminals to your load's Live and Neutral wire. We use a DIN Rail 3-Pin AC Socket as our load, so we can plug in any appliances to be used as the load.

 

 

Next, connect terminals 21 and 22 to RS485 pins on the IRIV PiControl (Pins might differ based on the Power Meter models. Please refer to your respective Power Meter manual or Datasheet for the correct pins):

  • 21 -> RS485 A
  • 22 -> RS485 B

 

 

The overall connection between your Power Meter and IRIV PiControl should look like this:

 

 

Now, that everything is ready, double-check your connection. If everything is good, we can proceed to power up the system to set the Unit ID. The methods might differ based on the Power Meter models. Please refer to your respective Power Meter manual or Datasheet for how to set up the Unit ID / Device Address. Let's set the Unit ID to 2.

 

 

 

 

3. Power up your IRIV PiControl and access it using ssh. Open any text editor of your choice, and write this Python code. And then save it. Let's give it a name, PowerMeter.py.

 


from pymodbus.client import ModbusSerialClient
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
import time

client = ModbusSerialClient(port="/dev/ttyACM0",
                          method="rtu",
                          baudrate=9600,
                          stopbits=1,
                          bytesize=8,
                          parity='N',
                          timeout=1)

connection = client.connect()
if connection:
    print("Connected.")

while True:
    #client.read_holding_registers(Register Address, Register to Read, Unit ID)
    #Register Address 0 @ 0x000, Read Humidity
    resp = client.read_holding_registers(0x000,1,1)
    registers = resp.registers[0:1]
    Decoder = BinaryPayloadDecoder.fromRegisters(registers, Endian.BIG, wordorder=Endian.LITTLE)
    encoding = '16bit_int'
    result = getattr(Decoder,f"decode_{encoding}")()
    result = result / 10
    
    #Register Address 1 @ 0x001, Read Temperature
    resp2 = client.read_holding_registers(0x001,1,1)
    registers2 = resp2.registers[0:1]
    Decoder2 = BinaryPayloadDecoder.fromRegisters(registers2, Endian.BIG, wordorder=Endian.LITTLE)
    encoding = '16bit_int'
    result2 = getattr(Decoder2,f"decode_{encoding}")()
    result2 = result2 / 10
    
    # print(result)
    # print(result2)
    print(str(result) + '\t' + str(result2))

    time.sleep(1)

#Closes the underlying socket connection
client.close()

 

Most RS485 sensors read data from registers. Some sensors have more than two readings. You can refer to their respective manual or datasheets to learn more about their register address. Now, let's run the Python code.

python3.9 PowerMeter.py 

 

You should be able to see the readings on the terminal. The voltage is on the left, and the currents are on the right.

 

 

Now you are done! You can use this Python code and manipulate the variable with other wonderful stuff, for example, monitoring systems or even data logging!

 

Check out the image below, we already tried using IRIV PiControl with a full 'industrial-like' setup with a DIN rail attachment!

 

 

This is just one example of the application of IRIV PiControl. You can use it in many industrial environments and compatible with most industrial components.

Hardware Components