RPI Pico with SHT30

October 16, 2024, 12:18

dethress

Hey, I'm trying to get my SHT30 to work with my Pi Pico using those libraries: https://github.com/rsc1975/micropython-sht30/blob/master/sht30.py https://github.com/n1kdo/temperature-sht30/blob/master/src/temperature/sht30.py But with both I run into errors. The way I have SHT30 connected to my Rpi Pico: SDA -> GP4 SCL -> GP5 GND -> Physical Pin 23 (GND) VIN -> 3v3(OUT) I also tried with 10kOhm pull-up resistors SDA->3v3(OUT) + SCL->3v3(OUT) I tried doing an I2C scan but it seems it doesn't even see the device using the following code:
py
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
devices = i2c.scan()

if devices:
    print("Found I2C devices:", devices)
else:
    print("No I2C devices found")
The code I'm trying to test SHT30 with is:
py
from sht30 import SHT30

sensor = SHT30()

temperature, humidity = sensor.measure()

print('Temperature:', temperature, 'ºC, RH:', humidity, '%')
The errors I get: 1. First lib error
MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "sht30.py", line 40, in __init__
TypeError: 'id' argument required
2. Second lib error
MPY: soft reboot
[Errno 110] ETIMEDOUT
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "sht30.py", line 140, in measure
  File "sht30.py", line 104, in send_cmd
TypeError: 'int' object isn't iterable
3. (after adding i2c_id=0 in first lib)
MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "sht30.py", line 136, in measure
  File "sht30.py", line 101, in send_cmd
SHT30Error: Bus error
4. (after adding i2c_id=1 in first lib)
MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "sht30.py", line 40, in __init__
ValueError: bad SCL pin

oops.se

Just to be sure, do you have Circuit or Micro.python loaded on the Pico?

dethress

Yes, other things such as LCD display work

dethress

>>>help('modules') output: __main__ asyncio/lock gc random _asyncio asyncio/stream hashlib re _boot binascii heapq rp2 _boot_fat builtins io select _onewire cmath json struct _rp2 collections machine sys _thread cryptolib math time array deflate micropython uasyncio asyncio/__init__ dht neopixel uctypes asyncio/core ds18x20 onewire vfs asyncio/event errno os asyncio/funcs framebuf platform Plus any modules on the filesystem

dethress

Here's a new code I tried it with
py
from machine import I2C, Pin
import time

I2C_ADDRESS = 0x44 
MEASURE_CMD = b'\x2C\x10'

i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=100000)

def read_temperature_humidity():
    try:
        i2c.writeto(I2C_ADDRESS, MEASURE_CMD)
        
        time.sleep_ms(100)
        
        data = i2c.readfrom(I2C_ADDRESS, 6)
        
        if len(data) == 6:
            temp_raw = data[0] << 8 | data[1]
            hum_raw = data[3] << 8 | data[4]
            
            temperature = -45 + (175  temp_raw / 65535)
            humidity = 100  hum_raw / 65535
            
            return temperature, humidity
        else:
            raise Exception("Invalid response length")
    
    except OSError as e:
        print(f"I2C communication failed: {e}")
        return None, None

devices = i2c.scan()
if I2C_ADDRESS in devices:
    temp, hum = read_temperature_humidity()
    if temp is not None:
        print(f"Temperature: {temp:.2f} °C")
        print(f"Humidity: {hum:.2f} %")
    else:
        print("Failed to read data from sensor")
else:
    print("Sensor not detected")
but the output is Failed to read data from sensor

dethress

And when I do the I2C scan test it gives me address list from 8 to 119, should it even be like that?

oops.se

7 bits addressing = 128 bits and if I remember correctly the first 8 adr is reserved (0-7) and if it does the same with the last it sounds ok https://learn.adafruit.com/i2c-addresses/the-list

oops.se

And the pullup of 10K at 3,3 volt sounds to high, 4k7 or 2k7 ohm is valid values at 3.3volt

dethress

Alright I'll try going with either 4k7 and 2k7 today. Could it be that the problem is because I didn't solder the goldpins to the sensor?

oops.se

No solder = Unreliable connection

oops.se

And if you haven't solder the pins, I can't give you any assistance because everything is unreliable.

dethress

I also have an issue when trying to get DHT11 to work, it's soldered to a breakout board with an internal pull-up resistor on the DATA pin. While using the built-in lib "dht" in my Raspberry Pi Pico, I keep getting an error InvalidPulseCount: Expected 84 but got 1 pulses. There's also an error when I try to pull data without using a library, here's the code I tried:
py
import machine
import utime

def read_dht11(pin):
    dht_pin = machine.Pin(pin, machine.Pin.OUT)
    
    dht_pin.value(0)
    utime.sleep_ms(18)
    
    dht_pin.init(machine.Pin.IN)
    
    timeout = 1000
    
    while dht_pin.value() == 1 and timeout > 0:
        utime.sleep_us(1)
        timeout -= 1
    if timeout == 0:
        print("Timeout waiting for low signal (start)")
        return None, None
    
    timeout = 1000
    while dht_pin.value() == 0 and timeout > 0:
        utime.sleep_us(250)
        timeout -= 1
    if timeout == 0:
        print("Timeout waiting for high signal")
        return None, None
    
    timeout = 1000
    while dht_pin.value() == 1 and timeout > 0:
        utime.sleep_us(1)
        timeout -= 1
    if timeout == 0:
        print("Timeout waiting for start of data")
        return None, None
    
    # Read 40 bits of data
    data = []
    for i in range(40):
        timeout = 1000
        while dht_pin.value() == 0 and timeout > 0:
            utime.sleep_us(1)
            timeout -= 1
        if timeout == 0:
            print("Timeout waiting for data bit")
            return None, None
        
        length = 0
        while dht_pin.value() == 1:
            length += 1
            utime.sleep_us(1)
        
        if length > 30:
            data.append(1)
        else:
            data.append(0)
    
    humidity = 0
    temperature = 0
    for i in range(8):
        humidity = (humidity << 1) | data[i]
    for i in range(16, 24):
        temperature = (temperature << 1) | data[i]
    
    checksum = 0
    for i in range(32, 40):
        checksum = (checksum << 1) | data[i]
    
    return humidity, temperature

humidity, temperature = read_dht11(5)
if humidity is not None and temperature is not None:
    print("Hum: {}%".format(humidity))
    print("Temp: {}C".format(temperature))
else:
    print("Error reading from DHT11 sensor.")

and I keep getting "Timeout waiting for high signal"

thunder07337

Try this
# Load libraries
from machine import Pin
from time import sleep
from dht import DHT11

# Initialize GPIO and DHT11
sleep(1)
dht11_sensor = DHT11(Pin(14, Pin.IN, Pin.PULL_UP))

# Repetition (endless loop)
while True:
    # Perform measurement
    dht11_sensor.measure()
    # Read values
    temp = dht11_sensor.temperature()
    humi = dht11_sensor.humidity()
    # Output values
    print(' Temperature:', temp, '°C')
    print('Humidity:', humi, '%')
    print()
    sleep(3)

Translated with DeepL.com (free version)