diff --git a/aht20/README.md b/aht20/README.md new file mode 100644 index 0000000000000000000000000000000000000000..6865272eac898be3d7a5e1bde51833ddb4b078ce --- /dev/null +++ b/aht20/README.md @@ -0,0 +1,82 @@ +# AHT20 + +用于AHT20和AHT10温度和湿度传感器的MicroPython驱动程序。 + + + +## 案例展示 + +  读取温湿度数据, 并判断温度与湿度的高低情况,将其输出在屏幕上 + +![1.jpg](/aht20/img/1.jpg) + +## 物理连接 + + SCL --> G01 + SDA --> G00 + VCC --> 3V3 + GND --> GND + +### 传感器选择 + +   传感器选择如下图所示的型号为aht20的温湿度传感器模块 + +![2.jpg](/aht20/img/2.jpg) + +### + +## 传感器库使用 + +  可以获取[ahtx0.py](https://gitee.com/blackwalnutlabs/waffle-nano-v1-sensor-lib/blob/master//aht20/code/ahtx0.py),将此库通过[Waffle Maker](https://wafflenano.blackwalnut.tech/ide/index.html#/editor) 的文件上传功能将此库上传到`Waffle Nano` + +  我们在可以在主函数中使用以下代码导入此库 + +```python +import ahtx0 +``` + +  在对象构造函数中,我们需要传入三个引脚数字,分别是ADC引脚,以及两个可设置模式为输入的引脚 + +```python +heartSensor = AD8232(analogPin = 5, LO1Pin = 2, LO2Pin = 14) #构造心电传感器对象 +``` + +  使用心电传感器对象的`read()`方法读取出整型数据 + +``` +x=int(sensor.temperature*100) x=int(sensor.relative_humidity*100) #从心电传感器中获取数据 +``` + +​ 关于此库相关细节说明详见代码注释 + +# 样例代码 + +import utime +from machine import Pin, I2C + +import ahtx0 +i2c = I2C(1, sda=Pin(0), scl=Pin(1), freq=100000) + +#使用I2C创建传感器对象 + +sensor = ahtx0.AHT20(i2c) + +while True: + print("\nTemperature: %0.2f C" % sensor.temperature) + print("Humidity: %0.2f %%" % sensor.relative_humidity) + utime.sleep(5) + +# 使用I2C创建传感器对象 +sensor = ahtx0.AHT20(i2c) + +while True: + print("\nTemperature: %0.2f C" % sensor.temperature) + print("Humidity: %0.2f %%" % sensor.relative_humidity) + utime.sleep(5) + +## 案例代码复现 + +  可以获取[main.py](https://gitee.com/blackwalnutlabs/waffle-nano-v1-sensor-lib/blob/master/aht20/code/main.py)函数,将其内容复制到[Waffle Maker](https://wafflenano.blackwalnut.tech/ide/index.html#/editor) 编辑器上传输给`Waffle Nano`,以复现此案例。 + +  案例相关细节说明详见代码注释 + diff --git a/aht20/code/ahtx0.py b/aht20/code/ahtx0.py new file mode 100644 index 0000000000000000000000000000000000000000..7361d36f188a919877b2eecef933525c13857038 --- /dev/null +++ b/aht20/code/ahtx0.py @@ -0,0 +1,117 @@ +# 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. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" + +MicroPython driver for the AHT10 and AHT20 Humidity and Temperature Sensor + +Author(s): Andreas Bühl, Kattni Rembor + +""" + +import utime +from micropython import const + + +class AHT20: + """Interface library for AHT10/AHT20 temperature+humidity sensors""" + + AHTX0_I2CADDR_DEFAULT = const(0x38) # Default I2C address + AHTX0_CMD_INITIALIZE = 0xBE # Initialization command + AHTX0_CMD_TRIGGER = const(0xAC) # Trigger reading command + AHTX0_CMD_SOFTRESET = const(0xBA) # Soft reset command + AHTX0_STATUS_BUSY = const(0x80) # Status bit for busy + AHTX0_STATUS_CALIBRATED = const(0x08) # Status bit for calibrated + + def __init__(self, i2c, address=AHTX0_I2CADDR_DEFAULT): + utime.sleep_ms(20) # 20ms delay to wake up + self._i2c = i2c + self._address = address + self._buf = bytearray(6) + self.reset() + if not self.initialize(): + raise RuntimeError("Could not initialize") + self._temp = None + self._humidity = None + + def reset(self): + """Perform a soft-reset of the AHT""" + self._buf[0] = self.AHTX0_CMD_SOFTRESET + self._i2c.write(self._address, self._buf[0:1]) + utime.sleep_ms(20) # 20ms delay to wake up + + def initialize(self): + """Ask the sensor to self-initialize. Returns True on success, False otherwise""" + # self._buf[0] = self.AHTX0_CMD_INITIALIZE + # self._buf[1] = 0x08 + # self._buf[2] = 0x00 + # self._i2c.write(self._address, self._buf[0:3]) + # self._wait_for_idle() + # if not self.status & self.AHTX0_STATUS_CALIBRATED: + # return False + return True + + @property + def status(self): + """The status byte initially returned from the sensor, see datasheet for details""" + self._read_to_buffer() + # print("status: "+hex(self._buf[0])) + return self._buf[0] + + @property + def relative_humidity(self): + """The measured relative humidity in percent.""" + self._perform_measurement() + self._humidity = (self._buf[1] << 12) | (self._buf[2] << 4) | (self._buf[3] >> 4) + self._humidity = (self._humidity * 100) / 0x100000 + return self._humidity + + @property + def temperature(self): + """The measured temperature in degrees Celcius.""" + self._perform_measurement() + self._temp = ((self._buf[3] & 0xF) << 16) | (self._buf[4] << 8) | self._buf[5] + self._temp = ((self._temp * 200.0) / 0x100000) - 50 + return self._temp + + def _read_to_buffer(self): + self._buf = self._i2c.read(self._address, 6) + # self._i2c.readfrom_into(self._address, self._buf) + + def _trigger_measurement(self): + """Internal function for triggering the AHT to read temp/humidity""" + # self._buf[0] = self.AHTX0_CMD_TRIGGER + # self._buf[1] = 0x33 + # self._buf[2] = 0x00 + # self._i2c.write(self._address, self._buf[0:3]) + self._i2c.write(self._address, b'\xAC\x33\x00') + + def _wait_for_idle(self): + while self.status & self.AHTX0_STATUS_BUSY: + utime.sleep_ms(5) + + def _perform_measurement(self): + self._trigger_measurement() + self._wait_for_idle() + self._read_to_buffer() + + diff --git a/aht20/code/main.py b/aht20/code/main.py new file mode 100644 index 0000000000000000000000000000000000000000..d6761438ed91908c7e6e7d9ed18ff661d126c84a --- /dev/null +++ b/aht20/code/main.py @@ -0,0 +1,82 @@ +from machine import I2C, Pin, SPI +import utime +import ahtx0 +import st7789 +import ujson +import urandom + +spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8)) +display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT)) + +display.init() +utime.sleep(4) +display.fill(st7789.color565(255, 255, 255)) +display.pixel(5, 5, st7789.color565(255, 0, 0)) +display.draw_string(15, 55, "Welcome to use ",size=3,color=st7789.color565(0, 0, 0)) +display.draw_string(5, 85, "the temperature",size=3,color=st7789.color565(0, 0, 0)) +display.draw_string(30, 115, "and humidity",size=3,color=st7789.color565(0, 0, 0)) +display.draw_string(60, 145, "detector",size=3,color=st7789.color565(0, 0, 0)) +utime.sleep(4) + +i2c=I2C(1,sda=Pin(0),scl=Pin(1),freq=100000) +print(i2c.scan()) +sensor = ahtx0.AHT20(i2c) + +while True: + + x=int(sensor.temperature*100) + a=int(x/100) + b=int(x%100) + temp=float(x*1.0/100) + print(temp) + + + display.fill(st7789.color565(255, 255, 255)) + display.pixel(5, 5, st7789.color565(255, 0, 0)) + if(temp<-40): + display.draw_string(1, 10, "Error",size=3,color=st7789.color565(0, 0, 0)) + display.draw_string(1, 40, "Check the connection",size=2,color=st7789.color565(0, 0, 0)) + elif(temp<0): + b=100-b + display.draw_string(20, 10, "Temperature:",size=3,color=st7789.color565(0, 0, 0)) + display.draw_string(30, 40, " "+str(a)+"."+str(b)+" C",size=3,color=st7789.color565(0, 0, 0)) + else: + display.draw_string(20, 10, "Temperature:",size=3,color=st7789.color565(0, 0, 0)) + display.draw_string(30, 40, " "+str(a)+"."+str(b)+" C",size=3,color=st7789.color565(0, 0, 0)) + + if(temp<12 and temp>=-40): + display.draw_string(1, 70, "You need to make the te-",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(20, 90, "mperature higher.",size=2,color=st7789.color565(0, 0, 0)) + elif(temp>45): + display.draw_string(1, 70, "You need to get the tem-",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(40, 90, "perature down.",size=2,color=st7789.color565(0, 0, 0)) + elif(temp>=12 and temp<=45): + display.draw_string(5, 70, "The temperature at the ",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(45, 90, "right moment.",size=2,color=st7789.color565(0, 0, 0)) + + + x=int(sensor.relative_humidity*100) + a=int(x/100) + b=int(x%100) + humi=float(x*1.0/100) + print(humi) + + if(humi>80 or humi<0): + display.draw_string(1, 120, "Error",size=3,color=st7789.color565(0, 0, 0)) + display.draw_string(1, 150, "Check the connection",size=2,color=st7789.color565(0, 0, 0)) + elif(humi<=80 and humi>=0): + display.draw_string(45, 120, "Humidity:",size=3,color=st7789.color565(0, 0, 0)) + display.draw_string(30, 150, " "+str(a)+"."+str(b)+" %",size=3,color=st7789.color565(0, 0, 0)) + + if(humi<50): + display.draw_string(1, 180, "You need to make the hu-",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(50, 200, "midity higher.",size=2,color=st7789.color565(0, 0, 0)) + elif(humi>90): + display.draw_string(1, 180, "You need to get the hum-",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(40, 200, "idity down.",size=2,color=st7789.color565(0, 0, 0)) + elif(humi>=50 and humi<=90): + display.draw_string(20, 180, "The humidity at the ",size=2,color=st7789.color565(0, 0, 0)) + display.draw_string(45, 200, "right moment.",size=2,color=st7789.color565(0, 0, 0)) + + + utime.sleep(4) diff --git a/aht20/img/1.jpg b/aht20/img/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c4f43bdb171ef5cfbf8f734ef1b1e2f12c4f8dea Binary files /dev/null and b/aht20/img/1.jpg differ diff --git a/aht20/img/2.jpg b/aht20/img/2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..267416c04e88b934ce760b4d0f69cf68ee26a552 Binary files /dev/null and b/aht20/img/2.jpg differ