si7021

package module
v0.0.0-...-877404a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2018 License: MIT Imports: 8 Imported by: 0

README

Silicon Labs Si7021 relative humidity and temperature sensor

Build Status Go Report Card GoDoc MIT License

Si7021 (pdf reference) high accuracy temperature and relative humidity sensor. Easily integrated with Arduino and Raspberry PI due to i2c communication interface: image

This sensor has extra feature - integrated heater which could be helpful in some specific application (such as periodic condensate removal, for example).

Here is a library written in Go programming language for Raspberry PI and counterparts, which gives you in the output relative humidity and temperature values (making all necessary i2c-bus interacting and values computing).

Golang usage

func main() {
	// Create new connection to i2c-bus on 1 line with address 0x40.
	// Use i2cdetect utility to find device address over the i2c-bus
	i2c, err := i2c.NewI2C(0x40, 1)
	if err != nil {
		log.Fatal(err)
	}
	defer i2c.Close()

	sensor := si7021.NewSi7021()
	
	rh, t, err := sensor.ReadRelativeHumidityAndTemperature(i2c)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Relative humidity and temperature = %v%%, %v*C\n", rh, t)

Getting help

GoDoc documentation

Installation

$ go get -u github.com/d2r2/go-si7021

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing armv6l in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-si7021 is licensed under MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CMD_REL_HUM_CSE        = []byte{0xE5}       // Measure Relative Humidity, Hold Master Mode (clock stretching enabled)
	CMD_REL_HUM            = []byte{0xF5}       // Measure Relative Humidity, No Hold Master Mode
	CMD_TEMPRATURE_CSE     = []byte{0xE3}       // Measure Temperature, Hold Master Mode (clock stretching enabled)
	CMD_TEMPRATURE         = []byte{0xF3}       // Measure Temperature, No Hold Master Mode
	CMD_TEMP_FROM_PREVIOUS = []byte{0xE0}       // Read Temperature Value from Previous RH Measurement
	CMD_RESET              = []byte{0xFE}       // Reset
	CMD_WRITE_USER_REG_1   = []byte{0xE6}       // Write RH/T User Register 1
	CMD_READ_USER_REG_1    = []byte{0xE7}       // Read RH/T User Register 1
	CMD_WRITE_HEATER_REG   = []byte{0x51}       // Write Heater Control Register
	CMD_READ_HEATER_REG    = []byte{0x11}       // Read Heater Control Register
	CMD_READ_ID_1ST_PART   = []byte{0xFA, 0x0F} // Read Electronic ID 1st Byte
	CMD_READ_ID_2ND_PART   = []byte{0xFC, 0xC9} // Read Electronic ID 2nd Byte
	CMD_READ_FIRMWARE_REV  = []byte{0x84, 0xB8} // Read Firmware Revision
)

Command byte's sequences

Functions

This section is empty.

Types

type FirmwareVersion

type FirmwareVersion byte
const (
	FIRMWARE_VER_1_0 FirmwareVersion = 0xFF
	FIRMWARE_VER_2_0 FirmwareVersion = 0x20
)

func (FirmwareVersion) String

func (v FirmwareVersion) String() string

String define stringer interface.

type HeaterLevel

type HeaterLevel byte

HeaterLevel define gradation of sensor heating. Keep in mind that when heater is on, the temprature value provided by sensor is not correspond to real ambient environment temprature.

const (
	HEATER_LEVEL_1    HeaterLevel = 0x0 // Typical power consumption: 3.09 mA
	HEATER_LEVEL_2    HeaterLevel = 0x1 // Typical power consumption: 9.18 mA
	HEATER_LEVEL_3    HeaterLevel = 0x2 // Typical power consumption: 15.24 mA
	HEATER_LEVEL_4    HeaterLevel = 0x3
	HEATER_LEVEL_5    HeaterLevel = 0x4 // Typical power consumption: 27.39 mA
	HEATER_LEVEL_6    HeaterLevel = 0x5
	HEATER_LEVEL_7    HeaterLevel = 0x6
	HEATER_LEVEL_8    HeaterLevel = 0x7
	HEATER_LEVEL_9    HeaterLevel = 0x8 // Typical power consumption: 51.69 mA
	HEATER_LEVEL_10   HeaterLevel = 0x9
	HEATER_LEVEL_11   HeaterLevel = 0xA
	HEATER_LEVEL_12   HeaterLevel = 0xB
	HEATER_LEVEL_13   HeaterLevel = 0xC
	HEATER_LEVEL_14   HeaterLevel = 0xD
	HEATER_LEVEL_15   HeaterLevel = 0xE
	HEATER_LEVEL_16   HeaterLevel = 0xF // Typical power consumption: 94.2 mA
	HEATER_LEVEL_MASK HeaterLevel = 0xF
)

func (HeaterLevel) String

func (v HeaterLevel) String() string

String define stringer interface.

type SensorType

type SensorType byte

SensorType denote sensor type.

const (
	SI_ENGINEERING_TYPE1 SensorType = 0x00
	SI_ENGINEERING_TYPE2 SensorType = 0xFF
	SI_7013_TYPE         SensorType = 0x0D
	SI_7020_TYPE         SensorType = 0x14
	SI_7021_TYPE         SensorType = 0x15
)

func (SensorType) String

func (v SensorType) String() string

String define stringer interface.

type SerialNumberRaw

