EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Sensirion SHT11 Humidity sensor

Started by smithexpedition April 6, 2011
Hello,

I am trying to connect a RCM3400 to the Sensirion SHT11 humidity sensor.

I searched the group archives and found a reference some posted code for a RCM2200, but it was no longer on site.

I was curious if someone might have code for the SHT11?

Phil

Hi Phil,

I don't have code for the Rabbit but I wrote code for an Atmel AVR many
years ago and here is the code you can try to port to the Rabbit. As the
interface on the SHT11 is not true I2C you will have to bit bang it.
Compiler was Codevision AVR.

#define DATA PINB.0
#define SCK PORTB.1
#define DATAO PORTB.0
#define DATAD DDRB.0

HumDig = ReadSensor(3);

Temp = (-40.0 + 0.01 * (float) HumDig) * 10.0;

delay_ms(500);

HumDig = ReadSensor(5);

Humidity = (-4.0 + 0.0405 * (float) HumDig + -0.0000028 * (float)
HumDig);

Then I used the following functions to bit bang the ports.

//--
-
// Read the sensor value. Reg is register to read from

unsigned int ReadSensor(int Reg)
{
unsigned char msb, lsb, crc;

sht_start();
sht_write(Reg);

while(DATA);

msb = sht_read(ACK);
lsb = sht_read(ACK);
crc = sht_read(NOACK);

return(((unsigned short) msb << 8) | (unsigned short) lsb);
}

//--
-

void sht_start()
{
DATAD = 1; // DATA is output

DATAO = 1;
SCK = 0;
SCK = 1;
DATAO = 0;
SCK = 0;
SCK = 1;
DATAO = 1;
SCK = 0;
}

//--
-

char sht_write(unsigned char Byte)
{
unsigned char i, error = 0;

DATAD = 1; // Data is an output
delay_us(DL1);

for(i = 0x80; i > 0; i /= 2)
{
SCK = 0;
if(i & Byte)
{
DATAO = 1;
}
else
{
DATAO = 0;
}
SCK = 1;
}
SCK = 0;
DATAD = 0; // DATA is input
SCK = 1;
error = DATA;
SCK = 0;

return(error);
}

//--
-

unsigned char sht_read(unsigned char ack)
{
unsigned char i, val = 0;

DATAD = 0; // DATA is INPUT

for(i = 0x80; i > 0; i /= 2)
{
SCK = 1;
if(DATA)
{
val = val | i;
}
SCK = 0;
}
DATAD = 1; // DATA is output
DATAO = ! ack;
SCK = 1;
SCK = 0;

return(val);
}
//--
-

void sht_reset()
{
sht_start();
sht_write(0x1E);

delay_ms(100);
}

Good luck.

Dave...
---
Very funny Scotty, now beam down my clothes!!!
---

From: r... [mailto:r...] On
Behalf Of smithexpedition
Sent: 07 April 2011 04:13
To: r...
Subject: [rabbit-semi] Sensirion SHT11 Humidity sensor

Hello,

I am trying to connect a RCM3400 to the Sensirion SHT11 humidity sensor.

I searched the group archives and found a reference some posted code for a
RCM2200, but it was no longer on site.

I was curious if someone might have code for the SHT11?

Phil
Thanks Dave,

I have never communicated with a I2C device before. Thanks for the code. I sure
it will help as struggle through the process.

They say that the SHT11 is not true I2C. Do you know off hand which way it is
not true I2C ?

Phil

________________________________
From: Dave McLaughlin
To: r...
Sent: Thu, April 7, 2011 1:46:58 AM
Subject: RE: [rabbit-semi] Sensirion SHT11 Humidity sensor

Hi Phil,

I don’t have code for the Rabbit but I wrote code for an Atmel AVR many years
ago and here is the code you can try to port to the Rabbit. As the interface on
the SHT11 is not true I2C you will have to bit bang it. Compiler was Codevision
AVR.

#define DATA PINB.0
#define SCK PORTB.1
#define DATAO PORTB.0
#define DATAD DDRB.0

HumDig = ReadSensor(3);

Temp = (-40.0 + 0.01 * (float) HumDig) * 10.0;

delay_ms(500);

HumDig = ReadSensor(5);

Humidity = (-4.0 + 0.0405 * (float) HumDig + -0.0000028 * (float) HumDig);

Then I used the following functions to bit bang the ports.

//---
// Read the sensor value. Reg is register to read from

unsigned int ReadSensor(int Reg)
{
unsigned char msb, lsb, crc;

sht_start();
sht_write(Reg);

while(DATA);

msb = sht_read(ACK);
lsb = sht_read(ACK);
crc = sht_read(NOACK);

return(((unsigned short) msb << 8) | (unsigned short) lsb);
}

//---

void sht_start()
{
DATAD = 1; // DATA is output

DATAO = 1;
SCK = 0;
SCK = 1;
DATAO = 0;
SCK = 0;
SCK = 1;
DATAO = 1;
SCK = 0;
}

//---

char sht_write(unsigned char Byte)
{
unsigned char i, error = 0;

DATAD = 1; // Data is an output
delay_us(DL1);

for(i = 0x80; i > 0; i /= 2)
{
SCK = 0;
if(i & Byte)
{
DATAO = 1;
}
else
{
DATAO = 0;
}
SCK = 1;
}
SCK = 0;
DATAD = 0; // DATA is input
SCK = 1;
error = DATA;
SCK = 0;

return(error);
}

//---

unsigned char sht_read(unsigned char ack)
{
unsigned char i, val = 0;

DATAD = 0; // DATA is INPUT

for(i = 0x80; i > 0; i /= 2)
{
SCK = 1;
if(DATA)
{
val = val | i;
}
SCK = 0;
}
DATAD = 1; // DATA is output
DATAO = ! ack;
SCK = 1;
SCK = 0;

return(val);
}
//---

