问 天问文档中心
⌕
文档

AHTx0 - 温湿度传感器

更新于 2025-01-16 · 文档路径 esp32c3/02-天问ESP32C3-Pro/09-扩展类库/14-AHTx0---温湿度传感器

AHT20和AHT10是两款由广州奥松电子股份有限公司生产的高精度温湿度传感器,它们均采用I2C接口,具有数字输出和高性价比的特点。

数据手册:

一.初始化

初始化AHT10

from machine import Pin
import ahtx0

aht = ahtx0.AHT10(scl = Pin(0),sda = Pin(1))
  • 参数
    • scl:I2C时钟线(SCL)引脚
    • sda:I2C数据线(SDA)引脚

初始化AHT20

from machine import Pin
import ahtx0

aht = ahtx0.AHT20(scl = Pin(0),sda = Pin(1))
  • 参数
    • scl:I2C时钟线(SCL)引脚
    • sda:I2C数据线(SDA)引脚

二.获取温湿度


获取温度

aht.temperature
  • 返回值
    • 返回获取到的温度数值,单位:摄氏度(°C)

使用示例

from machine import Pin
import ahtx0
from board import *
import time

aht = ahtx0.AHT20(scl = Pin(0),sda = Pin(1))
while True:
    try:
        oled.fill(0)
        oled.DispChar(str((str('温度:') + str(round((aht.temperature), 2)))), 0, 0, 1)
        oled.show()
    except:
        pass
    print(str('温度:') + str(aht.temperature))
    time.sleep(1)

获取湿度

aht.relative_humidity
  • 返回值
    • 返回获取到的湿度百分比,单位:相对湿度(%RH)

使用示例

from machine import Pin
import ahtx0
from board import *
import time

aht = ahtx0.AHT20(scl = Pin(0),sda = Pin(1))
while True:
    try:
        oled.fill(0)
        oled.DispChar(str((str('湿度:') + str(round((aht.relative_humidity), 2)))), 0, 0, 1)
        oled.show()
    except:
        pass
    print(str('湿度:') + str(aht.relative_humidity))
    time.sleep(1)