Skip to content

Commit f9a7605

Browse files
authored
Merge pull request pycom#44 from pycom/features/PYFW-64-Pysense-temperature-sensor-dew-point
Updated SI7006 temperature sensor lib with dew point computation
2 parents 3f0b2de + f068ac5 commit f9a7605

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

pysense/.DS_Store

-6 KB
Binary file not shown.

pysense/lib/SI7006A20.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import time
22
from machine import I2C
3+
import math
4+
5+
__version__ = '0.0.2'
36

47
class SI7006A20:
8+
""" class for handling the temperature sensor SI7006-A20
9+
+/- 1 deg C error for temperature
10+
+/- 5% error for relative humidity
11+
datasheet available at https://www.silabs.com/documents/public/data-sheets/Si7006-A20.pdf """
12+
513
SI7006A20_I2C_ADDR = const(0x40)
14+
615
TEMP_NOHOLDMASTER = const(0xF3)
716
HUMD_NOHOLDMASTER = const(0xF5)
817

@@ -16,17 +25,84 @@ def _getWord(self, high, low):
1625
return ((high & 0xFF) << 8) + (low & 0xFF)
1726

1827
def temperature(self):
28+
""" obtaining the temperature(degrees Celsius) measured by sensor """
1929
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xF3]))
2030
time.sleep(0.5)
21-
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 2)
31+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 3)
32+
#print("CRC Raw temp data: " + hex(data[0]*65536 + data[1]*256 + data[2]))
2233
data = self._getWord(data[0], data[1])
2334
temp = ((175.72 * data) / 65536.0) - 46.85
2435
return temp
2536

2637
def humidity(self):
38+
""" obtaining the relative humidity(%) measured by sensor """
2739
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xF5]))
2840
time.sleep(0.5)
2941
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 2)
3042
data = self._getWord(data[0], data[1])
3143
humidity = ((125.0 * data) / 65536.0) - 6.0
3244
return humidity
45+
46+
def read_user_reg(self):
47+
""" reading the user configuration register """
48+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xE7]))
49+
time.sleep(0.5)
50+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
51+
return data[0]
52+
53+
def read_heater_reg(self):
54+
""" reading the heater configuration register """
55+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x11]))
56+
time.sleep(0.5)
57+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
58+
return data[0]
59+
60+
def read_electronic_id(self):
61+
""" reading electronic identifier """
62+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFA]) + bytearray([0x0F]))
63+
time.sleep(0.5)
64+
sna = self.i2c.readfrom(SI7006A20_I2C_ADDR, 4)
65+
time.sleep(0.1)
66+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFC]) + bytearray([0xC9]))
67+
time.sleep(0.5)
68+
snb = self.i2c.readfrom(SI7006A20_I2C_ADDR, 4)
69+
return [sna[0], sna[1], sna[2], sna[3], snb[0], snb[1], snb[2], snb[3]]
70+
71+
def read_firmware(self):
72+
""" reading firmware version """
73+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x84])+ bytearray([0xB8]))
74+
time.sleep(0.5)
75+
fw = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
76+
return fw[0]
77+
78+
def read_reg(self, reg_addr):
79+
""" reading a register """
80+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([reg_addr]))
81+
time.sleep(0.5)
82+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
83+
return data[0]
84+
85+
def write_reg(self, reg_addr, value):
86+
""" writing a register """
87+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([reg_addr])+bytearray([value]))
88+
time.sleep(0.1)
89+
90+
def dew_point(self):
91+
""" computing the dew pointe temperature (deg C) for the current Temperature and Humidity measured pair
92+
at dew-point temperature the relative humidity is 100% """
93+
temp = self.temperature()
94+
humid = self.humidity()
95+
h = (math.log(humid, 10) - 2) / 0.4343 + (17.62 * temp) / (243.12 + temp)
96+
dew_p = 243.12 * h / (17.62 - h)
97+
return dew_p
98+
99+
def humid_ambient(self, t_ambient, dew_p = None):
100+
""" returns the relative humidity compensated for the current Ambient temperature
101+
for ex: T-Ambient is 24.4 degC, but sensor indicates Temperature = 31.65 degC and Humidity = 47.3%
102+
-> then the actual Relative Humidity is 72.2%
103+
this is computed because the dew-point should be the same """
104+
if dew_p is None:
105+
dew_p = self.dew_point()
106+
h = 17.62 * dew_p / (243.12 + dew_p)
107+
h_ambient = math.pow(10, (h - (17.62 * t_ambient) / (243.12 + t_ambient)) * 0.4343 + 2)
108+
return h_ambient

pysense/main.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
lt = LTR329ALS01(py)
1313
li = LIS2HH12(py)
1414

15-
print(mp.temperature())
16-
print(mp.altitude())
15+
print("MPL3115A2 temperature: " + str(mp.temperature()))
16+
print("Altitude: " + str(mp.altitude()))
1717
mpp = MPL3115A2(py,mode=PRESSURE) # Returns pressure in Pa. Mode may also be set to ALTITUDE, returning a value in meters
18-
print(mpp.pressure())
19-
print(si.temperature())
20-
print(si.humidity())
21-
print(lt.light())
22-
print(li.acceleration())
23-
print(li.roll())
24-
print(li.pitch())
18+
print("Pressure: " + str(mpp.pressure()))
2519

26-
print(py.read_battery_voltage())
20+
print("Temperature: " + str(si.temperature())+ " deg C and Relative Humidity: " + str(si.humidity()) + " %RH")
21+
print("Dew point: "+ str(si.dew_point()) + " deg C")
22+
t_ambient = 24.4
23+
print("Humidity Ambient for " + str(t_ambient) + " deg C is " + str(si.humid_ambient(t_ambient)) + "%RH")
24+
25+
print("Light (channel Blue lux, channel Red lux): " + str(lt.light()))
26+
27+
print("Acceleration: " + str(li.acceleration()))
28+
print("Roll: " + str(li.roll()))
29+
print("Pitch: " + str(li.pitch()))
30+
31+
print("Battery voltage: " + str(py.read_battery_voltage()))

0 commit comments

Comments
 (0)