Skip to content

Commit f53a901

Browse files
committed
add SHT3x 16bit I2C mode drive
SHT3x Humidity and Temperature Sensor 16bit I2C micropython driver
1 parent 6282955 commit f53a901

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

‎sensor/SHT3x/I2C_16bit/README.md‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [mpy-lib](https://github.com/micropython-Chinese-Community/mpy-lib)
2+
3+
## SHT3x
4+
5+
Humidity and Temperature Sensor micropython driver, **16bit** I2C address mode.
6+
7+
![](sht30.jpg)
8+
9+
10+
11+
## ESP32 example
12+
13+
```python
14+
from machine import I2C, Pin
15+
from time import sleep_ms
16+
from sht3x_16bit import SHT3x
17+
18+
i2c = I2C(0, sda=Pin(21), scl = Pin(22))
19+
20+
sht30 = SHT3x(i2c)
21+
22+
while True:
23+
sht30.measure()
24+
print(sht30.ht())
25+
sleep_ms(1000)
26+
```
27+
28+
## API
29+
30+
- SHT3x.**measure()**
31+
Starting measurement.
32+
33+
- SHT3x.**humidity()**
34+
get humidity.
35+
36+
- SHT3x.**temperature()**
37+
get temperature.
38+
39+
- SHT3x.**ht()**
40+
get humidity and temperature.
41+
42+
- SHT3x.**config(mode = 0x240b, delay = 6, decimal = 1)**
43+
- mode, measurement command. Please see [mode table](#mode) below.
44+
- delay, measurement duration time
45+
- low repeatability, 4ms
46+
- Medium repeatability, 6ms (default)
47+
- High repeatability, 15ms
48+
- decimal, number of digits after decimal point
49+
50+
- SHT3x.**heater(on=0)**
51+
Turn on/off internal heater.
52+
- on=1, turn internal heater
53+
- on=0, turn off internal heater
54+
55+
- SHT3x.**status()**
56+
Read **Status Register**
57+
58+
- SHT3x.**clear_status()**
59+
Clear **Status Register**
60+
61+
- SHT3x.**reset()**
62+
Soft Reset
63+
64+
<a name='mode'>mode table</a>
65+
| mode | Repeatability | Measurement | mps |
66+
|-|-|-|-|
67+
| 0x2c06 | High | Single Shot| - |
68+
| 0x2c0D | Medium | Single Shot | -|
69+
| 0x2c10 | Low | Single Shot | -|
70+
| 0x2400 | High | Single Shot|- |
71+
| 0x240b (default)| Medium | Single Shot |-|
72+
| 0x2416 | Low | Single Shot | -|
73+
| 0x2032 | High | Periodic mode | 0.5|
74+
| 0x2024 | Medium | Periodic mode | 0.5|
75+
| 0x202F | Low | Periodic mode | 0.5|
76+
| 0x2130 | High | Periodic mode | 1|
77+
| 0x2126 | Medium | Periodic mode | 1|
78+
| 0x212d | Low | Periodic mode | 1|
79+
| 0x2236 | High | Periodic mode | 2|
80+
| 0x2220 | Medium | Periodic mode | 2|
81+
| 0x222b | Low | Periodic mode | 2|
82+
| 0x2334 | High | Periodic mode | 4|
83+
| 0x2322 | Medium | Periodic mode | 4|
84+
| 0x2329 | Low | Periodic mode | 4|
85+
| 0x2737 | High | Periodic mode | 10|
86+
| 0x2721 | Medium | Periodic mode | 10|
87+
| 0x272a | Low | Periodic mode | 10|
88+
89+
90+
please note, In order to simplify the code, crc8 checksum is ignored.
91+
92+
From [microbit/micropython Chinese community](https://www.micropython.org.cn).

‎sensor/SHT3x/I2C_16bit/sht30.jpg‎

17.9 KB
Loading
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# mpy-lib
2+
# SHT3x humidity and temperature sensor 16bit I2C micropython drive
3+
# ver: 1.0
4+
# License: MIT
5+
# Author: shaoziyang
6+
# https://github.com/micropython-Chinese-Community/mpy-lib
7+
8+
from time import sleep_ms
9+
10+
class SHT3x():
11+
12+
def __init__(self, i2c, addr = 68):
13+
self.i2c = i2c
14+
self._addr = addr
15+
self.ver = '1.0'
16+
self.info = 'SHT3x 16bit I2C drive for micropython'
17+
self._mode = 1
18+
self._cmd = 0x240b
19+
self._delay = 6
20+
self._decimal = 1
21+
self._rb = bytearray(3)
22+
self._ht = bytearray(6)
23+
self._T = 0
24+
self._H = 0
25+
self.reset()
26+
27+
def status(self):
28+
self.i2c.readfrom_mem_into(self._addr, 0xf32d, self._rb, addrsize=16)
29+
return self._rb[0]*256 + self._rb[1]
30+
31+
def write(self, cmd):
32+
self.i2c.writeto_mem(self._addr, cmd, b'', addrsize=16)
33+
34+
def reset(self):
35+
self.write(0x30a2)
36+
37+
def heater(self, on=0):
38+
if on: self.write(0x306d)
39+
else: self.write(0x3066)
40+
41+
def config(self, mode = 0x240b, delay = 6, decimal = 1):
42+
t = mode >> 8
43+
self._mode = 1 if t == 0x24 or t == 0x2C else 0
44+
self._cmd = mode
45+
self._delay = delay
46+
self.write(mode)
47+
48+
def measure(self):
49+
if self._mode:
50+
self.write(self._cmd)
51+
sleep_ms(self._delay)
52+
self.i2c.readfrom_mem_into(self._addr, 0xe000, self._ht, addrsize=16)
53+
self._T = self._ht[0]*256+self._ht[1]
54+
self._H = self._ht[3]*256+self._ht[4]
55+
56+
def humidity(self):
57+
return round(100*self._H/65535, self._decimal)
58+
59+
def temperature(self):
60+
return round(175*self._T/65535 - 45, self._decimal)
61+
62+
def ht(self):
63+
return self.humidity(), self.temperature()

0 commit comments

Comments
 (0)