October 16, 2024, 12:18
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 required2. 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 iterable3. (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 error4. (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
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
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"
# 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)