Reputation: 1
I am trying to read sensor data from "Modbus SHT20 Sensor XY-MD02" using "minimalmodbus" library but i am unable to get the readings. I would be grateful if you can help me where am i doing wrong . I apologies if this question sounds stupid as this is my first experience with RS485 communication.
Wiring Diagram https://docs.google.com/document/d/1P_Ts34HH1cGGb3XseNed1IJn0kJBaPhUwH1PTz5cvIc/edit?usp=sharing
[Modules & Sensors']
RS485 to TTL Converter - https://silicontechnolabs.in/products/rs485-to-ttl-converter Temperature and Humidity Transmitter Modbus SHT20 Sensor XY-MD02 - https://roboticsdna.in/product/temperature-and-humidity-transmitter-modbus-sht20-sensor-xy-md02/?src=google&kwd=&adgroup={adgroup}&device=c&campaign={campaign}&adgroup={adgroup}&keyword=&matchtype=&gad_source=1&gclid=Cj0KCQiAvbm7BhC5ARIsAFjwNHtxYcYTCSiO4Y6TK0oxffiWH1NYwVIijQOv5MmM6OdBevpQqZE3KG4aAu0PEALw_wcB and Raspberry pi 5 - dibian OS
My Config.txt
# For more options and information see
# http://rptl.io/configtxt
# Some settings may impact device functionality. See link above for details
# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
dtparam=spi=off
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
# Additional overlays and parameters are documented
# /boot/firmware/overlays/README
# Automatically load overlays for detected cameras
camera_auto_detect=1
# Automatically load overlays for detected DSI displays
display_auto_detect=1
# Automatically load initramfs files, if found
auto_initramfs=1
# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2
# Don't have the firmware create an initial video= setting in cmdline.txt.
# Use the kernel's default instead.
disable_fw_kms_setup=1
# Run in 64-bit mode
arm_64bit=1
# Disable compensation for displays with overscan
disable_overscan=1
# Run as fast as firmware / board allows
arm_boost=1
[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller.
# This line should be removed if the legacy DWC2 controller is required
# (e.g. for USB device mode) or if USB support is not required.
otg_mode=1
[cm5]
dtoverlay=dwc2,dr_mode=host
[all]
dtparam=uart0=on
enable_uart=1
dtoverlay=i2c-rtc,ds3231
dtparam=rtc=off
#dtoverlay=pi3-miniuart-bt
My code -
import minimalmodbus
import serial
import time
instrument = minimalmodbus.Instrument('/dev/ttyAMA0', 1)
instrument.serial.baudrate = 9600
instrument.serial.timeout = 2
def read_temperature_humidity():
try:
temperature = instrument.read_register(0x0001, 2)
humidity = instrument.read_register(0x0002, 2)
temperature = temperature / 10.0
humidity = humidity / 10.0
print(f"Temperature: {temperature} °C")
print(f"Humidity: {humidity} %")
except minimalmodbus.NoResponseError:
print("No response from the sensor. Check connections and settings.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
while True:
read_temperature_humidity()
time.sleep(2)
Output-
pi@raspberrypi:~ $ sudo python3 sensor.py
No response from the sensor. Check connections and settings.
No response from the sensor. Check connections and settings.
Upvotes: 0
Views: 103