General Raspberry Pi

CHT8305C micropython

userHead DERDRE.CHAMBER 2024-03-29 07:53:05 277 Views0 Replies

Hello, 

I am using an ESP32 and I would like to read data from the CHT8305C sensor with Micropython. I did not find any micropython-library.

and I am definitly not expert with I2C communication. Does anyone could give me an example please?

I began writing this code here after, it works to give the ID of sensor, but I don't manage to read the temperature.

I know that before reading temperature, I have to write into register but I did not find the good code.

Thank you for your help.

 

import machine
from machine import Pin, I2C
import time
import struct

led_pin=2
led = machine.Pin(led_pin, machine.Pin.OUT)
i2c_scl = 35
i2c_sda = 36
i2c = I2C(scl=Pin(i2c_scl), sda=Pin(i2c_sda), freq=400000)

buf = i2c.readfrom_mem(0x40, 0xFE, 2) #Reading sensor ID
ID = struct.unpack(">H",buf)
print(hex(ID))

buf = i2c.readfrom_mem(0x40, 0x00, 2) #Reading sensor temperature
Temp = struct.unpack(">H",buf)
print('Temperature: ’ ,(Temp * 165 / 65535.0) - 40)