:: Sensirion Humidity and Temperature Sensors ::

Note: Page under construction. Feel free to add to it!

This page is dedicated to the care and feeding of the Sensirion SHT1x and SHT7x series humidity and temperature sensors. More Information regarding the new SHT21 sensors will be added at a later date (after I've gained experience with my forthcoming sample!).


Navigation


Helpful Hints

  • Hint: If the measured temperature and humidity has very bad resolution, read this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1229870415/0
    • >> Version 2.0 of the library added consistent setting of the internal pullup for the data pin (on by default). Previous versions left the internal pullup state undefined, i.e., dependent on the value of the last bit transmitted. A #define in Sensirion.h can be used to modify pullup usage. Note: the Sensirion data sheet recommends using a 10K ohm pullup - the Arduino internal pullup ranges from 20K to 50K ohms.

  • The CRC (cyclical redundancy check) functionality and enhanced error reporting included in the latest version of the library can be used to help diagnose communication problems with the sensor. See the NonBlocking example included with the library for an approach to taking advantage of these features.

  • Self-heating: Sensirion recommends that the sensor not be active more than 10% of the time to avoid erroneous temperature measurements due to self-heating. The time required to calculate / measure a reading appears to vary by sensor.
    • >> The [SHT1x / SHT7x series?] sensors takes up to 320 msec to perform a 14-bit temperature measurement and 80 msec to perform a 12-bit humidity measurement. That's 400 msec combined so include at least a 3.6 second delay between pairs of temperature & humidity measurements. Low resolution mode reduces these numbers to 80 msec for temperature (12-bit) and 20 msec for humidity (8-bit), allowing the delay to be reduced to 1 second. Note: The SHT1x library currently measures both temperature and humidity in "readHumidity()" (raw humidity measurements must be temperature compensated) so the delay should be at least 6.5 seconds when using this library.
    • >>The Sensiron SHT21 series can typically complete a temperature and humidity reading in less than 0.1 seconds, allowing a reading every second. A drop-in substitute for the SHT21, the HTU21 from Measurement Specialties, is even faster and costs less.

  • Non-blocking calls: As noted above, the sensor can take quite a while to perform a measurement. Traditional blocking calls send a command to the sensor and then spin, waiting for the measurement to complete. This means your sketch can spend hundreds of milliseconds doing nothing! Non-blocking calls return control to your sketch immediately after the command is sent but require the sketch to periodically check for completion. The example code on this page and the NonBlocking example included in the Sensirion library show ways to take advantage of this approach.


Libraries and Example Code

Sensirion

Version 2.0 - 10Dec2010

Rewritten version of the library that includes extensive changes for robustness, reduced code size, plus a couple of new features while maintaining backwards compatibility with previous versions.

  • Added CRC checking (enabled by default via #define in Sensirion.h)
  • Added consistent setting of the data pin internal pullup (was previously undefined - depended on last bit transmitted)
  • Improved error reporting capability
  • Added status register read function; expanded write function to cover all settable bits.

Download library: Sensirion_10Dec2010.zip

Note: to make this library work with Arduino >= 1.0, you need to make two small changes to Sensirion.cpp:

  • uncomment the line #include "WConstants.h" (by prepending '//')
  • add a line #include "Arduino.h" after the block extern "C" { ... }", i.e. immediately before the line that includes Sensirion.h

Example code:

  1. /*
  2.  * Example code for SHT1x or SHT7x sensors demonstrating blocking calls
  3.  * for temperature and humidity measurement in the setup routine and
  4.  * non-blocking calls in the main loop.  The pin 13 LED is flashed as a
  5.  * background task while temperature and humidity measurements are made.
  6.  */
  7.  
  8. #include <Sensirion.h>
  9.  
  10. const uint8_t dataPin =  9;              // SHT serial data
  11. const uint8_t sclkPin =  8;              // SHT serial clock
  12. const uint8_t ledPin  = 13;              // Arduino built-in LED
  13. const uint32_t TRHSTEP   = 5000UL;       // Sensor query period
  14. const uint32_t BLINKSTEP =  250UL;       // LED blink period
  15.  
  16. Sensirion sht = Sensirion(dataPin, sclkPin);
  17.  
  18. uint16_t rawData;
  19. float temperature;
  20. float humidity;
  21. float dewpoint;
  22.  
  23. byte ledState = 0;
  24. byte measActive = false;
  25. byte measType = TEMP;
  26.  
  27. unsigned long trhMillis = 0;             // Time interval tracking
  28. unsigned long blinkMillis = 0;
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.   pinMode(ledPin, OUTPUT);
  33.   delay(15);                             // Wait >= 11 ms before first cmd
  34. // Demonstrate blocking calls
  35.   sht.measTemp(&rawData);                // sht.meas(TEMP, &rawData, BLOCK)
  36.   temperature = sht.calcTemp(rawData);
  37.   sht.measHumi(&rawData);                // sht.meas(HUMI, &rawData, BLOCK)
  38.   humidity = sht.calcHumi(rawData, temperature);
  39.   dewpoint = sht.calcDewpoint(humidity, temperature);
  40.   logData();
  41. }
  42.  
  43. void loop() {
  44.   unsigned long curMillis = millis();          // Get current time
  45.  
  46.   // Rapidly blink LED.  Blocking calls take too long to allow this.
  47.   if (curMillis - blinkMillis >= BLINKSTEP) {  // Time to toggle the LED state?
  48.     ledState ^= 1;
  49.     digitalWrite(ledPin, ledState);
  50.     blinkMillis = curMillis;
  51.   }
  52.  
  53.   // Demonstrate non-blocking calls
  54.   if (curMillis - trhMillis >= TRHSTEP) {      // Time for new measurements?
  55.     measActive = true;
  56.     measType = TEMP;
  57.     sht.meas(TEMP, &rawData, NONBLOCK);        // Start temp measurement
  58.     trhMillis = curMillis;
  59.   }
  60.   if (measActive && sht.measRdy()) {           // Note: no error checking
  61.     if (measType == TEMP) {                    // Process temp or humi?
  62.       measType = HUMI;
  63.       temperature = sht.calcTemp(rawData);     // Convert raw sensor data
  64.       sht.meas(HUMI, &rawData, NONBLOCK);      // Start humidity measurement
  65.     } else {
  66.       measActive = false;
  67.       humidity = sht.calcHumi(rawData, temperature); // Convert raw sensor data
  68.       dewpoint = sht.calcDewpoint(humidity, temperature);
  69.       logData();
  70.     }
  71.   }
  72. }
  73.  
  74. void logData() {
  75.   Serial.print("Temperature = ");   Serial.print(temperature);
  76.   Serial.print(" C, Humidity = ");  Serial.print(humidity);
  77.   Serial.print(" %, Dewpoint = ");  Serial.print(dewpoint);
  78.   Serial.println(" C");
  79. }

Version 1.1 - 04Aug10

This version of the library includes several enhancements over the original. It was a goal of this rewrite to bring in new functionality while modifying the original code as little as possible to avoid impacting existing sketches that use this library. Enhancements include:

  • Added separate functions for temperature and humidity measurements, each with blocking vs. non-blocking flags
  • Added function to write the sensor Status Register to enable selecting the sensor precision (ie, 14-bit/12-bit Temp/RH vs. 12-bit/8-bit Temp/RH)
  • Dewpoint calculation is now a public function
  • Updated temperature, humidity, and dewpoint equations to match latest recommendations from Sensirion (V4)

Download library: Sensirion_04Aug10.zip

Original version - 28Nov08

Version of the Sensirion library first available through the Interfacing With Hardware page.

Download library: http://www.target23.de/data/Sensirion_20081128.zip

SHT1x

Library available from Practical Arduino. No commentary yet due to my lack of experience with this library. Feel free to contribute comments to this page.

Download library: http://github.com/practicalarduino/SHT1x

Share