Reading data from a Sensirion SHT1x with a Raspberry Pi

Note: An updated version of my code is available at /2012/11/update-reading-data-from-a-sensirion-sht1x-with-a-raspberry-pi/

The Sensirion SHT1x range of sensors provide a rather convenient way to accurately measure Temperature and Relative Humidity.

They aren’t the cheapest sensors as they typically sell for around $40, but they seem very accurate. I obtained a Sensirion SHT11 a while back as a free sample. I’d managed to get it working with an AVR ATMEGA328P for a planned project to build an Incubator, but now I wanted to read data from it using the GPIO of a Raspberry Pi.

I decided to keep the SHT11 allocated to my Incubator and bought an SHT15 for my weather station. The sensors are very similar. The data sheet shows that the SHT11 has an accuracy of ±3% Relative Humidity and ±0.4° Centigrade while the SHT15 is slightly better at ±2%RH and ±0.3°C. The communication interface is identical so no problems changing sensors in the future.

Before you can use my code sample, you’ll need to get the latest BCM2835 Raspberry Pi GPIO Library from http://www.open.com.au/mikem/bcm2835/ and wire up the sensor to the Raspberry Pi GPIO port.

SHT1x Pin Connection
GND Connected to GPIO Port P1-06 (Ground)
DATA Connected via a 10k pullup resistor to GPIO Port P1-01 (3V3 Power)
DATA Connected to GPIO Port P1-18 (GPIO 24)
SCK Connected to GPIO Port P1-16 (GPIO 23)
VDD Connected to GPIO Port P1-01 (3V3 Power)

Note that if you’re using a Spark Fun SHT15 Breakout board, the breakout board already has pull up resistors, so you don’t need the one listed in the table above.

http://elinux.org/RPi_Low-level_peripherals is very useful if you need a pin diagram of the GPIO header or other information on the Raspberry Pi GPIO.

Here’s the source code you’re looking for: RaspberryPi_SHT1x.zip

Here’s the source of testSHT1x.c. (RPi_SHT1x.c and RPi_SHT1x.h can be found in RaspberryPi_SHT1x.zip)

// Compile with: gcc -o testSHT1x ./../bcm2835-1.3/src/bcm2835.c ./RPi_SHT1x.c testSHT1x.c

/*
Raspberry Pi SHT1x communication library.
By:      John Burns (www.john.geek.nz)
Date:    14 August 2012
License: CC BY-SA v3.0 - http://creativecommons.org/licenses/by-sa/3.0/

This is a derivative work based on
	Name: Nice Guy SHT11 library
	By: Daesung Kim
	Date: 04/04/2011
	Source: http://www.theniceguy.net/2722

Dependencies:
	BCM2835 Raspberry Pi GPIO Library - http://www.open.com.au/mikem/bcm2835/

Sensor:
	Sensirion SHT11 Temperature and Humidity Sensor interfaced to Raspberry Pi GPIO port

SHT pins:
	1. GND  - Connected to GPIO Port P1-06 (Ground)
	2. DATA - Connected via a 10k pullup resistor to GPIO Port P1-01 (3V3 Power)
	2. DATA - Connected to GPIO Port P1-18 (GPIO 24)
	3. SCK  - Connected to GPIO Port P1-16 (GPIO 23)
	4. VDD  - Connected to GPIO Port P1-01 (3V3 Power)

Note:
	GPIO Pins can be changed in the Defines of RPi_SHT1x.h
*/

#include 
#include 
#include "RPi_SHT1x.h"
#include 

void printTempAndHumidity(void)
{
	unsigned char noError = 1;  
	value humi_val,temp_val;
	
	
	// Wait at least 11ms after power-up (chapter 3.1)
	delay(20); 
	
	// Set up the SHT1x Data and Clock Pins
	SHT1x_InitPins();
	
	// Reset the SHT1x
	SHT1x_Reset();
	
	// Request Temperature measurement
	noError = SHT1x_Measure_Start( SHT1xMeaT );
	if (!noError) {
		return;
		}
		
	// Read Temperature measurement
	noError = SHT1x_Get_Measure_Value( (unsigned short int*) &temp_val.i );
	if (!noError) {
		return;
		}

	// Request Humidity Measurement
	noError = SHT1x_Measure_Start( SHT1xMeaRh );
	if (!noError) {
		return;
		}
		
	// Read Humidity measurement
	noError = SHT1x_Get_Measure_Value( (unsigned short int*) &humi_val.i );
	if (!noError) {
		return;
		}

	// Convert intergers to float and calculate true values
	temp_val.f = (float)temp_val.i;
	humi_val.f = (float)humi_val.i;
	
	// Calculate Temperature and Humidity
	SHT1x_Calc(&humi_val.f, &temp_val.f);

	//Print the Temperature to the console
	printf("Temperature: %0.2f%cC\n",temp_val.f,0x00B0);

	//Print the Humidity to the console
	printf("Humidity: %0.2f%%\n",humi_val.f);

}

int main ()
{
	//Initialise the Raspberry Pi GPIO
	if(!bcm2835_init())
		return 1;
	
	printTempAndHumidity();

	return 1;
}

You May Also Like

About the Author: John

9 Comments

  1. Thank you! I’m planning to build a moisturize sensor “network” for my attic based on RPi and a few SHT11’s. How would I go about adding a few sensors on a single serial bus? Can I target each sensor individually?

  2. Hi John, great work.
    Though I haven’t test the codes, I found tiny problem at SHT1x_Calc function on RPi_SHT1x.c.
    The sensor is driven at 3.3V on R-PI, but you used a conversion factor “-40” for 5.0V drive:
    t_C = t*0.01 – 40;

    Unfortunately, we can’t find the exact factor at 3.3V on SHT data sheet.
    The factors on data sheet are at 5.0V, 4.0V, 3.5V, 3.0V, 2.5V.
    I calculated the factor at 3.3V with linear regression method using the five factors and got the value as “-39.65”.

    So the transformation code should be:
    t_C = t*0.01 – 39.65; // at 3.3V drive on R-PI

    You will get more precise temperature and humid values with the correction.

  3. Hi John,

    I am wondering if the Linux native driver would not be a better solution. As you probably already know the Linux has driver for SHT15 (actually shall work with SHT11) inside hwmon subsystem (drivers/hwmon/sht15.c) as well another for SHT21 (drivers/hwmon/sht21.c).

    I myself had no experience with that driver because could not put my hands on as SHT15 yet. But since it has been long time since it is mainstream I believe it work properly. For sure you would have to recompile the RPi kernel to enable support but it shall present a better performance shall interface to userspace very easily.

    Regards,
    Benito

  4. Hi,
    thank you for you tutorial.
    Pardon my noobness, But, do you have a schema to explain with an image how to mount the sensor and connect it to the raspberry pi ?
    Thank you very much

  5. Hi,
    may I know how we going to run the c code for our raspberry pi? Since we running the raspberry in root, is it we need the .py file to run in lxterminal?

Leave a Reply to John Burns Cancel reply

Your email address will not be published. Required fields are marked *