Modbus Python Script to Read PT100 Temperature Sensors

by Louis Tournesol in Circuits > Electronics

55 Views, 1 Favorites, 0 Comments

Modbus Python Script to Read PT100 Temperature Sensors

IMG_4338.JPG

These instructions cover a Python script to read the PT100 temperature sensors connected to a four channel Ebyte ME31 data acquisition module (DAC). This is a simple script to communicate with the device from a Windows PC, to read the content of four registers, and to print the temperature readings in the terminal. Note that the Ebyte ME31 provides readings in degree C. The script can easily be edited to convert the degrees in Fahrenheit.

Supplies

ME31.jpg
USB_RS485.jpg
PT100.jpg

EByte ME31: https://www.cdebyte.com/products/ME31-XAXA0404

Waveshare USB to RS485: https://www.amazon.com

PT100 Temperature sensors: https://www.amazon.com

USB cable

Power supply capable of providing a voltage in the range of 8 to 28 VDC

Connections of the PT100 Sensors

IMG_4342.JPG

Connect each PT100 sensor to the Ebyte module. I used 3-wire PT100's. They are available in 2, 3 and 4 wire configurations, the latter being the most precise model. The model I choose came with one red terminal connected to a clear insulation wire, and two blue terminals connected to red wires. I connected the red terminal to the CH+ and the bleu terminals to the CH- and COM connections to the module. The position of both blue terminals can be interchanged.

Connections of the RS485 Converter

IMG_4345.JPG

Connect a wire from the A+ of the USB to RS485 converter to the 485-A connection on the Ebyte module and a wire from the B- of the converter to the 485-B connector of the module. I connected the GND wire of the converter to the GND connector of the module.

Connection to the PC

Connect a USB cable from the USB to RS485 converter to the PC.

Connection of the Power Supply

Connect a power supply that can provide a voltage in the range of 8 to 28 VDC to the VCC and GND of the Ebyte module.

Device Connected

IMG_4343.JPG

Once the module is powered you should see the temperature of each PT100 in the display of the module.

The Simple Python Script

main.PNG

This is my Python script for positive values. A function is added in the step 8 to consider negative values as well as positive ones.

Useful Notes

Notes:

a) COM4 is the USB port name of my PC. You will have to change the name to match the name of your USB port.

b) the register addresses of the Ebyte device where the temperatures are written are 0x0190 to 0x0193. See section 4.1.2 of the user manual.

c) the read function code is 0x04. Also in section 4.1.2 of the user manual.

d) I created a virtual environment to be able to install the minimalmodbus library. I ran my script from a Command Prompt terminal after activating the virtual environment in the terminal. Using virtual environments is greatly encouraged. These instructions are not covering this topic, and very good information can be found in the following link, or other sites: https://docs.python.org/3/library/venv.html.

Downloads

Considering Below 0C Readings

Two_complements.PNG

We will now treat negative temperatures. Refer to the note for negative temperatures in section 4.1.2 of the user manual:

Note: When the temperature is lower than 0℃, the temperature data is uploaded in the form of complement code. Temperature: 0xFFCE=-50→temperature=-5.0℃


Downloads

Considering Below 0C Readings

Below0C.PNG

This is a snip of my command prompt as channel #3 PT100 probe was placed in a freezer.

Considering now that any reading above the number 850, which is the value should the temperature reaches the maximum value the Ebyte can read (850C), is a "complement code" and should be converted in a below 0C value.

Function to Convert Below 0C Readings

Two_complements.PNG

This function is now added to consider the temperature values below 0C:

Final Results

BelowC_ch3.PNG

These are the temperature readings when using the script with the "complement code" conversion function for below 0C readings. The channel #3 PT100 sensor was once again placed in a freezer.

Information on Holding Register Addresses

This is the user manual of the Ebyte ME31.

It's important to get familiar with the register addresses, especially in term of their nomenclature, as not all manufacturers use the same reference.

Fortunately for this project, the addresses shown in the user manual were usable in the Python script as they appear in the text. I had scripts in the past that were returning errors with other devices where the addresses of the holding registers in the manual were in the 5 digit format used originally by Modicon. For example when using a holding resister "40049" listed in the user manual the 400 had to be dropped and the remaining to be substracted by 1. The register 40049 became 48.

I hope these instructions will be useful for your project.