3.14 haas506 2.0开发教程-example-温湿度传感器(SHT30/GXHT30)
最新案例教程点击下方链接跳转,CSDN已停止更新
点
击
跳
转
1.GXHT30介绍
(1)器件实物图
(2)参数
(3)硬件接线
2.测试代码
- 案例说明
获取GXHT30传感器湿度、温度数值。
寄存器地址0x00,存有6个相关字节数据
cTemp MSB | cTemp LSB | cTemp CRC | Humididty MSB | Humidity LSB | Humidity CRC |
---|---|---|---|---|---|
温度高位 | 温度低位 | 校验码 | 湿度高位 | 湿度低位 | 校验码 |
- main.py
import utime as time
from driver import I2C
#SHT30 address:0x44
#measure command:0x2c
#High repeatability measurement:0x06
#read data from :0x00
#len(data) are 6 bytes : cTemp MSB, cTemp LSB, cTemp CRC, Humididty MSB, Humidity LSB, Humidity CRC
i2c=I2C()
i2c.open('gxht30')
# write cmd to register
def write(cmd1,cmd2):
writeBuf=bytearray([cmd1,cmd2])
i2c.write(writeBuf)
#read data from register
def read(cmd,len):
readBuf=bytearray(len)
readBuf[0]=cmd
i2c.read(readBuf)
return readBuf
# Convert the data
def covert_data(data):
cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45
fTemp = cTemp * 1.8 + 32
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
return cTemp,fTemp,humidity
# main code
def main():
while True:
write(0x2c,0x06)
data=read(0x00,6)
cTemp,fTemp,humidity=covert_data(data)
print ("Relative Humidity : %.2f %%RH" %humidity)
print ("Temperature in Celsius : %.2f C" %cTemp)
print ("Temperature in Fahrenheit : %.2f F" %fTemp)
time.sleep(1)
print("-----------------\n")
if __name__=="__main__":
main()
- board.json
{
"name": "haas506",
"version": "2.0.0",
"io": {
"ADC0": {
"type": "ADC",
"port": 0,
"sampling": 12000000
},
"ADC1": {
"type": "ADC",
"port": 1,
"sampling": 12000000
},
"OLED": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 60
},
"gxht30": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 68
},
"KEY1": {
"type": "GPIO",
"port": 44,
"dir": "irq",
"pull": "pullup",
"intMode": "rising"
},
"led1": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pulldown"
},
"led_g": {
"type": "GPIO",
"port": 32,
"dir": "output",
"pull": "pulldown"
},
"SPI0": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
},
"serial1": {
"type": "UART",
"port": 0,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial2": {
"type": "UART",
"port": 1,
"dataWidth": 8,
"baudRate": 9600,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial3": {
"type": "UART",
"port": 2,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
}
},
"debugLevel": "ERROR",
"repl": "enable",
"replPort": 0
}