Categories

Arduino and SI7021 Humidity and Temperature Sensor example

The Si7021 I2C Humidity and Temperature Sensor is a monolithic CMOS IC integrating humidity and temperature sensor elements, an analog-to-digital converter, signal processing, calibration data, and an I2C Interface. The patented use of industry-standard, low-K polymeric dielectrics for sensing humidity enables the construction of low-power, monolithic CMOS Sensor ICs with low drift and hysteresis, and excellent long term stability

Features

  • Relative Humidity Sensor:
    • Si7013/21: ± 3% RH (maximum) @ 0-80% RH
    • Si7020: ± 4% RH (maximum) @ 0-80% RH
    • Si7006: ± 5% RH (maximum) @ 0-80% RH
  • Temperature Sensor:
    • Si7013/20/21: ±0.4°C accuracy (maximum) @ -10 to +85°C
    • Si7006: ±1.0°C accuracy (maximum) @ -10 to +85°C
  • 0 to 100% RH operating range
  • Up to -40 to +125°C operating range
  • Wide operating voltage range (1.9 to 3.6V)
  • Low Power Consumption: 2.2µW average power at 3.3V and 1 sample per second
  • I2C host interface
  • Integrated on-chip heater
  • 3mm x 3mm QFN package
  • Excellent long term stability
  • Factory calibrated
  • Optional factory-installed filter/cover
    • Lifetime protection during reflow and in operation
    • Protects against contamination from dust, dirt, household chemicals and other liquids
    • AEC-Q100 automotive qualified (Si7013/20/21)

 

 

Connection

 

Must use 3v3 for Vin

Arduino Pins Module Pins
 3v3  Vin
 Gnd  Gnd
 SDA  SDA
 SCL  SCL

 

Code

#include <Wire.h>
 
const int ADDR =0x40;
int X0,X1,Y0,Y1,Y2,Y3;
double X,Y,X_out,Y_out1,Y_out2;
 
void setup()
{ 
  Serial.begin(9600);   
  Wire.begin();                                   
  delay(100);  
  Wire.beginTransmission(ADDR);
  Wire.endTransmission();                        
}
 
void loop()
{
  /**Send command of initiating temperature measurement**/
  Wire.beginTransmission(ADDR);
  Wire.write(0xE3);
  Wire.endTransmission();
 
  Serial.print("Temp");
  Serial.print("\t");
  Serial.println("Humidity");
 
  /**Read data of temperature**/
  Wire.requestFrom(ADDR,2);
 
  if(Wire.available()<=2);
  {
    X0 = Wire.read();
    X1 = Wire.read();
    X0 = X0<<8;
    X_out = X0+X1;
  }
 
  /**Calculate and display temperature**/
  X=(175.72*X_out)/65536;                        
  X=X-46.85;
  Serial.print(X);
  Serial.print("C");
  Serial.print("\t");
 
  /**Send command of initiating relative humidity measurement**/
  Wire.beginTransmission(ADDR);                     
  Wire.write(0xE5);
  Wire.endTransmission(); 
 
  /**Read data of relative humidity**/
  Wire.requestFrom(ADDR,2);
  if(Wire.available()<=2);
  {
    Y0 = Wire.read();
    Y2=Y0/100; 
    Y0=Y0%100;
    Y1 = Wire.read();
    Y_out1 = Y2*25600;
    Y_out2 = Y0*256+Y1;
  }
 
  /**Calculate and display relative humidity**/
  Y_out1 = (125*Y_out1)/65536;                     
  Y_out2 = (125*Y_out2)/65536;
  Y = Y_out1+Y_out2;
  Y=Y-6;
  Serial.print(Y);
  Serial.println("%");                     
 delay(300);
 
 
Serial.println(); 
delay(1000);
 
}

 

Output

Open the serial monitor, you should see something like this

Temp    Humidity
23.12C    52.83%

Temp    Humidity
24.04C    53.13%

Temp    Humidity
26.28C    53.83%

Temp    Humidity
27.42C    54.57%

Temp    Humidity
28.27C    55.32%

Temp    Humidity
27.94C    56.11%

 

Links
Humidity Temperature Sensor Breakout Barometric Pressure SI7021 for Arduino GY-21

Share

Sending SHT31 data to thingspeak using an Arduino Uno

In this example we will take the SHt31, read the temperature and humidity and send this data to an online IoT platform, in this case we will use Thingspeak. To achieve this we will need an Arduino Uno with an Ethernet shield fitted and then we will connect the SHt31sensor to this.

We covered the SHT31 in the Arduino and SHT31 module so we will focus on the thingspeak part and arduino code here

You will now need to create a new account at thingspeak – https://thingspeak.com. Once done create a new channel and add two new fields called temperature and humidity. You can see this in a screen capture of my simple channel, notice the ChannelID you will need that in your code later. You can also fill in other fields such as Name, description and there are a few others as well. The key ones are Field1 and Field2 – this effectively is the data you send to thingspeak

thingspeak1

Layout

This layout shows the SHT31 connected to the ethernet shield

ethernet-and-sht31_bb

Code

You will need to install the thingspeak library first of all. 2 options here

In the Arduino IDE, choose Sketch/Include Library/Manage Libraries. Click the ThingSpeak Library from the list, and click the Install button.

— or —

  1. Download the ZIP file from https://github.com/mathworks/thingspeak-arduino to your machine.
  2. In the Arduino IDE, choose Sketch/Include Library/Add Zip Library
  3. Navigate to the ZIP file, and click Open

 

 

Here is the code, its basically a merge of a basic SHt31 example and the thingspeak WriteVoltage example with a lot of the hardware specific code removed – so it only works for an Arduino Uno and an Ethernet shield whereas the WriteVoltaghe example can cater for various pieces of hardware such as an ESP8266, WiFi shield, Arduino Yun

