Update – Reading data from a Sensirion SHT1x with a Raspberry Pi

After a few comments regarding my code to read data from a Sensirion SHT1x with a Raspberry Pi, I’ve got some updated code.

Please see my previous post for general information, but use the code here.

The list of changes are:

  • Added Dewpoint calculation from Page 9 of Datasheet
  • Updated Humidity calculation Coefficients to recommended (12 bit) figures from Page 8 of Datasheet
  • Updated Temperature calculation Coefficients to recommended (3.3 Volt Interpolated) figures from Page 9 of Datasheet

Any references to the datasheet refer to the Version 5, Dec 2011 datasheet found on Sensirions website.

Also note that the bcm2835 GPIO library has been updated and is now version 1.8 – Get the updated version from

Here’s the source code you’re looking for:

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

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

Raspberry Pi SHT1x communication library.
By:      John Burns (
Date:    01 November 2012
License: CC BY-SA v3.0 -

This is a derivative work based on
	Name: Nice Guy SHT11 library
	By: Daesung Kim
	Date: 04/04/2011
	License: Unknown - Attempts have been made to contact the author

	BCM2835 Raspberry Pi GPIO Library -

	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)

	GPIO Pins can be changed in the Defines of RPi_SHT1x.h

#include <bcm2835.h>
#include <stdio.h>
#include "RPi_SHT1x.h"

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

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

	// 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);
	//Calculate and print the Dew Point
	float fDewPoint;
	SHT1x_CalcDewpoint(humi_val.f ,temp_val.f, &fDewPoint);
	printf("Dewpoint: %0.2f%cC\n",fDewPoint,0x00B0);


int main ()
	//Initialise the Raspberry Pi GPIO
		return 1;

	return 1;


  • [...] out this nice example of using the GPIO gcc libraries on a Pi (for those of us who want a break from writing Python code) from John.Geek.NZ who has been exploring [...]

    Pingback | 3 November 2012
  • [...] ผ่านทาง [...]

    Pingback | 12 November 2012
  • tasasaki

    Hi John

    I tried to compile the codes with latest bcm2835 libralies but failed with the following errors:

    $ gcc -lm -o testSHT1x ./../bcm2835-1.12/src/bcm2835.c ./RPi_SHT1x.c ./testSHT1x.c
    /tmp/ccnhXD74.o: In function `bcm2835_delayMicroseconds’:
    bcm2835.c:(.text+0xc38): undefined reference to `clock_gettime’
    bcm2835.c:(.text+0xc90): undefined reference to `clock_gettime’
    collect2: ld returned 1 exit status

    According to the bcm library page, the author decided to change the name of delay functions to prevent collisions with wiringPi from version 1.10.
    I tried to change the name of the delay functions on your source codes but with no luck…

    Comment | 17 November 2012
  • tasasaki

    I cracked my problem.
    After installing a bcm2835 GPIO library(ver. 1.14), I compiled the SHT1x program like this:

    $ gcc -o testSHT1x -l rt testSHT1x.c RPi_SHT1x.c -l bcm2835 -lm

    The SHT1x program worked like a charm. Thanx.
    Without “-l rt”, previous error “undefined reference to `clock_gettime’” occurs.

    And I changed the definition
    #define SHT1x_DELAY delayMicroseconds(2)
    #define SHT1x_DELAY bcm2835_delayMicroseconds(2)
    just to be safe.

    Comment | 25 December 2012
  • Fabio

    I compiled with gcc -lm -o testSHT1x /root/lib/bcm2835-1.22/src/bcm2835.c ./RPi_SHT1x.c testSHT1x.c a in the directory a new file testSHT1x was created. Now, How can i get temperature and humidity infos?

    Comment | 10 March 2013
  • Derrick

    I compiled with gcc -lm -o testSHT1x /root/lib/bcm2835-1.22/src/bcm2835.c ./RPi_SHT1x.c testSHT1x.c a in the directory a new file testSHT1x was created. So how I going to get the result of temperature and humidity information?

    Thank you.

    Comment | 26 May 2013

RSS feed for comments on this post. TrackBack URL

Leave a comment