Smart Humidity and Temperature sensor TH02 Library

Playing around with Hoperf RFM12B and RFM69 for my wireless sensors, I discovered they also have one interesting sensor for temperature and humidity called TH02. Just look the picture below, this sensor is so small

th02 pinout

th02 sensor pinout

Of course I already looked and tried some other humidity sensors such as DHT11 or DHT22 but from my point of view they are too big and not so accurate, slow conversion time and does not have really low power. Moreover, protocol to read them need to be interrupt free and strict timing for controlling the pins.

I found some other more accurate but far more expensive. Well the famous “you’ll have what you pay for” applied for humidity sensors until I discovered this one.

TH02 is an I2C sensor, as I’m using a lot of I2C devices such as OLED described in previous post it’s not really a problem. And what I like on this sensor is :

  • it’s small,
  • it’s almost accurate for the price (0.5°C, 4.5% RH)
  • it is not very expensive (less than $5)
  • it is numeric and factory calibrated
  • it has really low power consumption (about 320µA during conversion, 150µA in idle mode)
  • it has a fast conversion time (40ms max)
  • it’s bread board friendly or easy PCB soldering
  • it has a fast response time (15ms max after power up) so powering up, and doing a temperature conversion AND a humidy conversion is typical 63ms and maximum 76ms
  • it can use I2C bus until 400 KHz (tested) and has 75 Ko integrated pull up resistor

And for all of this, I think it is an excellent humidity and temperature sensor. This is why I ordered some and played with them without any problem.

Arduino Library

While I was testing them with my sensors, I created a Arduino library to use it. You can find the library and sample code on my github in the TH02 repository


List of features implemented in this library:

  • Reading device ID
  • Starting a Temperature / Humidity conversion in normal/fast mode
  • Keep in memory last Temperature and Humidity with 2 digits precision
  • Calculate Linear and temperature compensation for Humidity
  • Waiting end of conversion with time out
  • return data as int16 * 10 rounded to avoid float in your program

Usage and installation are described on README.md file. As the standard Arduino I2C library (Wire) can’t do repeated start, I’m using the excellent I2C library written by Wayne Truchsess. You fill find excellent documentation about I2C and library comparison on the DSS Circuit site . I strongly suggest to use this library in all you project using I2C as master. Edit of April 2015, The library now use the Arduino Wire library. This add compatibility with other I2C libraries already using the wire library.

Here is a dump of the sample sketch GetTempHum provided with my library running :

See this sketch source code on Github TH02 library. The test has been done cabling the sensor to custom Moteino USB board like the following picture

TH02 connected to Moteino

TH02 connected to custom Moteino USB using I2C


For any explanation see TH02 sensor information and datasheet on Hoperf site. This is the documentation I used to create the library.

You can buy sensor on Anarduino site for $4.55





  1. Pingback: Sensor Humidity
  2. Hi, I was super happy to find your library for the TH02. I just got a few of the sensors and I’m testing them right now. However, I do get a compile error indicating something going wrong with the TH02 lib.. but I’m not sure. Do you see anything out of the ordinary here…? Other I2C sensors I have works fine using the Wire lib. Grateful for all you work and hoping to be able to use it!

    Here is a verbose output from the compiler, compiling the example from your library, the error is in bold at the bottom:

    Arduino: 1.5.6-r2 (Mac OS X), Board: “Arduino Uno”

    Build options changed, rebuilding all
    Using library Wire in folder: /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire (legacy)
    Using library TH02 in folder: /Users/Bo/Documents/Arduino/libraries/TH02 (legacy)

    /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire -I/Users/Johannes/Documents/Arduino/libraries/TH02 /var/folders/aE/aE8AHBi2FKulZG-bTdnaYU+++TI/-Tmp-/build1193856155966205342.tmp/GetTempHum_test.cpp -o /var/folders/aE/aE8AHBi2FKulZG-bTdnaYU+++TI/-Tmp-/build1193856155966205342.tmp/GetTempHum_test.cpp.o
    /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire -I/Users/Johannes/Documents/Arduino/libraries/TH02 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/utility /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/Wire.cpp -o /var/folders/aE/aE8AHBi2FKulZG-bTdnaYU+++TI/-Tmp-/build1193856155966205342.tmp/Wire/Wire.cpp.o
    /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc -c -g -Os -w -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire -I/Users/Johannes/Documents/Arduino/libraries/TH02 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/utility /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/utility/twi.c -o /var/folders/aE/aE8AHBi2FKulZG-bTdnaYU+++TI/-Tmp-/build1193856155966205342.tmp/Wire/utility/twi.c.o
    /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire -I/Users/Bo/Documents/Arduino/libraries/TH02 -I/Users/Bo/Documents/Arduino/libraries/TH02/utility /Users/Bo/Documents/Arduino/libraries/TH02/TH02.cpp -o /var/folders/aE/aE8AHBi2FKulZG-bTdnaYU+++TI/-Tmp-/build1193856155966205342.tmp/TH02/TH02.cpp.o
    /Users/Bo/Documents/Arduino/libraries/TH02/TH02.cpp: In member function ‘uint8_t TH02::readRegister(uint8_t, uint8_t*)’:
    /Users/Bo/Documents/Arduino/libraries/TH02/TH02.cpp:85: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
    /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/Wire.h:58: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)
    /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire/Wire.h:56: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)


    • Hi Arne
      Would you mind trying to compile with the latest Arduino IDE 1.6.3 to see if there is any difference ?
      I’m using this version under Windows and it’s working fine.

      Anyway you can also keep your version of IDE but you need to change line in th02.cpp
      Line 85 (your error) by casting parameters like as follow,

      change Wire.requestFrom( _address, 1); by Wire.requestFrom( (uint8_t) _address, (uint8_t) 1);
      If it works I will update the repo.


  3. I have some Problems to use it with other I2C devices.

    If i use it alone on the i2c there is no problem, but when i use more the one i2c device the th02 block the other.

    If i remove the th02 all works fine.

    • Hi,
      You found the limit oh TH02 chip. I discovered that, yes you can’t use it with other I2C device except powering TH02 of during talk to other devices. Well it’s written in the datasheet, but I did not pay attention to this information until I had same problem as your. Tried to get it working on ULPNode with a TSL2861 (see this post), no way, this device claim as an I2C device, it’s just not, so I decided to switch to SI7021 sensor but this one seems now had it price raised twice on each reseller, no luck. !!!

Comments on this topic in community Forums.