├── docs ├── demo.jpg ├── DHT12.jpg └── DHT12.pdf ├── package.json ├── examples ├── package.json ├── basic_measurement.py └── continuous_measurement.py ├── LICENSE.txt ├── src └── dht12.py └── README.md /docs/demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcauser/micropython-dht12/HEAD/docs/demo.jpg -------------------------------------------------------------------------------- /docs/DHT12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcauser/micropython-dht12/HEAD/docs/DHT12.jpg -------------------------------------------------------------------------------- /docs/DHT12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcauser/micropython-dht12/HEAD/docs/DHT12.pdf -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "urls": [ 3 | ["dht12/__init__.py", "github:mcauser/micropython-dht12/src/dht12.py"] 4 | ], 5 | "deps": [], 6 | "version": "1.1.0" 7 | } 8 | -------------------------------------------------------------------------------- /examples/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "urls": [ 3 | ["dht12/examples/basic_measurement.py", "github:mcauser/micropython-dht12/examples/basic_measurement.py"], 4 | ["dht12/examples/continuous_measurement.py", "github:mcauser/micropython-dht12/examples/continuous_measurement.py"] 5 | ], 6 | "deps": [], 7 | "version": "1.1.0" 8 | } 9 | -------------------------------------------------------------------------------- /examples/basic_measurement.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2016 Mike Causer 2 | # SPDX-License-Identifier: MIT 3 | 4 | """ 5 | MicroPython Aosong DHT12 I2C driver 6 | https://github.com/mcauser/micropython-dht12 7 | 8 | Prints the temperature and humidity 9 | """ 10 | 11 | from machine import I2C, Pin 12 | import dht12 13 | 14 | i2c = I2C(0) 15 | sensor = dht12.DHT12(i2c) 16 | 17 | if sensor.check(): 18 | print(f"DHT12 found at I2C address {dht12.I2C_ADDRESS:#x}") 19 | 20 | sensor.measure() 21 | print(f"Temperature: {sensor.temperature()} C") 22 | print(f"Humidity: {sensor.humidity()} RH") 23 | -------------------------------------------------------------------------------- /examples/continuous_measurement.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2016 Mike Causer 2 | # SPDX-License-Identifier: MIT 3 | 4 | """ 5 | MicroPython Aosong DHT12 I2C driver 6 | https://github.com/mcauser/micropython-dht12 7 | 8 | Prints the temperature and humidity every 4 sec 9 | """ 10 | 11 | from time import sleep_ms 12 | from machine import I2C, Pin 13 | import dht12 14 | 15 | i2c = I2C(0) 16 | sensor = dht12.DHT12(i2c) 17 | 18 | if sensor.check(): 19 | print(f"DHT12 found at I2C address {dht12.I2C_ADDRESS:#x}") 20 | 21 | while True: 22 | sensor.measure() 23 | print(f"Temperature: {sensor.temperature()} C, Humidity: {sensor.humidity()} RH") 24 | sleep_ms(4000) 25 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Mike Causer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/dht12.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2016 Mike Causer 2 | # SPDX-License-Identifier: MIT 3 | 4 | """ 5 | MicroPython Aosong DHT12 I2C driver 6 | https://github.com/mcauser/micropython-dht12 7 | """ 8 | 9 | from micropython import const 10 | 11 | __version__ = "1.1.0" 12 | 13 | I2C_ADDRESS = const(0x5C) # fixed I2C address 14 | 15 | 16 | class DHT12: 17 | def __init__(self, i2c): 18 | self._i2c = i2c 19 | self._buf = bytearray(5) 20 | 21 | def check(self): 22 | if self._i2c.scan().count(I2C_ADDRESS) == 0: 23 | raise OSError(f"DHT12 not found at I2C address {I2C_ADDRESS:#x}") 24 | return True 25 | 26 | def measure(self): 27 | buf = self._buf 28 | self._i2c.readfrom_mem_into(I2C_ADDRESS, 0, buf) 29 | if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xFF != buf[4]: 30 | raise ValueError("Checksum error") 31 | 32 | def temperature(self): 33 | t = self._buf[2] + (self._buf[3] & 0x7F) * 0.1 34 | if self._buf[3] & 0x80: 35 | t = -t 36 | return t 37 | 38 | def humidity(self): 39 | return self._buf[0] + self._buf[1] * 0.1 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MicroPython DHT12 I2C 2 | 3 | A MicroPython library for interfacing with an Aosong DHT12 temperature and humidity sensor over I2C. 4 | 5 | This library focuses on using the I2C interface. The sensor also supports a 1-wire interface, available when pin 4 is connected to GND. 6 | 7 | ![demo](docs/demo.jpg) 8 | 9 | 10 | ## Installation 11 | 12 | Using mip via mpremote: 13 | 14 | ```bash 15 | $ mpremote mip install github:mcauser/micropython-dht12 16 | $ mpremote mip install github:mcauser/micropython-dht12/examples 17 | ``` 18 | 19 | Using mip directly on a WiFi capable board: 20 | 21 | ```python 22 | >>> import mip 23 | >>> mip.install("github:mcauser/micropython-dht12") 24 | >>> mip.install("github:mcauser/micropython-dht12/examples") 25 | ``` 26 | 27 | Manual installation: 28 | 29 | Copy `src/dht12.py` to the root directory of your device. 30 | 31 | 32 | ## Examples 33 | 34 | **Basic usage** 35 | 36 | ```python 37 | from machine import I2C, Pin 38 | import dht12 39 | 40 | i2c = I2C(0) 41 | sensor = dht12.DHT12(i2c) 42 | 43 | sensor.measure() 44 | print(sensor.temperature()) 45 | print(sensor.humidity()) 46 | ``` 47 | 48 | 49 | ## Methods 50 | 51 | ### __init__(i2c) 52 | 53 | As with other modern Aosong sensors, this sensor supports an I2C interface and can be found at address 0x5C. 54 | 55 | ### check() 56 | 57 | Scans the I2C bus looking for the sensor at it's fixed I2C address 0x5C. Raises a OSError if not found. 58 | 59 | ### measure() 60 | 61 | Reads the temperature and humidity from the sensor over the I2C bus and persists for subsequent calls to `temperature()` and `humidity()`. 62 | Received bytes contains a checksum to ensure the data is error free, otherwise an Exception is raised. 63 | 64 | ### temperature() 65 | 66 | Returns the temperature in degrees Celsius from the data collected from the last `measure()` call. 67 | 68 | ### humidity() 69 | 70 | Get the relative humidity as a percentage from the data collected from the last `measure()` call. 71 | 72 | 73 | ## Parts 74 | 75 | * [DHT12 module](https://s.click.aliexpress.com/e/_DClODk9) 76 | * [DHT12](https://s.click.aliexpress.com/e/_DCP2d1B) 77 | * [DHT12](https://s.click.aliexpress.com/e/_DeFy39J) 78 | * [TinyPICO](https://www.tinypico.com/) 79 | * [WeMos D1 Mini](https://www.aliexpress.com/item/32529101036.html) 80 | 81 | 82 | ## Connections 83 | 84 | DHT12 | TinyPICO (ESP32) 85 | ----- | ---------------- 86 | VIN | 3V3 87 | SDA | 22 88 | GND | GND 89 | SCL | 21 90 | 91 | DHT12 | Wemos D1 Mini (ESP8266) 92 | ----- | ----------------------- 93 | VIN | 3V3 94 | SDA | GPIO4 95 | GND | GND 96 | SCL | GPIO5 97 | 98 | 99 | ## Links 100 | 101 | * [micropython.org](http://micropython.org) 102 | * [DHT12 datasheet](docs/DHT12.pdf) 103 | * [TinyPICO Getting Started](https://www.tinypico.com/gettingstarted) 104 | 105 | 106 | ## License 107 | 108 | Licensed under the [MIT License](http://opensource.org/licenses/MIT). 109 | 110 | Copyright (c) 2016 Mike Causer 111 | --------------------------------------------------------------------------------