2

I'm trying to use a Raspberry Pi 4 to read an I2C temperature and humidity sensor. The part I'm using is a module from Grove based on the DHT20 sensor. Here's a link to a page about the module:

https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-V2-0-DHT20-p-4967.html

I need some help programming the I2C communication. The specs sheet for the sensor explains the frames that are needed for correct communication (see page 8):

https://files.seeedstudio.com/products/101020932/DHT20-Product-Manual.pdf

I would be grateful if someone could show me the Python code needed to get a sensor reading. Preferably this would be based on the smbus package, but I will be grateful for any solution (based on whatever package you prefer). I understand that there are many answers about the DHT11 and DHT22 sensors, but these older models have one-wire logic whereas the newer DHT20 has I2C.

Thanks!

tlfong01
  • 4,665
  • 3
  • 10
  • 24
  • 1
  • 1
    @Milliways: Thanks for the link! Unfortunately the DHT11 and DHT22 are older models that use one-wire logic. The DHT20 is a newer model with I2C logic, so none of the DHT11 and DHT22 solutions will work. I have edited my post to clarify this difference. But I appreciate you trying to help! – thatguyfromcanada Nov 24 '21 at 02:46
  • 1
    a search for dht20 python yielded (amongst many) this result - there is a raspberry pi folder there! – Bravo Nov 24 '21 at 04:47
  • (1) AHT20 - Temperature & Humidity Sensor Breakout Board Product Sheet - Adafruit US$4.50 https://www.adafruit.com/product/4566

    (2) AHT20 - Temperature & Humidity Sensor Breakout Board Learning Guide- Adafruit https://learn.adafruit.com/adafruit-aht20

    (3) AHT20 Temperature Sensor Datasheet - aosong http://www.aosong.com/en/products-32.html

    (4) Why are DHT11/DHT22 problematic? - AdaFruit https://learn.adafruit.com/modern-replacements-for-dht11-dht22-sensors/why-are-they-probelmatic

    / to continue, ...

    – tlfong01 Nov 25 '21 at 08:24
  • (5) What are better alternatives to DHT11/DHT22? - AdaFruit https://learn.adafruit.com/modern-replacements-for-dht11-dht22-sensors/what-are-better-alternatives

    (6) DHT20 GitHub Home - DFRobot https://github.com/DFRobot/DFRobot_DHT20

    (7) DHT20 GitHub Python - DFRobot https://github.com/DFRobot/DFRobot_DHT20/tree/master/python/raspberrypi

    – tlfong01 Nov 25 '21 at 08:24
  • (8) *DHT20 GitHub Python Readme* - DFRobot https://github.com/DFRobot/DFRobot_DHT20/blob/master/python/raspberrypi/README.md

    (9) *DHT20 GitHub Python Program* - DFRobot https://github.com/DFRobot/DFRobot_DHT20/blob/master/python/raspberrypi/DFRobot_DHT20.py

    (10) *DHT20 GitHub Python Example* - DFRobot https://github.com/DFRobot/DFRobot_DHT20/blob/master/python/raspberrypi/examples/get_data.py

    – tlfong01 Nov 25 '21 at 08:30
  • (11) SHT20 Humidity Sensor Product Sheet - Sensirion https://www.sensirion.com/en/environmental-sensors/humidity-sensors/humidity-temperature-sensor-sht2x-digital-i2c-accurate/

    (12) SHT20 Digital Humidity Sensor Datasheet - Sensirion https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_SHT20_Datasheet.pdf

    – tlfong01 Nov 25 '21 at 13:15
  • (13) SHT20 I2C 3V3/5V humidity sensor module - US$1.75 https://pt.aliexpress.com/item/1005001658779750.html?spm=a2g0o.search0304.0.0.3b2c4713npuJhb&algo_pvid=7757d995-f018-4e74-bdaf-b4e66b1a2d7e&algo_exp_id=7757d995-f018-4e74-bdaf-b4e66b1a2d7e-8 – tlfong01 Nov 25 '21 at 13:20
  • (14) Why I cannot read the I2C temperature / humidity sensor SHT20?, RpiSE, Asked 6 years, 10 months ago, Active 6 years, 10 months ago, Viewed 3k times https://raspberrypi.stackexchange.com/questions/26978/why-i-cannot-read-the-temperature-humidity-sensor-sht20 – tlfong01 Nov 26 '21 at 01:37
  • (15) [SHT20 Temperature and Humidity Sensor] I2C slave device address bouncing around [closed] - EESE, Asked 6 months ago Active 6 months ago Viewed 106 times https://electronics.stackexchange.com/questions/563825/i2c-slave-device-address-bouncing-around – tlfong01 Nov 26 '21 at 02:39

2 Answers2

4

First start by finding the I2C address of your sensor. Here's an explanation of how to do that: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c (look under the section "Testing I2C").

Once you have the address, try the following Python code:

import time
import smbus

address = 0x38 #Put your device's address here

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

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

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

data = i2cbus.read_i2c_block_data(address,0x71,7)

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

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

print(temperature) print(humidity)

ultracold
  • 56
  • 1
0

It seems like smbus is deprecated, but smbus2 works as well. Just adapt the code of @ultracold:

import time
import smbus2

address = 0x38 #Put your device's address here

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

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

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

data = i2cbus.read_i2c_block_data(address,0x71,7)

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

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

print(temperature) print(humidity)

ucczs
  • 101