Home Raspberry PI Raspberry Pi and SHT31 C example

Raspberry Pi and SHT31 C example

by iainhendry

In this example we will connect a SHT31 module to a Raspberry Pi

The new digital SHT3x humidity sensor series takes sensor technology to a new level. As the successor of the SHT2x series it is determined to set the next industry standard in humidity sensing. The SHT3x humidity sensor series consists of a low-cost version with the SHT30 humidity sensor, a standard version with the SHT31 humidity sensor, and a high-end version with the SHT35 humidity sensor. The SHT3x humidity sensor series combines multiple functions and various interfaces (I2C, analog voltage output) with a applications-friendly, very wide operating voltage range (2.4 to 5.5 V). The SHT3x humidity sensor is available in both large and small volumes.

The SHT3x builds on a completely new and optimized CMOSens® chip, which allows for increased reliability and improved accuracy specifications. The SHT3x offers a range of new features, such as enhanced signal processing, two distinctive and user-selectable I2C addresses, an alert mode with programmable humidity and temperature limits, and communication speeds of up to 1 MHz.

 

Features

Size 2.5 x 2.5 x 0.9 mm
Output I²C, Voltage Out
Supply voltage range 2.4 to 5.5 V
Energy consumption 4.8µW (at 2.4 V, low repeatability, 1 measurement / s)
RH operating range 0 – 100% RH
T operating range -40 to +125°C (-40 to +257°F)
RH response time 8 sec (tau63%)

Layout

Here is a layout for this device

pi and sht31

pi and sht31

Code

save the file below as SHT31.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.
// SHT31
// This code is designed to work with the SHT31_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SHT31_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, SHT31 I2C address is 0x44(68)
ioctl(file, I2C_SLAVE, 0x44);

// Send high repeatability measurement command
// Command msb, command lsb(0x2C, 0x06)
char config[2] = {0};
config[0] = 0x2C;
config[1] = 0x06;
write(file, config, 2);
sleep(1);

// Read 6 bytes of data
// temp msb, temp lsb, temp CRC, humidity msb, humidity lsb, humidity CRC
char data[6] = {0};
if(read(file, data, 6) != 6)
{
printf(“Error : Input/output Error \n”);
}
else
{
// Convert the data
double cTemp = (((data[0] * 256) + data[1]) * 175.0) / 65535.0 – 45.0;
double fTemp = (((data[0] * 256) + data[1]) * 315.0) / 65535.0 – 49.0;
double humidity = (((data[3] * 256) + data[4])) * 100.0 / 65535.0;

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

 

[/codesyntax]

compile this like the following

gcc SHT31.c -o SHT31 -lwiringPi - lm

 

run this like this

sudo ./SHT31

Output

This is what you should see in the terminal

sht31 output

sht31 output

Link

https://www.sensirion.com/en/download-center/humidity-sensors/digital-humidity-sensors-for-various-applications/

1PCS/LOT SHT31 Temperature & SHT31-D Humidity Sensor module Breakout Weather 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