Home Raspberry PI Raspberry Pi and SHT31 sensor example in C

Raspberry Pi and SHT31 sensor example in C

SHT31 is the next generation of Sensirion’s temperature and humidity sensors. It builds on a new CMOSens® sensor chip that is at the heart of Sensirion’s new humidity and temperature platform.

The SHT3x-DIS has increased intelligence, reliability and improved accuracy specifications compared to its predecessor. Its functionality includes enhanced signal processing, two distinctive and user selectable I2C addresses and communication speeds of up to 1 MHz. The DFN package has a footprint of 2.5 x 2.5 mm2 while keeping a height of 0.9 mm.

This allows for integration of the SHT3x-DIS into a great variety of applications.


Size 2.5 x 2.5 x 0.9 mm
Output I²C, Voltage Out
Supply voltage range 2.15 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%)


I bought the following module

Parts List


Part Link
Raspberry PI 4 Latest Raspberry Pi 4 Model B with 1/2/4GB RAM
SHT31 SHT31 Temperature & Humidity Sensor module Breakout Weather for Arduino
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire


If you’re using an Raspberry Pi simply connect the VIN pin to the 3v3 voltage pin, GND to ground, SCL1 (D5) to I2C Clock (Analog 5) and SDA1 (D3) to I2C Data (Analog 4).

Here is a layout


Save the following as SHT31.c

[codesyntax lang=”cpp”]

#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");
	// 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);

	// 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");
	// 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);



Compile the c program.

$>gcc MMA8452Q.c -o MMA8452Q

Run the c program.


This is what you should see

sht31 output

sht31 output



You may also like