- Use with Raspberry Pi
- Requires lgpio
- Code tested inside virtual Raspberry Pi Zero/3/4 environments on GitHub
#include "SI7021.h"
#include <iostream>
int main() {
SI7021::SI7021 sensor;
//connect to and initialise the sensor
sensor.connect();
//obtain new temperature and humidity readings
sensor.refresh();
std::cout
<< "Temperature: "
<< sensor.getTemperature() << " C, "
<< "Humidity: "
<< sensor.getHumidity() << " %RH"
<< std::endl;
//prints eg.
//Temperature: 16.0205 C, Humidity: 62.8667 %RH
return 0;
}
-
SI7021( int dev, int addr )
. Constructor takingdev
identifying which I2C device to use (default is device 1), andaddr
identifying whichI2C
address to use (default is 0x40). -
void connect( )
. Setup and connect to the device. -
void disconnect( )
. Disconnect from the device. -
void refresh( )
. Get a humidity and temperature sample from the sensor. -
double getTemperature( )
. Returns the last read temperature in degrees celsius. -
double getHumidity( )
. Returns the last read relative humidity as a percentage. -
void reset( )
. Reset the device. -
void resetSettings( )
. Reset UserRegister1 register (ie. measurement resolution and heater status) and HeaterControlRegister register (ie. heater power). -
std::uint8_t getMeasurementResolution( )
. Returns the measurement resolution from the device, between 0 and 3. -
void setMeasurementResolution( std::uint8_t res )
. Sets the measurement resolution, between 0 and 3. -
VddStatus getVddStatus( )
. ReturnsVddStatus::OK
if voltage level is sufficient, otherwiseVddStatus::LOW
. -
HeaterStatus getHeaterStatus( )
. ReturnsHeaterStatus::ENABLED
if the heater is enabled, orHeaterStatus::DISABLED
if disabled. -
void setHeaterStatus( HeaterStatus status )
. Enable or disable the heater. -
std::uint8_t getHeaterPower( )
. Returns the heater power, between 0 and 15. -
void setHeaterPower( std::uint8_t power )
. Set the heater power, between 0 and 15. -
SerialNumber getSerialNumber( )
. Gets the serial number of the device.SerialNumber
is a typedef for astd::uint64_t
. -
DeviceId getDeviceId( )
. Gets the device id.
identifier | description | devIdToString |
---|---|---|
DeviceId::ENG_SAMPLE_1 |
engineering sample | engineering sample |
DeviceId::ENG_SAMPLE_2 |
engineering sample | engineering sample |
DeviceId::SI7013 |
SI7013 | Si7013 |
DeviceId::SI7020 |
SI7020 | Si7020 |
DeviceId::SI7021 |
SI7021 | Si7021 |
DeviceId::UNKNOWN |
unknown device | unknown |
FirmwareRevision getFirmwareRevision( )
. Gets the device firmware revision.
identifier | description | fwRevToString |
---|---|---|
FirmwareRevision::REV_1_0 |
revision 1.0 | 1.0 |
FirmwareRevision::REV_2_0 |
revision 2.0 | 2.0 |
FirmwareRevision::UNKNOWN |
unknown firmware | unknown |
-
const char* const devIdToString( DeviceId id )
. Returns a C string representing the device id. -
const char* const fwRevToString( FirmwareRevision rev )
. Returns a C string representing the firmware revision.