How to Read Holding Register Values of Modbus Serial/RS485/RTU Device in Raspberry Pi Pico W with TTL to RS485 Converter using MicroPython

Introduction:

In today’s industrial landscape, communication between devices plays a crucial role in automation and control systems. Modbus protocol stands out as one of the most widely used standards for this purpose. Leveraging the versatility of Raspberry Pi Pico W and the simplicity of MicroPython, we can seamlessly interact with Modbus slave devices. This guide will provide a detailed walkthrough of reading holding register values from a Modbus slave device using Raspberry Pi Pico W, paired with a TTL to RS485 converter, all within the MicroPython environment.

Prerequisites:

Before delving into the tutorial, ensure you have the following components and prerequisites in place:

  • Raspberry Pi Pico W

  • A Modbus slave device or Simulator (e.g., PLC, sensor).

  • TTL to RS485 converter.

  • MicroUSB cable for Pico.

  • A computer with Thonny or another MicroPython IDE installed.

  • Basic knowledge of Python and Modbus protocol.

Setting up Hardware:

Setting up the hardware involves establishing connections between Raspberry Pi Pico W, TTL to RS485 converter, and the Modbus slave device:

  • Connect the TTL to RS485 converter to Raspberry Pi Pico W:
    • Establish connections between Pico’s UART pins (TX, RX) and the corresponding pins on the converter.
    • Connect the converter’s A and B terminals to the RS485 lines of the Modbus slave device.
  • Power up Raspberry Pi Pico W using a microUSB cable connected to your computer.

Installing Required Libraries:

Before proceeding, we need to ensure the necessary libraries are installed for communication with the Modbus slave device:

  1. Open Thonny or your preferred MicroPython IDE.
  2. Install the ‘micropython-modbus’ library by following this guide [Watch]

Writing the Code:

Now, let’s write the code to read holding register values from the Modbus Serial/RTU/RS485 device:

				
					import time
from machine import Pin, UART
from umodbus.serial import Serial as ModbusRTUMaster

# Define the pins for Modbus communication
rtu_pins = (Pin(0), Pin(1))

# Define the starting address to read from
starting_address = 0

# Define the quantity of registers to read
qty = 3

# Initialize Modbus RTU Master
host = ModbusRTUMaster(baudrate=9600, data_bits=8, stop_bits=1, parity=None, pins=rtu_pins, ctrl_pin=None, uart_id=0)

# Continuous reading loop
while True:
    try:
        print('Reading qty={} from starting address {}:'.format(qty, starting_address))

        # Read holding registers from the slave device
        values = host.read_holding_registers(slave_addr=1, starting_addr=starting_address, register_qty=qty, signed=False)

        # Print the result
        print('Result: {}'.format(values))

    except Exception as e:
        print('An error occurred:', e)

    # Wait for 5 seconds before the next reading
    time.sleep(5)
				
			

Testing:

To ensure that our setup is functioning correctly, we can perform a simple test:

  • Connect Raspberry Pi Pico W to the Modbus slave device via the TTL to RS485 converter.

  • Run the script on Raspberry Pi Pico W.

  • Verify that the holding register values are correctly read and displayed on the console output.

  • Optionally, perform additional tests by modifying the script to read different registers or quantities of registers.

Conclusion:

In this comprehensive guide, we’ve explored how to utilize Raspberry Pi Pico W and MicroPython to communicate with Modbus slave devices using a TTL to RS485 converter. By following the steps outlined above, you can seamlessly read holding register values from Modbus devices, empowering you to integrate Raspberry Pi Pico W into industrial automation and control systems with ease. Experiment, explore, and innovate with this powerful combination of hardware and software!