Skip to content

Commit 6282955

Browse files
committed
add SHT3x drive
SHT3x humidity and temperature sensor 8bit I2C micropython drive
1 parent e996465 commit 6282955

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

‎sensor/SHT3x/I2C_8bit/README.md‎

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

‎sensor/SHT3x/I2C_8bit/sht30.jpg‎

17.9 KB
Loading

‎sensor/SHT3x/I2C_8bit/sht3x.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# mpy-lib
2+
# SHT3x humidity and temperature sensor 8bit 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 I2C drive for micropython'
17+
self._mode = 1
18+
self._cmd = 0x240b
19+
self._delay = 6
20+
self._decimal = 1
21+
self._tb = bytearray(2)
22+
self._rb = bytearray(3)
23+
self._ht = bytearray(6)
24+
self._T = 0
25+
self._H = 0
26+
self.reset()
27+
28+
def status(self):
29+
self.write(0xf32d)
30+
self.i2c.readfrom_into(self._addr, self._rb)
31+
return self._rb[0]*256 + self._rb[1]
32+
33+
def clear_status(self):
34+
self.write(0x3041)
35+
36+
def write(self, cmd):
37+
self._tb[0] = cmd>>8
38+
self._tb[1] = cmd
39+
self.i2c.writeto(self._addr, self._tb)
40+
41+
def reset(self):
42+
self.write(0x30a2)
43+
44+
def heater(self, on=0):
45+
if on: self.write(0x306d)
46+
else: self.write(0x3066)
47+
48+
def config(self, mode = 0x240b, delay = 6, decimal = 1):
49+
t = mode >> 8
50+
self._mode = 1 if t == 0x24 or t == 0x2C else 0
51+
self._cmd = mode
52+
self._delay = delay
53+
self._decimal = decimal
54+
self.write(mode)
55+
56+
def measure(self):
57+
if self._mode:
58+
self.write(self._cmd)
59+
sleep_ms(self._delay)
60+
self.write(0xe000)
61+
self.i2c.readfrom_into(self._addr, self._ht)
62+
self._T = self._ht[0]*256+self._ht[1]
63+
self._H = self._ht[3]*256+self._ht[4]
64+
65+
def humidity(self):
66+
return round(100*self._H/65535, self._decimal)
67+
68+
def temperature(self):
69+
return round(175*self._T/65535 - 45, self._decimal)
70+
71+
def ht(self):
72+
return self.humidity(), self.temperature()

0 commit comments

Comments
 (0)