1.AHT10简介
-
模块实物图
-
模块说明
产品型号 | AHT10 |
---|---|
供电电压 | 1.8-3.6V(DC) |
测量范围(湿度) | 0~99.9%RH |
测量范围(温度) | -40~+85℃ |
湿度精度 | ±2%RH(25℃) |
温度精度 | ±0.3℃ |
分辨率 | 温度:0.1℃,湿度:0.1%RH |
输出信号 | I2C信号 |
- 模块引脚表
PIN | 说明 |
---|---|
VIN | 接供电电源 |
GND | 接电源地 |
SCL | I2C的时钟线 |
SDA | I2C的数据线 |
2.测试代码
- main.py
import utime as time
from ahtx0 import AHT10
#creat an instance
a=AHT10()
#set mode(℃ or F)
a.set_mode(0)
while True:
time.sleep(1)
#get datas of temperature and humidity
a.get_data()
- ahtx0.py
import utime as time
from driver import I2C
#CONSTANTS
AHT10_ADDRESS = 0x38 # 0111000 (7bit address)
AHT10_READ_DELAY_MS = 75 # Time it takes for AHT to collect data
AHT_TEMPERATURE_CONST = 200
AHT_TEMPERATURE_OFFSET = 50
KILOBYTE_CONST = 1048576
CMD_INITIALIZE = bytearray([0xE1, 0x08, 0x00])
CMD_MEASURE = bytearray([0xAC, 0x33, 0x00])
FARENHEIT_MULTIPLIER = 9/5
FARENHEIT_OFFSET = 32
class AHT10:
def __init__(self, mode=0, address=AHT10_ADDRESS):
self.i2c = I2C()
self.i2c.open('aht10')
self.address = address
writeBuf=bytearray(4)
writeBuf[0]=self.address
writeBuf[1]=CMD_INITIALIZE[0]
writeBuf[2]=CMD_INITIALIZE[1]
writeBuf[3]=CMD_INITIALIZE[2]
self.i2c.write(writeBuf,4)
self.readings_raw = bytearray(8)
self.results_parsed = [0, 0]
self.mode = mode # 0 for Celsius(摄氏度), 1 for Farenheit(华氏度)
def read_raw(self):
writeBuf=bytearray(4)
writeBuf[0]=self.address
writeBuf[1]=CMD_MEASURE[0]
writeBuf[2]=CMD_MEASURE[1]
writeBuf[3]=CMD_MEASURE[2]
self.i2c.write(writeBuf,4)
time.sleep_ms(AHT10_READ_DELAY_MS)
readBuf=bytearray(6)
readBuf[0]=AHT10_ADDRESS
self.i2c.read(readBuf,6)
self.readings_raw[0]=readBuf[0]
self.readings_raw[1]=readBuf[1]
self.readings_raw[2]=readBuf[2]
self.readings_raw[3]=readBuf[3]
self.readings_raw[4]=readBuf[4]
self.readings_raw[5]=readBuf[5]
self.results_parsed[0] = self.readings_raw[1] << 12 | self.readings_raw[2] << 4 | self.readings_raw[3] >> 4
self.results_parsed[1] = (self.readings_raw[3] & 0x0F) << 16 | self.readings_raw[4] << 8 | self.readings_raw[5]
def humidity(self):
self.read_raw()
return (self.results_parsed[0] / KILOBYTE_CONST) * 100
def temperature(self):
self.read_raw()
if self.mode is 0:
return (self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET
else:
return ((self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET) * FARENHEIT_MULTIPLIER + FARENHEIT_OFFSET
def set_mode(self, mode):
if mode==0 or mode==1:
self.mode = mode
else:
raise ValueError('Mode must be either 0 for Celsius or 1 Farenheit')
def get_data(self):
print("Temperature: " + str(self.temperature()) + ("C","F")[self.mode] + ", Humidity: " + str(self.humidity()))
- board.json
{
"name": "haas506",
"version": "1.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
},
"aht10": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 56
},
"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
}
3.测试结果
-
mode=0 显示摄氏度
-
mode=1 显示华氏度
版权声明:本文为CSDN博主「智云服」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w_hizyf_m/article/details/122843751
暂无评论