I2C interfacing SHT21 sensor

12 posts
by moesizlag » Thu May 31, 2012 12:40 pm
Hi everyone,

I'm trying to get a Sensirion SHT21 I2C temp/humidity sensor running with the Raspberry Pi.

I use the Debian Image with the latest firmware:
And the 3.2.18 kernel with I2C support:

First: Thanks to all devs for your great work -> I have a DS2482-800 which is working very nice (together with with owfs).

Unfortunately I don't get any data out of the SHT21, here's what I tried.

First try to detect the I2C devices (0x18 is the DS2482-800, 0x40 the Sensirion SHT21:
Code: Select all
i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Load sht21 driver:
Code: Select all
modprobe sht21

Add as new I2C device:
Code: Select all
echo sht21 0x40 > /sys/class/i2c-adapter/i2c-0/new_device

Which results in:
Code: Select all
i2c i2c-0: new_device: Instantiated device sht21 at 0x40
sht21 0-0040: initialized

But unfortunately I don't get an error when trying to query it:
Code: Select all
Adapter: bcm2708_i2c.0
ERROR: Can't get value of subfeature temp1_input: I/O error
temp1:        +0.0°C

So I tried to manually read the user register of the sensor, so first delete I2C device (otherwise we get a "Device or resource busy" error):
Code: Select all
echo 0x40 > /sys/class/i2c-adapter/i2c-0/delete_device

And than:
Code: Select all
i2cget -y 0 0x40 0xe7
Error: Read failed

So this is where I'm currently stuck :-(

Any ideas what to try / what the problem is / what I'm doing wrong?
Posts: 3
Joined: Thu May 31, 2012 11:48 am
by bootc » Thu May 31, 2012 4:01 pm
Do you see anything in /var/log/kernel.log or dmesg that might be of any indication? Do you have a logic analyzer or scope you can connect to the i2c pins to check what's going on?

Posts: 19
Joined: Mon May 28, 2012 7:45 pm
by moesizlag » Thu May 31, 2012 6:03 pm
Unfortunately there's nothing helpful to the problem in dmesg or kern.log.
Is there a way to debug what the I2C driver sends/receives ?

I don't have a logic analyzator yet but hope to get mine in the next few weeks.
I do have a scope but I'm not sure if know how to correctly interpret the signals I see.
Posts: 3
Joined: Thu May 31, 2012 11:48 am
by bootc » Thu May 31, 2012 8:19 pm
Easiest thing to do would be to build a new kernel with CONFIG_I2C_DEBUG_CORE and/or CONFIG_I2C_DEBUG_ALGO and/or CONFIG_I2C_DEBUG_BUS enabled. They should give you a much better idea about what's going on.

If you haven't yet got a logic analyser, I recommend the Open Bench Logic Analyzer from Dangerous Prototypes / Gadget Factory / Seeed Studio - I have one and it's been very useful while working on this driver (and my SPI one)!
Posts: 19
Joined: Mon May 28, 2012 7:45 pm
by johankj » Mon Jun 04, 2012 8:06 pm
the i2c-tools that i have only accepts integer adresses, so have you tried:
Code: Select all
sudo i2cdump -y 0 64
Posts: 10
Joined: Tue Mar 06, 2012 2:07 pm
by moesizlag » Thu Jun 14, 2012 6:04 pm
@johankj: My i2c tools seem to work correctly with hex values, but thanks for your hint.

@Chris: Thanks for your suggestion about the logic analyzer, I finally got a mine and are getting a bit more forward now.

So here are some updates which maybe help to solve the problems with the sensor.
First about the SHT21 Linux driver (made by Urs Fleisch from Sensirion):
As far as I can see the driver uses the "hold master" measurement mode which means after sending the Trigger Temperature or Humidity to the sensor it will block the SCL line as long as the measurement takes.

This is described in the datasheet on page 8 under 5.4:

My first shot would be this confuses the I2C driver?

After playing around some more I finally got the sensor to send some data.

Read User Register:
Code: Select all
i2cget -y 0 0x40 0xe7 b

In my case returns: 0x3A -> 0011 1010

Trigger Temperature measurement (with no hold master)
Code: Select all
i2cset -y 0 0x40 0xf3

Unfortunately I found no way to read the measurements as word.
i2cget expects a "DATA-ADDRESS" when setting the "MODE".
Here's what I got with reading the data byte after byte:
Posts: 3
Joined: Thu May 31, 2012 11:48 am
by soend » Tue Jan 01, 2013 6:37 pm
Hey, sorry to bring this old topic up. But im also trying to get this sensor working on raspberry. I have the sht21 module loaded but i cant find anywhere how i can us it. Do anybody know and give me some example?
Posts: 9
Joined: Sun Jun 17, 2012 7:23 pm
by gcb » Wed Jan 02, 2013 9:41 pm
take a look at http://www.emsystech.de/raspi-sht21/, (page is in german) some sourcecode is available under http://www.emsystech.de/wp-content/uplo ... V3_0_0.zip.
It works here
Posts: 3
Joined: Sun Jul 29, 2012 3:12 pm
by oliverju » Wed Feb 06, 2013 10:21 pm
I have modified the broadcom i2c-driver to provide sht21-support.
The patch can be found here:

Greetings Oliver
Posts: 3
Joined: Wed Feb 06, 2013 10:16 pm
by Anks » Mon Mar 09, 2015 2:15 pm
Hello everyone, I have interfaced Sht21 (Temperature and Humidity sensor) with raspberry pi (B+ version). I want to do data acquisition from this sensor. For this purpose firstly, I have gone through the procedure given by link "https://github.com/jaques/sht21_python" to configure Raspberry pi according to requirement then I used the python code available on link "https://github.com/jaques/sht21_python/blob/master/sht21.py". I have also modified the code by making changes as "device_number=1" and "with SHT21(1) as sht21:", since my raspberry pi is of B+ version and code given by above link is for old version. When I tried to run the code in Geany I am getting the error like
[Error 5] Input/Output error
Error creating connection to i2c. This must be run as root.
I am unable to get rid of this error. Can any one suggest me how to fix this problem. Thanks in advance.
Posts: 4
Joined: Thu Sep 18, 2014 12:03 pm
by drfrank » Tue Mar 24, 2015 12:13 pm
Thanks for all the links, I've got my SHT21 sensor working. Note to the wise - the NOOBS image caused me no end of trouble, but Wheezy was straightforward.
To solve your problem, you need to run sht21.py from the command line like this (I placed sht21.py in a subfolder called python and ran it from there):
pi@raspberrypi ~/python $ python sht21.py
[Errno 13] Permission denied: '/dev/i2c-1'
Error creating connection to i2c. This must be run as root

pi@raspberrypi ~/python $ sudo python sht21.py
Temperature: 20.7502905273
Humidity: 37.083190918
Posts: 1
Joined: Tue Mar 24, 2015 11:56 am
by Prod69 » Wed Apr 29, 2015 11:22 am
I have the exact same problem as Anks.
Noobs install but with the SHT25 sensor.
As far as I can tell the 21 code should also run on the 25.
Posts: 4
Joined: Tue Mar 03, 2015 9:00 am