Si7021 Humidity and Temperature Sensor Hookup Guide
Si7021 Library and Example Code
To get started, use the example code and library files below.
Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.
If you have not previously installed an Arduino library, please check out our installation guide.You can download the library from the link below. Check out our Installing an Arduino Library tutorial for more help.
Once the library is installed, open Arduino, and expand the examples menu. You should see the Si7021 example.
language:c
/******************************************************************************
SparkFun Si7021 Breakout Example
Joel Bartlett @ SparkFun Electronics
Original Creation Date: May 18, 2015
Updated May 4, 2016
This sketch prints the temperature and humidity the Serial port.
The library used in this example can be found here:
https://github.com/sparkfun/Si7021_Breakout/tree/master/Libraries
Hardware Connections:
HTU21D ------------- Photon
(-) ------------------- GND
(+) ------------------- 3.3V (VCC)
CL ------------------- D1/SCL
DA ------------------- D0/SDA
Development environment specifics:
IDE: Particle Dev
Hardware Platform: SparkFun RedBoard
Arduino IDE 1.6.5
This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful,
please buy us a round!
Distributed as-is; no warranty is given.
*******************************************************************************/
#include "SparkFun_Si7021_Breakout_Library.h"
#include <Wire.h>
float humidity = 0;
float tempf = 0;
int power = A3;
int GND = A2;
//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather sensor;
//---------------------------------------------------------------
void setup()
{
Serial.begin(9600); // open serial over USB at 9600 baud
pinMode(power, OUTPUT);
pinMode(GND, OUTPUT);
digitalWrite(power, HIGH);
digitalWrite(GND, LOW);
//Initialize the I2C sensors and ping them
sensor.begin();
}
//---------------------------------------------------------------
void loop()
{
//Get readings from all sensors
getWeather();
printInfo();
delay(1000);
}
//---------------------------------------------------------------
void getWeather()
{
// Measure Relative Humidity from the HTU21D or Si7021
humidity = sensor.getRH();
// Measure Temperature from the HTU21D or Si7021
tempf = sensor.getTempF();
// Temperature is measured every time RH is requested.
// It is faster, therefore, to read it from previous RH
// measurement with getTemp() instead with readTemp()
}
//---------------------------------------------------------------
void printInfo()
{
//This function prints the weather data out to the default Serial Port
Serial.print("Temp:");
Serial.print(tempf);
Serial.print("F, ");
Serial.print("Humidity:");
Serial.print(humidity);
Serial.println("%");
}
Once you've uploaded the code, connect using this serial terminal to see the output.
Si7021 Functions:
Weather::getRH()
- Returns current Relative Humidity measurement.
Weather::readTemp()
- Returns temperature in Celsius from previous RH measurement.
Weather::getTemp()
- Returns current temp in Celsius.
Weather::readTempF()
- Returns temperature in Fahrenheit from previous RH measurement.
Weather::getTempF()
- Returns current temp in Fahrenheit.
Weather::changeResolution()
- Allows the user to change the humidity and temperature resolution. The vast majority of users do not need to change the resolution. By default the sensor will be in its highest resolution settings. This function is useful if you need to decrease the amount of time between readings or to save power. See the datasheet for more information. As an example, to change the resolution to 11 bit RH and 11 bit temperature, you would call myHumidity.SetResolution(0b10000001); to set bit 7 and bit 0.