Skip to content

Commit 3b217b4

Browse files
committed
Add mip support
1 parent 75fe94e commit 3b217b4

File tree

9 files changed

+222
-88
lines changed

9 files changed

+222
-88
lines changed

README.md

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,114 @@
11
# MicroPython AM2320 I2C
22

3-
A MicroPython library for interfacing with an [Aosong AM2320](http://www.aosong.com/products-32.html) temperature and humidity sensor over I2C.
3+
A MicroPython library for interfacing with an [Aosong AM2320](http://www.aosong.com/en/products-41.html) temperature and humidity sensor over I2C.
44

55
This library focuses on using the I2C interface. The sensor also supports a 1-wire interface, available when pin 4 is connected to GND.
66

7-
![demo](docs/AM2320.jpg)
7+
![demo](docs/demo.jpg)
88

9-
#### Examples
109

11-
Basic measurement
10+
## Installation
11+
12+
Using mip via mpremote:
13+
14+
```bash
15+
$ mpremote mip install github:mcauser/micropython-am2320
16+
$ mpremote mip install github:mcauser/micropython-am2320/examples
17+
```
18+
19+
Using mip directly on a WiFi capable board:
20+
21+
```python
22+
>>> import mip
23+
>>> mip.install("github:mcauser/micropython-am2320")
24+
>>> mip.install("github:mcauser/micropython-am2320/examples")
25+
```
26+
27+
Manual installation:
28+
29+
Copy `src/am2320.py` to the root directory of your device.
30+
31+
32+
## Examples
33+
34+
**Basic usage**
1235

1336
```python
14-
import am2320
1537
from machine import I2C, Pin
16-
i2c = I2C(scl=Pin(5), sda=Pin(4))
38+
import am2320
39+
40+
i2c = I2C(0)
1741
sensor = am2320.AM2320(i2c)
42+
1843
sensor.measure()
1944
print(sensor.temperature())
2045
print(sensor.humidity())
2146
```
2247

23-
Continuous measurement
2448

25-
```python
26-
import time
27-
import am2320
28-
from machine import I2C, Pin
29-
i2c = I2C(scl=Pin(5), sda=Pin(4))
30-
sensor = am2320.AM2320(i2c)
49+
## Methods
3150

32-
while True:
33-
sensor.measure()
34-
print(sensor.temperature())
35-
print(sensor.humidity())
36-
time.sleep_ms(4000)
37-
```
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+
### _wake()
73+
74+
The sensor goes into sleep mode when idle to help minimise influencing the readings and requires writing a byte to wake it back up.
75+
The write will fail, however the sensor interprets it as a wake up call.
76+
77+
### _crc16(buf)
78+
79+
A 16-bit cyclic redundancy check for verifying the received data is error free.
80+
81+
82+
## Parts
83+
84+
* [AM2320](https://s.click.aliexpress.com/e/_DCcOdjL)
85+
* [AM2320](https://s.click.aliexpress.com/e/_DeLEjAD)
86+
* [TinyPICO](https://www.tinypico.com/)
87+
* [WeMos D1 Mini](https://www.aliexpress.com/item/32529101036.html)
88+
89+
90+
## Connections
91+
92+
AM2320 | TinyPICO (ESP32)
93+
------ | ----------------
94+
VIN | 3V3
95+
SDA | 22
96+
GND | GND
97+
SCL | 21
98+
99+
AM2320 | Wemos D1 Mini (ESP8266)
100+
------ | -----------------------
101+
VIN | 3V3
102+
SDA | GPIO4
103+
GND | GND
104+
SCL | GPIO5
105+
106+
107+
## Links
108+
109+
* [micropython.org](http://micropython.org)
110+
* [AM2320 datasheet](docs/AM2320.pdf)
111+
* [TinyPICO Getting Started](https://www.tinypico.com/gettingstarted)
38112

39113

40114
## License

am2320.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

docs/AM2320.pdf

1.28 MB
Binary file not shown.

docs/demo.jpg

249 KB
Loading

examples/basic_measurement.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython Aosong AM2320 I2C driver
6+
https://github.com/mcauser/micropython-am2320
7+
8+
Prints the temperature and humidity
9+
"""
10+
11+
from machine import I2C, Pin
12+
import am2320
13+
14+
i2c = I2C(0)
15+
sensor = am2320.AM2320(i2c)
16+
17+
if sensor.check():
18+
print(f"AM2320 found at I2C address {am2320.I2C_ADDRESS:#x}")
19+
20+
sensor.measure()
21+
print(f"Temperature: {sensor.temperature()} C")
22+
print(f"Humidity: {sensor.humidity()} RH")

examples/continuous_measurement.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython Aosong AM2320 I2C driver
6+
https://github.com/mcauser/micropython-am2320
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 am2320
14+
15+
i2c = I2C(0)
16+
sensor = am2320.AM2320(i2c)
17+
18+
if sensor.check():
19+
print(f"AM2320 found at I2C address {am2320.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)

examples/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"urls": [
3+
["am2320/examples/basic_measurement.py", "github:mcauser/micropython-am2320/examples/basic_measurement.py"],
4+
["am2320/examples/continuous_measurement.py", "github:mcauser/micropython-am2320/examples/continuous_measurement.py"]
5+
],
6+
"deps": [],
7+
"version": "1.1.0"
8+
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"urls": [
3+
["am2320/__init__.py", "github:mcauser/micropython-am2320/src/am2320.py"]
4+
],
5+
"deps": [],
6+
"version": "1.1.0"
7+
}

src/am2320.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython Aosong AM2320 I2C driver
6+
https://github.com/mcauser/micropython-am2320
7+
"""
8+
9+
from time import sleep_ms
10+
from micropython import const
11+
12+
__version__ = "1.1.0"
13+
14+
I2C_ADDRESS = const(0x5C) # fixed I2C address
15+
16+
17+
class AM2320:
18+
def __init__(self, i2c):
19+
self._i2c = i2c
20+
self._buf = bytearray(8)
21+
22+
def check(self):
23+
self._wake()
24+
if self._i2c.scan().count(I2C_ADDRESS) == 0:
25+
raise OSError(f"AM2320 not found at I2C address {I2C_ADDRESS:#x}")
26+
return True
27+
28+
def measure(self):
29+
buf = self._buf
30+
# wake sensor
31+
self._wake()
32+
# read 4 registers starting at offset 0x00
33+
self._i2c.writeto(I2C_ADDRESS, b"\x03\x00\x04")
34+
# wait at least 1.5ms
35+
sleep_ms(2)
36+
# read data
37+
self._i2c.readfrom_mem_into(I2C_ADDRESS, 0, buf)
38+
crc = buf[6] | (buf[7] << 8)
39+
if crc != self._crc16(buf[:-2]):
40+
raise ValueError("Checksum error")
41+
42+
def temperature(self):
43+
t = ((self._buf[4] & 0x7F) << 8 | self._buf[5]) * 0.1
44+
if self._buf[4] & 0x80:
45+
t = -t
46+
return t
47+
48+
def humidity(self):
49+
return (self._buf[2] << 8 | self._buf[3]) * 0.1
50+
51+
def _wake(self):
52+
try:
53+
self._i2c.writeto(I2C_ADDRESS, b"")
54+
except OSError:
55+
pass
56+
sleep_ms(10)
57+
58+
def _crc16(self, buf):
59+
crc = 0xFFFF
60+
for c in buf:
61+
crc ^= c
62+
for _ in range(8):
63+
if crc & 0x01:
64+
crc >>= 1
65+
crc ^= 0xA001
66+
else:
67+
crc >>= 1
68+
return crc

0 commit comments

Comments
 (0)