type SerialNumberRaw struct {
	SNA3     byte
	CRC_SNA3 byte
	SNA2     byte
	CRC_SNA2 byte
	SNA1     byte
	CRC_SNA1 byte
	SNA0     byte
	CRC_SNA0 byte
	SNB3     byte
	SNB2     byte
	CRC_SNB2 byte
	SNB1     byte
	SNB0     byte
	CRC_SNB0 byte
}

Keeps sensor serial number raw bytes including CRC info.

type Si7021

type Si7021 struct {
	// contains filtered or unexported fields
}

func NewSi7021

func NewSi7021() *Si7021

NewSi7021 returns new sensor instance.

func (*Si7021) GetHeaterLevel

func (v *Si7021) GetHeaterLevel(i2c *i2c.I2C) (HeaterLevel, error)

GetHeaterLevel return sensor heating gradation.

func (*Si7021) GetHeaterStatus

func (v *Si7021) GetHeaterStatus(i2c *i2c.I2C) (bool, error)

GetHeaterStatus return heater status: on (true) or off (false).

func (*Si7021) GetMeasureResolution

func (v *Si7021) GetMeasureResolution(i2c *i2c.I2C) (UserRegFlag, error)

GetMeasureResolution read current sensor measure accuracy.

func (*Si7021) GetVoltageLow

func (v *Si7021) GetVoltageLow(i2c *i2c.I2C) (bool, error)

GetVoltageStatus provide power supply voltage low: low (true) or OK (false).

func (*Si7021) ReadFirmwareVersion

func (v *Si7021) ReadFirmwareVersion(i2c *i2c.I2C) (FirmwareVersion, error)

ReadFirmwareVersion return sensor firmware revision.

func (*Si7021) ReadRelativeHumidity

func (v *Si7021) ReadRelativeHumidity(i2c *i2c.I2C) (float32, error)

ReadRelativeHumidity return relative humidity.

func (*Si7021) ReadRelativeHumidityAndTemperature

func (v *Si7021) ReadRelativeHumidityAndTemperature(i2c *i2c.I2C) (float32, float32, error)

ReadRelativeHumidityAndTemperature return relative humidity and temperature.

func (*Si7021) ReadSensoreType

func (v *Si7021) ReadSensoreType(i2c *i2c.I2C) (SensorType, error)

ReadSensorType return sensor model.

func (*Si7021) ReadSerialNumber

func (v *Si7021) ReadSerialNumber(i2c *i2c.I2C) (int64, error)

ReadSerialNumberRaw read sensor serial number to the struct.

func (*Si7021) ReadSerialNumberRaw

func (v *Si7021) ReadSerialNumberRaw(i2c *i2c.I2C) (*SerialNumberRaw, error)

ReadSerialNumberRaw read sensor serial number to the struct.

func (*Si7021) ReadTemperature

func (v *Si7021) ReadTemperature(i2c *i2c.I2C) (float32, error)

ReadTemperature return temprature.

func (*Si7021) ReadUncompHumidity

func (v *Si7021) ReadUncompHumidity(i2c *i2c.I2C) (uint16, byte, error)

ReadUncompHumidity returns uncompensated humidity and CRC.

func (*Si7021) ReadUncompHumidityAndTemprature

func (v *Si7021) ReadUncompHumidityAndTemprature(i2c *i2c.I2C) (uint16, uint16, error)

ReadUncompHumidityAndTemperature returns uncompensated humidity, temperature and CRC.

func (*Si7021) ReadUncompTemprature

func (v *Si7021) ReadUncompTemprature(i2c *i2c.I2C) (uint16, byte, error)

ReadUncompTemperature returns uncompensated temperature and CRC.

func (*Si7021) Reset

func (v *Si7021) Reset(i2c *i2c.I2C) error

Reset reboot a sensor.

func (*Si7021) SetHeaterLevel

func (v *Si7021) SetHeaterLevel(i2c *i2c.I2C, level HeaterLevel) error

SetHeaterLevel define internal heater heating gradation. Remeber, when heater is on temprature provided by sensor is not correspond to real ambient temprature.

func (*Si7021) SetHeaterStatus

func (v *Si7021) SetHeaterStatus(i2c *i2c.I2C, enableHeater bool) error

SetHeaterStatus enable of disable internal heater.

func (*Si7021) SetMeasureResolution

func (v *Si7021) SetMeasureResolution(i2c *i2c.I2C, res UserRegFlag) error

SetMeasureResolution set up sensor temprature and humidity measure accuracy.

type UserRegFlag

type UserRegFlag byte

UserRegFlag keep sensor states.

const (
	RES_RH_12BIT_TEMP_14BIT UserRegFlag = 0x00 // RH - 12bit, Temperature - 14bit
	RES_RH_8BIT_TEMP_12BIT  UserRegFlag = 0x01 // RH - 8bit, Temperature - 12bit
	RES_RH_10BIT_TEMP_13BIT UserRegFlag = 0x80 // RH - 10bit, Temperature - 13bit
	RES_RH_11BIT_TEMP_11BIT UserRegFlag = 0x81 // RH - 11bit, Temperature - 11bit
	RES_RH_TEMP_MASK        UserRegFlag = 0x81
	HEATER_ENABLED          UserRegFlag = 0x4  // Heater activated
	VOLTAGE_LOW             UserRegFlag = 0x40 // Voltage is lower than 1.9V
)

func (UserRegFlag) String

func (v UserRegFlag) String() string

String define stringer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL