Arduino or ESP & SI7021

Arduino or ESP & SI7021

The SI7021 is a high quality sensor for humidity and temperature. Of course there is a library for it for the Arduino IDE. As you can see in the picture, it has an I2C interface. The back side of the sensor:

si7021back

The connection is the same as with other I2C devices, use A4 SDA and A5 SCL or on the ESP D2 for SDA and D1 for SCL.

Below a slightly updated version of the example which comes with the library to have it compile correctly on the ESP8266.

#include 
#include 

#if defined(ESP8266)
// For the ESP8266 we need these to be defined
#define SDA 4
#define SCL 5
#endif

SI7021 sensor;

void setup() {
  Serial.begin(9600);
  Serial.println("Ready!");
#if defined(ESP8266)
// For the ESP8266 we need to start the sensor with SDA and SCL pin numbers
  sensor.begin(SDA,SCL);
#else
// For Arduino we can just call begin.
  sensor.begin();
#endif
}


void loop() {
  // temperature is an integer in hundredths
  float temperature = sensor.getCelsiusHundredths();
  temperature = temperature / 100;
  Serial.println("------------");
  Serial.print("Temperature in Celsius: ");
  Serial.println(temperature);

  // humidity is an integer representing percent
  int humidity = sensor.getHumidityPercent();
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  // this driver should work for SI7020 and SI7021, this returns 20 or 21
  int deviceid = sensor.getDeviceId();
  Serial.print("This is a SI70");
  Serial.println(deviceid);

  Serial.println();
  Serial.println("Heating");
  // enable internal heater for testing
  sensor.setHeater(true);
  delay(20000);
  sensor.setHeater(false);
  Serial.println("Done heating");
  
  // see if heater changed temperature
  temperature = sensor.getCelsiusHundredths();
  temperature = temperature / 100;
  Serial.print("Temperature in Celsius: ");
  Serial.println(temperature);

  Serial.println();
  Serial.println("Cooling");
  //cool down
  delay(20000);

  // get humidity and temperature at same time (also saves power)
  si7021_env data = sensor.getHumidityAndTemperature();
  Serial.println();
  Serial.print("Temperature in Celsius: ");
  Serial.println(data.celsiusHundredths/100);
  Serial.print("Humidity: ");
  Serial.print(data.humidityBasisPoints/100);
  Serial.println("%");
  
  Serial.println();
  Serial.println("Pausing");
  delay(5000);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s