#define USE_ETHERNET_SHIELD
 
#include "ThingSpeak.h"
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SPI.h>
#include <Ethernet.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 
EthernetClient client;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
//change the following for your channel and api key
unsigned long myChannelNumber = 11111;
const char * myWriteAPIKey = "api key in here";
 
void setup() 
{
 //start the serial
 //Serial.begin(9600);
 //network stuff
 Ethernet.begin(mac);
 ThingSpeak.begin(client);
 //start the sensor
 if (! sht31.begin(0x44)) 
 {
 //Serial.println("Couldn't find SHT31");
 while (1) delay(1);
 }
}
 
void loop() 
{
 
 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
 // pieces of information in a channel. Here, we write to field 1 and 2
 ThingSpeak.setField(1,sht31.readTemperature());
 ThingSpeak.setField(2,sht31.readHumidity());
 ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 
 delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}

 

Testing

Navigate to your thingspeak channel and take a look at the private view, all going well you should see a nice graph of data. You can actually get code and embed this on another site. You can also play about with colours and more

Here is a screen capture of mine

thingspeaksht31

 

 

 

Further Steps

There are obviously alternatives to thingspeak which we will look at and we are only scratching the surface using two fields when 8 are available. This example used a specific hardware combination and we will look at others in future articles such as a Wifi shield

 

Links

https://thingspeak.com/

1PCS/LOT SHT31 Temperature & SHT31-D Humidity Sensor module Breakout Weather for Arduino

 

Share

Temperature displayed on Esplora LCD

This example will display the temperature on an LCD connected to an Arduino Esplora

esplora

esplora

Code


#include <Esplora.h>
#include <TFT.h>
#include <SPI.h>

char tempCelsius[3];
char tempFahrenheit[3];

void setup()
{
//setup the LCD
EsploraTFT.begin();
EsploraTFT.background(0,0,0);
EsploraTFT.stroke(255,255,255);
EsploraTFT.setTextSize(2);
EsploraTFT.text("Celsius",0,0);
EsploraTFT.text("Fahrenheit",0,60);
}

void loop()
{
// read the temperature sensor in Celsius, then Fahrenheit:
String celsius = String(Esplora.readTemperature(DEGREES_C));
String fahrenheit = String(Esplora.readTemperature(DEGREES_F));
celsius.toCharArray(tempCelsius, 3);
fahrenheit.toCharArray(tempFahrenheit, 3);
EsploraTFT.stroke(255,255,255);
EsploraTFT.text(tempCelsius, 0, 30);
EsploraTFT.text(tempFahrenheit, 0, 90);
// wait a second before reading again:
delay(1000);
EsploraTFT.stroke(0,0,0);
EsploraTFT.text(tempCelsius, 0, 30);
EsploraTFT.text(tempFahrenheit, 0, 90);
}

 

Links

Atmega32U4 Esplora Game Board Module Leonardo Updated Version FZ0606 Free Shipping

1.8 inch TFT LCD For Arduino Esplora

Share

Temperature and humidity on a LCD4884 shield

In this example we connect a DHT11 sensor our Arduino, again we will display the temperature and this time the humidity as well on our LCD4884 shield. In this example we connected the data pin of the DHT11 to Pin 9. VCC and GND were derived from the shield.

Here’s a picture of the shield used

 

lcd4884

lcd4884

and here is the DHT11 breakout board, this simplifies things as it contains the necessary resistor already fitted otherwise you need to fit a 4k7 between Vcc and the data pin. You can also plug this into either a breadboard or simple connect dupont cables from the shield to the breakout

dht11 breakout

dht11 breakout

Code

You’ll need the LCD4884 library and the you will need the DHT11 library from the DHT library.

Download, extract and copy this to your Arduino libraries folder location


//required header files
#include <Wire.h>
#include "LCD4884.h"
#include <dht11.h>

//DHT11 sensor settings
dht11 DHT;
#define DHT11_PIN 9
int check;

//variables for readings from sensor
float tempC;
char tempstringC[10];
float humid;
char tempHumid[10];

void setup()
{
lcd.LCD_init();
lcd.LCD_clear();
init_MENU();
//debugging functionality
Serial.begin(9600);
Serial.print("DHT11 STATUS – \t");
}

void init_MENU(void)
{
//clear LCD and display initial display
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Temperature", MENU_HIGHLIGHT );
lcd.LCD_write_string(10, 3, "Humidity", MENU_HIGHLIGHT );
}

void display_READINGS(void)
{
//get the temperature and humidity readings
tempC=DHT.temperature;
itoa(tempC,tempstringC,10);
humid=DHT.humidity;
itoa(humid,tempHumid,10);
//display values on LCD
lcd.LCD_write_string(10, 2, tempstringC, MENU_NORMAL);
lcd.LCD_write_string(22, 2, " (c)", MENU_NORMAL);
lcd.LCD_write_string(10, 4, tempHumid, MENU_NORMAL);
lcd.LCD_write_string(22, 4, " %", MENU_NORMAL);
delay(2000);
}

void loop()
{
//read and convert values
check = DHT.read(DHT11_PIN);
//check status - this is useful for debugging the sensor
switch (check)
{
case DHTLIB_OK:
Serial.print("OK\n");
display_READINGS();
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Checksum error", MENU_HIGHLIGHT );
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Timeout error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Timeout error", MENU_HIGHLIGHT );
break;
default:
Serial.print("Unknown error\n");
lcd.LCD_clear();
lcd.LCD_write_string(10, 1, "Unknown error", MENU_HIGHLIGHT );
break;
}

}

Links

DFRobot – Graphic LCD4884 Shield for (For Arduino)

DHT11 Digital Temperature and Humidity Sensor Module For Arduino

Share