micropython读取AHT20温湿度数据
硬件:
合宙esp32-c3,aht20, 杜邦线,面包板
接线:
vin----16(5v)
gnd—14
scl----22
sda—21
每次循环需要调用一下scan(),否者会报错OSError: [Errno 19] ENODEV,对排阵重新焊接后该问题解决,可以去掉i2c.scan()语句,应该是接触不好导致的,但是原因比较费解。
示例代码如下:
from utime import sleep
from machine import Pin, I2C,SoftI2C
import ahtx0
i2c = SoftI2C(scl = Pin(6) ,sda = Pin(10), freq = 100000)
while True:
#i2c.scan()
sensor = ahtx0.AHT20(i2c)
fahrenheit = sensor.temperature * 9 / 5 + 32
wendu=(fahrenheit-32)/1.8
print("\nTemperature: %0.2f F" % fahrenheit)
print(wendu)
print("Humidity: %0.2f %%" % sensor.relative_humidity)
sleep(5)
ahtx0代码如下:
# The MIT License (MIT)
#
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
# Copyright (c) 2020 Andreas Bühl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.