pi 4 – How can Rpi4B use python to speak to the Aoson I2C DHT20 temperature and humidity sensor?

First begin by discovering the I2C tackle of your sensor. Here is a proof of how to try this: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c (look below the part "Testing I2C").

After you have the tackle, strive the next Python code:

import time
import smbus

tackle = 0x38 #Put your gadget's tackle right here

i2cbus = smbus.SMBus(1)
time.sleep(0.5)

information = i2cbus.read_i2c_block_data(tackle,0x71,1)
if (information[0] | 0x08) == 0:
  print('Initialization error')

i2cbus.write_i2c_block_data(tackle,0xac,[0x33,0x00])
time.sleep(0.1)

information = i2cbus.read_i2c_block_data(tackle,0x71,7)

Traw = ((information[3] & 0xf) << 16) + (information[4] << 8) + information[5]
temperature = 200*float(Traw)/2**20 - 50

Hraw = ((information[3] & 0xf0) >> 4) + (information[1] << 12) + (information[2] << 4)
humidity = 100*float(Hraw)/2**20

print(temperature)
print(humidity)


Source link

Leave a Reply

Your email address will not be published. Required fields are marked *