void sht_reset()
{
sht_start();
sht_write(0x1E);

delay_ms(100);
}

Good luck.

Dave...
---
Very funny Scotty, now beam down my clothes!!!
---

From:r... [mailto:r...] On Behalf
Of smithexpedition
Sent: 07 April 2011 04:13
To: r...
Subject: [rabbit-semi] Sensirion SHT11 Humidity sensor

Hello,

I am trying to connect a RCM3400 to the Sensirion SHT11 humidity sensor.

I searched the group archives and found a reference some posted code for a
RCM2200, but it was no longer on site.

I was curious if someone might have code for the SHT11?

Phil
I've read this post only today because I'm back to the rabbit-semi after a long time.

If you dont need the firmware sample anymore please ignore this post; otherwise check this link:

http://www.areasx.com/index.php?D=1&page=articoli.php&id05

At the bottom of the article (sorry it is only in italian) there is a sample firmware for RCM3700 with SHT75 sensor reading.
We make a wide use of SHT sensors with Rabbit processors so feel free to ask if you need some other information

Bye
Massimo

--------
Massimo Battisti
Area SX srl
E-Mail : b...@areasx.com
WWW : http://www.areasx.com/
Tel : 06.99.33.02.57
Fax : 06.62.20.27.85

--------
Il 08/04/2011 2.16, PJ Moser ha scritto:
> Thanks Dave,
>
> I have never communicated with a I2C device before. Thanks for the code. I sure it will help as struggle through the process.
>
> They say that the SHT11 is not true I2C. Do you know off hand which way it is not true I2C ?
>
> Phil
> ---------------
---
> *From:* Dave McLaughlin
> *To:* r...
> *Sent:* Thu, April 7, 2011 1:46:58 AM
> *Subject:* RE: [rabbit-semi] Sensirion SHT11 Humidity sensor
>
> Hi Phil,
>
> I don’t have code for the Rabbit but I wrote code for an Atmel AVR many years ago and here is the code you can try to port to the Rabbit. As the interface on the SHT11 is not true I2C you will have to bit bang it. Compiler was Codevision AVR.
>
> #define DATAPINB.0
>
> #define SCKPORTB.1
>
> #define DATAO PORTB.0
>
> #define DATAD DDRB.0
>
> HumDig = ReadSensor(3);
>
> Temp = (-40.0 + 0.01 * (float) HumDig) * 10.0;
>
> delay_ms(500);
>
> HumDig = ReadSensor(5);
>
> Humidity = (-4.0 + 0.0405 * (float) HumDig + -0.0000028 * (float) HumDig);
>
> Then I used the following functions to bit bang the ports.
>
> //---
>
> // Read the sensor value. Reg is register to read from
>
> unsigned int ReadSensor(int Reg)
>
> {
>
> unsigned char msb, lsb, crc;
>
> sht_start();
>
> sht_write(Reg);
>
> while(DATA);
>
> msb = sht_read(ACK);
>
> lsb = sht_read(ACK);
>
> crc = sht_read(NOACK);
>
> return(((unsigned short) msb << 8) | (unsigned short) lsb);
>
> }
>
> //---
>
> void sht_start()
>
> {
>
> DATAD = 1;// DATA is output
>
> DATAO = 1;
>
> SCK = 0;
>
> SCK = 1;
>
> DATAO = 0;
>
> SCK = 0;
>
> SCK = 1;
>
> DATAO = 1;
>
> SCK = 0;
>
> }
>
> //---
>
> char sht_write(unsigned char Byte)
>
> {
>
> unsigned char i, error = 0;
>
> DATAD = 1;// Data is an output
>
> delay_us(DL1);
>
> for(i = 0x80; i > 0; i /= 2)
>
> {
>
> SCK = 0;
>
> if(i & Byte)
>
> {
>
> DATAO = 1;
>
> }
>
> else
>
> {
>
> DATAO = 0;
>
> }
>
> SCK = 1;
>
> }
>
> SCK = 0;
>
> DATAD = 0;// DATA is input
>
> SCK = 1;
>
> error = DATA;
>
> SCK = 0;
>
> return(error);
>
> }
>
> //---
>
> unsigned char sht_read(unsigned char ack)
>
> {
>
> unsigned char i, val = 0;
>
> DATAD = 0;// DATA is INPUT
>
> for(i = 0x80; i > 0; i /= 2)
>
> {
>
> SCK = 1;
>
> if(DATA)
>
> {
>
> val = val | i;
>
> }
>
> SCK = 0;
>
> }
>
> DATAD = 1;// DATA is output
>
> DATAO = ! ack;
>
> SCK = 1;
>
> SCK = 0;
>
> return(val);
>
> }
>
> //---
>
> void sht_reset()
>
> {
>
> sht_start();
>
> sht_write(0x1E);
>
> delay_ms(100);
>
> }
>
> Good luck.
>
> Dave...
>
> ---
> Very funny Scotty, now beam down my clothes!!!
> ---
>
> *From:*r... [mailto:r...] *On Behalf Of *smithexpedition
> *Sent:* 07 April 2011 04:13
> *To:* r...
> *Subject:* [rabbit-semi] Sensirion SHT11 Humidity sensor
>
> Hello,
>
> I am trying to connect a RCM3400 to the Sensirion SHT11 humidity sensor.
>
> I searched the group archives and found a reference some posted code for a RCM2200, but it was no longer on site.
>
> I was curious if someone might have code for the SHT11?
>
> Phil
>

The 2024 Embedded Online Conference