micropython +ESP32+ sht30 温湿度模块
SHT30
1)查找SHT30芯片资料
https://www.szlcsc.com
2)根据芯片资料,查得
地址为 0x44 或 0x45
选 Measurement Commands for Single Shot Data Acquisition Mode, 命令为 0x2c10
3)连线
SHT30 ESP32
D1(SCL) 4
D2(SDA) 5
G GND
3V3 3V3
3)编代码
查看micropython官方文档esp32部分。
https://docs.micropython.org/en/latest/esp32/quickref.html
EPS32 的代码
from machine import Pin, I2C import time SHT30 = 0x45 # IIC通信地址 cmd = bytearray(2)# 放命令 data = bytearray(6)# 放数据 i2c = I2C(0, scl=Pin(4), sda=Pin(5), freq=400000) # 初始化硬件IIC0 print(i2c.scan()) # 打印出i2c总线上的全部设备地址 cmd[0] = 0x2c # Measurement Commands for Single Shot Data Acquisition Mode cmd[1] = 0x10 i2c.writeto(0x45, cmd) # write the given buffer to the peripheral 启动一次检测 #MEASURE_CMD = b'\x2C\x10' #i2c.writeto(0x45, MEASURE_CMD) # write the given buffer to the peripheral time.sleep_us(1000) # sleep for 1000 microseconds 等待检测过程结束 data = i2c.readfrom(0x45, 6)# read 6 bytes from device with address 0x45 读取测量结果 temperature = 175*(data[0]*256+data[1])/(65536-1) -45 #计算温度 humidity = 100*(data[3]*256+data[4])/(65536-1) #计算湿度 print('温度:',temperature,"℃") print('湿度:',humidity,"%")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)