Connecting SEN0546 to Raspberry Pi
 Jackson.Ryan  2024-06-07 08:36:49 1455 Views4 Replies
 Jackson.Ryan  2024-06-07 08:36:49 1455 Views4 Replies Hi, I recently acquired a SEN0546 Temperature/RH sensor and I am trying to connect it to my raspberry pi using python, but I am struggling to write the code as this is new to me. I see that the sensor was originally designed with the Arduino in mind with the C++ sample code and such. Can someone provide advice on how I would write similar python code to get this sensor to work on my raspberry pi?
I've got this sensor working with Python on the Raspberry Pi with the following code - hopefully that helps!
import smbus
import time
 
# I2C channel 1 is connected to the GPIO pins
channel = 1
 
# SEN0546 defaults to address 0x40
address = 0x40
 
bus = smbus.SMBus(channel)
 
while True:
# request temperature read
bus.write_byte(address, 0x00)
 
# wait briefly
time.sleep(0.3)
 
# read the 2 byte temperature value
val1 = bus.read_byte(address)
val2 = bus.read_byte(address)
 
# wait briefly
time.sleep(0.3)
 
# request humidity read
bus.write_byte(address, 0x01)
 
# wait briefly
time.sleep(0.3)
# read the 2 byte humidity value
val3 = bus.read_byte(address)
val4 = bus.read_byte(address)
 
# calculate the human-readable values
tempData = val1 << 8 | val2
humidityData = val3 << 8 | val4
 
temp = (tempData * 165 / 65535.0) - 40.0
hum = (humidityData / 65535.0) * 100
 
# print the result
print(f'temp(C): {temp}')
print(f'hum(%RH): {hum}')
time.sleep(1)
 Dylan.Black
 Dylan.Black  Is this sensor support by the RPi, can someone from DFRobot confirm this?
 Jackson.Ryan
 Jackson.Ryan  

