Updating Modbus TCP Coil Status of a Device from Raspberry Pi Pico W using MicroPython

Posted by

In this tutorial, we will learn how to write or update coil status of Modbus TCP device from Raspberry Pi Pico W using MicroPython. Modbus TCP is a communication protocol used for connecting industrial electronic devices. Raspberry Pi Pico W is a small microcontroller board that can be used to control various devices and communicate with them over the internet.

Before we start, ensure that you have the necessary hardware and software requirements:

Hardware:

  1. Raspberry Pi Pico W board
  2. Ethernet cable
  3. Modbus TCP device (e.g., PLC, industrial sensor, etc.)

Software:

  1. Thonny IDE for MicroPython
  2. Modbus TCP library for MicroPython (we will use minimalmodbus)

Now, let’s begin with the tutorial:

Step 1: Connect Raspberry Pi Pico W to Modbus TCP device
Connect your Raspberry Pi Pico W board to the Modbus TCP device using an Ethernet cable. Make sure both devices are powered on and connected to the same local network.

Step 2: Install Thonny IDE and minimalmodbus library
Download and install Thonny IDE on your computer. Open Thonny IDE and connect your Raspberry Pi Pico W board. Once connected, click on the File menu and select ‘New file’ to create a new Python script.

Now, we need to install the minimalmodbus library for MicroPython. To do this, click on the ‘Tools’ menu in Thonny IDE and select ‘Manage packages’. In the search bar, type ‘minimalmodbus’ and click on the ‘Install’ button to install the library.

Step 3: Write the Python script
Now, let’s write the Python script that will write or update the coil status of the Modbus TCP device. Copy and paste the following code into the Thonny IDE:

import minimalmodbus

# Define the Modbus address of the device
instrument = minimalmodbus.Instrument('/dev/ttyS0', 1)  # Change the port and slave address as per your setup

# Define the coil address and value to write
coil_address = 1
coil_value = 1

# Write or update the coil status
instrument.write_bit(coil_address, coil_value)

In the code above, we first import the minimalmodbus library and create an object called ‘instrument’ that represents the Modbus TCP device. We then define the coil address and value that we want to write or update, and finally, we use the ‘write_bit’ method to update the coil status.

Step 4: Run the Python script
Save the Python script by clicking on the ‘File’ menu and selecting ‘Save as’. Choose a file name for the script and save it on your computer. Once saved, click on the ‘Run’ button in Thonny IDE to run the script on your Raspberry Pi Pico W board.

If everything is correct, the coil status of the Modbus TCP device should be updated successfully.

That’s it! You have now learned how to write or update coil status of a Modbus TCP device from Raspberry Pi Pico W using MicroPython. Feel free to explore more functionalities of the minimalmodbus library and experiment with different Modbus TCP devices. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@xendika
1 month ago

Hello, I'm having problems with the function write_multiple_coils.
coil_values = [1,0,1,0,1]
operation_status = modbus_client.write_multiple_coils(slave_addr=slave_addr,starting_address= starting_address,value_list = coil_values)

And it gives me the following error, TypeError: unexpected keyword argument 'value_list'. But on the function prototype the parameter is correct. Do you know what i'm doing wrong? Thanks in advance