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 http://www.open.com.au/mikem/bcm2835/.

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

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

// 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 (www.john.geek.nz)
Date:    01 November 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
	License: Unknown - Attempts have been made to contact the author

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"

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);
	//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
	if(!bcm2835_init())
		return 1;
	
	printTempAndHumidity();

	return 1;
}

You May Also Like

About the Author: John

10 Comments

  1. 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…

  2. 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)
    to
    #define SHT1x_DELAY bcm2835_delayMicroseconds(2)
    just to be safe.

  3. Hi.
    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?
    Thanks!

  4. Hi.
    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.

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

  6. Hi,

    I managed to compile the Test-Code but the values just wont show up.
    When i execute the compiled code nothing happens.

  7. In just found out my problem.

    A short circiut between CLK and DATA cased the programm to enter error mode and shut down without giving the values.

Leave a Reply

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