Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 6198929

Browse files
author
Adrian McEwen
committed
Add methods to control the heater in the SI7006 temp/humidity sensor.
1 parent 8d655f9 commit 6198929

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pysense/lib/SI7006A20.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ class SI7006A20:
1212

1313
SI7006A20_I2C_ADDR = const(0x40)
1414

15+
# I2C commands
1516
TEMP_NOHOLDMASTER = const(0xF3)
1617
HUMD_NOHOLDMASTER = const(0xF5)
18+
WRITE_USER_REG1 = const(0xE6)
19+
READ_USER_REG1 = const(0xE7)
20+
WRITE_HEATER_CTRL_REG = const(0x51)
21+
READ_HEATER_CTRL_REG = const(0x11)
22+
23+
# Register masks and offsets
24+
USER_REG1_HTR_ENABLE_MASK = const(0b00000100)
25+
USER_REG1_HTR_ENABLE_OFFSET = const(0x02)
26+
HTR_CTRL_REG_MASK = const(0b00001111)
1727

1828
def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
1929
if pysense is not None:
@@ -45,18 +55,32 @@ def humidity(self):
4555

4656
def read_user_reg(self):
4757
""" reading the user configuration register """
48-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xE7]))
58+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_USER_REG1]))
4959
time.sleep(0.5)
5060
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
5161
return data[0]
5262

5363
def read_heater_reg(self):
5464
""" reading the heater configuration register """
55-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x11]))
65+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_HEATER_CTRL_REG]))
5666
time.sleep(0.5)
5767
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
5868
return data[0]
5969

70+
def write_heater_reg(self, heater_value):
71+
""" writing the heater configuration register """
72+
# We should only set the bottom four bits of this register
73+
heater_setting = heater_value & HTR_CTRL_REG_MASK
74+
self.write_reg(WRITE_HEATER_CTRL_REG, heater_setting)
75+
76+
def heater_control(self, on_off):
77+
""" turn the heater on or off """
78+
# Get current settings for everything else
79+
user_reg = self.read_user_reg()
80+
# Set the heater bit
81+
user_reg = (user_reg & ~USER_REG1_HTR_ENABLE_MASK) | (on_off << USER_REG1_HTR_ENABLE_OFFSET)
82+
self.write_reg(WRITE_USER_REG1, user_reg)
83+
6084
def read_electronic_id(self):
6185
""" reading electronic identifier """
6286
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFA]) + bytearray([0x0F]))

0 commit comments

Comments
 (0)