Home Raspberry PI Raspberry Pi and SI7021 temperature sensor example in C

Raspberry Pi and SI7021 temperature sensor example in C

by iainhendry

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.

The humidity and temperature sensors are factory-calibrated and the calibration data is stored in the on-chip non-volatile memory. This ensures that the sensors are fully interchangeable, with no recalibration or software changes required.

The Si7021 is available in a 3×3 mm DFN package and is reflow solderable. It can be used as a hardware- and software-compatible drop-in upgrade for existing RH/ temperature sensors in 3×3 mm DFN-6 packages, featuring precision sensing over a wider range and lower power consumption. The optional factory-installed cover offers a low profile, convenient means of protecting the sensor during assembly (e.g., reflow soldering) and throughout the life of the product, excluding liquids (hydrophobic/oleophobic) and particulates.

The Si7021 offers an accurate, low-power, factory-calibrated digital solution ideal for measuring humidity, dew-point, and temperature, in applications ranging from HVAC/R and asset tracking to industrial and consumer platforms.

 

Layout

 

 

Code

save the file below as SI7021.c

[codesyntax lang=”cpp”]

 

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SI7021
// This code is designed to work with the SI7021_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SI7021_I2CS#tabs-0-product_tabset-2

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

void main()
{
// Create I2C bus
int file;
char *bus = “/dev/i2c-1”;
if((file = open(bus, O_RDWR)) < 0)
{
printf(“Failed to open the bus. \n”);
exit(1);
}
// Get I2C device, SI7021 I2C address is 0x40(64)
ioctl(file, I2C_SLAVE, 0x40);

// Send humidity measurement command(0xF5)
char config[1] = {0xF5};
write(file, config, 1);
sleep(1);

// Read 2 bytes of humidity data
// humidity msb, humidity lsb
char data[2] = {0};
if(read(file, data, 2) != 2)
{
printf(“Error : Input/output Error \n”);
}
else
{
// Convert the data
float humidity = (((data[0] * 256 + data[1]) * 125.0) / 65536.0) – 6;

// Output data to screen
printf(“Relative Humidity : %.2f RH \n”, humidity);
}

// Send temperature measurement command(0xF3)
config[0] = 0xF3;
write(file, config, 1);
sleep(1);

// Read 2 bytes of temperature data
// temp msb, temp lsb
if(read(file, data, 2) != 2)
{
printf(“Error : Input/output Error \n”);
}
else
{
// Convert the data
float cTemp = (((data[0] * 256 + data[1]) * 175.72) / 65536.0) – 46.85;
float fTemp = cTemp * 1.8 + 32;

// Output data to screen
printf(“Temperature in Celsius : %.2f C \n”, cTemp);
printf(“Temperature in Fahrenheit : %.2f F \n”, fTemp);
}
}

[/codesyntax]

compile this like the following

gcc SI7021.c -o SI7021 -lwiringPi - lm

 

run this like this

sudo ./SI7021

 

Output

You should see something like this in the terminal

si7021 output

si7021 output

 

Link

https://www.silabs.com/documents/public/data-sheets/Si7021-A20.pdf

Industrial High Precision Si7021 Humidity Sensor with I2C Interface for Arduino

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More