GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — micropython-libraries/sensors/ahtx0.py at main · gaveshalabs/micropython-libraries Expand file tree/ Copy pathahtx0.pyMore file actions 142 lines (121 loc) · 5.21 KB / Copy pathahtx0.pyFile metadata and controls142 lines (121 loc) · 5.21 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 # The MIT License (MIT) # # Copyright (c) 2020 Kattni Rembor for Adafruit Industries # Copyright (c) 2020 Andreas Bühl # Copyright (c) 2023 Lihini Senanayake for Gavesha Labs # # 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. import utime from micropython import const from machine import I2C, Pin def init_aht10(scl_pin, sda_pin, freq=100000, address=0x38): i2c = I2C(scl = Pin(scl_pin), sda = Pin(sda_pin), freq=freq) return AHT10(i2c, address) def init_aht20(scl_pin=None, sda_pin=None, freq=100000, address=0x38): i2c = I2C(scl = Pin(scl_pin), sda = Pin(sda_pin), freq=freq) return AHT20(i2c, address) class AHT10: """Interface library for AHT10/AHT20 temperature+humidity sensors""" AHTX0_I2CADDR_DEFAULT = const(0x38) # Default I2C address AHTX0_CMD_INITIALIZE = 0xE1 # 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 _ready = False def __init__(self, i2c, address=AHTX0_I2CADDR_DEFAULT, fail_silently=True): utime.sleep_ms(20) # 20ms delay to wake up self._i2c = i2c self._address = address self._buf = bytearray(6) if self.is_ready(): self.reset() if not self.initialize(): if fail_silently: print("Error: Could not initialize AHT sensor") else: raise RuntimeError("Could not initialize AHT sensor") elif fail_silently: print("Error: AHT sensor is not available") else: raise RuntimeError("AHT sensor is not available") 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.writeto(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.writeto(self._address, self._buf[0:3]) self._wait_for_idle() if not self.status & self.AHTX0_STATUS_CALIBRATED: return False return True def is_ready(self): try: self._i2c.readfrom(self._address, 1) return True except ValueError: return False @property def status(self): """The status byte initially returned from the sensor, see datasheet for details""" self._read_to_buffer() 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): """Read sensor data to buffer""" 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.writeto(self._address, self._buf[0:3]) def _wait_for_idle(self): """Wait until sensor can receive a new command""" while self.status & self.AHTX0_STATUS_BUSY: utime.sleep_ms(5) def _perform_measurement(self): """Trigger measurement and write result to buffer""" self._trigger_measurement() self._wait_for_idle() self._read_to_buffer() class AHT20(AHT10): AHTX0_CMD_INITIALIZE = 0xBE # Calibration command |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |