I2C interfacing SHT21 sensor


9 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:
https://github.com/raspberrypi/firmware/tree/master/boot
And the 3.2.18 kernel with I2C support:
http://www.bootc.net/archives/2012/05/22/another-new-rpi-kernel/

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
sensors
sht21-i2c-0-40
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?

HTH,
Chris
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:
http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_SHT21_Datasheet_V3.pdf

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:
0x669b6cb2c82080
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
Hello,
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:
https://github.com/raspberrypi/linux/issues/211

Greetings Oliver
Posts: 3
Joined: Wed Feb 06, 2013 10:16 pm