Thanks to visit codestin.com
Credit goes to github.com

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

Commit 4fd80bc

Browse files
Adrian McEwenpeter-pycom
Adrian McEwen
authored andcommitted
Add methods to control the heater in the SI7006 temp/humidity sensor.
1 parent 69414b2 commit 4fd80bc

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

shields/lib/SI7006A20.py

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

2323
SI7006A20_I2C_ADDR = const(0x40)
2424

25+
# I2C commands
2526
TEMP_NOHOLDMASTER = const(0xF3)
2627
HUMD_NOHOLDMASTER = const(0xF5)
28+
WRITE_USER_REG1 = const(0xE6)
29+
READ_USER_REG1 = const(0xE7)
30+
WRITE_HEATER_CTRL_REG = const(0x51)
31+
READ_HEATER_CTRL_REG = const(0x11)
32+
33+
# Register masks and offsets
34+
USER_REG1_HTR_ENABLE_MASK = const(0b00000100)
35+
USER_REG1_HTR_ENABLE_OFFSET = const(0x02)
36+
HTR_CTRL_REG_MASK = const(0b00001111)
2737

2838
def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
2939
if pysense is not None:
@@ -55,18 +65,32 @@ def humidity(self):
5565

5666
def read_user_reg(self):
5767
""" reading the user configuration register """
58-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xE7]))
68+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_USER_REG1]))
5969
time.sleep(0.5)
6070
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
6171
return data[0]
6272

6373
def read_heater_reg(self):
6474
""" reading the heater configuration register """
65-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x11]))
75+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_HEATER_CTRL_REG]))
6676
time.sleep(0.5)
6777
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
6878
return data[0]
6979

80+
def write_heater_reg(self, heater_value):
81+
""" writing the heater configuration register """
82+
# We should only set the bottom four bits of this register
83+
heater_setting = heater_value & HTR_CTRL_REG_MASK
84+
self.write_reg(WRITE_HEATER_CTRL_REG, heater_setting)
85+
86+
def heater_control(self, on_off):
87+
""" turn the heater on or off """
88+
# Get current settings for everything else
89+
user_reg = self.read_user_reg()
90+
# Set the heater bit
91+
user_reg = (user_reg & ~USER_REG1_HTR_ENABLE_MASK) | (on_off << USER_REG1_HTR_ENABLE_OFFSET)
92+
self.write_reg(WRITE_USER_REG1, user_reg)
93+
7094
def read_electronic_id(self):
7195
""" reading electronic identifier """
7296
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFA]) + bytearray([0x0F]))

0 commit comments

Comments
 (0)