CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

SHT21 - Wrong Humidity value [SOLVED]
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

SHT21 - Wrong Humidity value [SOLVED]
PostPosted: Thu Jun 30, 2016 1:26 am     Reply with quote

Hi All,

I was following the thread here http://www.ccsinfo.com/forum/viewtopic.php?t=45751&highlight=sht21 for connecting my SHT21

I was able to make the code work but there is huge mismatch in the humidity. Humidity is shown 120% when the humidty is actually 80%.

Can someone help me out with it? I tested 2 SHT21 sensors both of them are giving me almost identical readings.

Thanks in Advance
Devil
_________________
Regards,
Devil


Last edited by devilfrmheaven on Wed Jul 06, 2016 4:35 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 12630

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 1:45 am     Reply with quote

Have you made the PCM_programmer fix to all the checksum reading lines?.

There is also a fault in the code as posted. Once he has read lsbnum, this needs to be 'anded' with 0xFC.
So:
Code:

void sht21_humid(float &humidum1)
{
   int8 msbhum, lsbhum;
   int16 humidum;

   i2c_start();
   i2c_write(sht21);
   i2c_write(humidhold);
   i2c_start();
   i2c_write(read);
   //delay_ms(25);
   msbhum = i2c_read();
   lsbhum = i2c_read();
   checksum = i2c_read(0);
   i2c_stop();

   lsbnum &= 0XFC;
   humidum = make16(msbhum,lsbhum);
 
   humidum1= (-6 + (0.00190734*humidum);
   SoftResett();
   //return humidum1;
}


Reason is that there are two extra bits sent on every read command. Top right of page 8 of the data sheet:
"For both modes, since the maximum resolution of a
measurement is 14 bit, the two last least significant bits
(LSBs, bits 43 and 44) are used for transmitting status
information. Bit 1 of the two LSBs indicates the
measurement type (‘0’: temperature, ‘1’ humidity). Bit 0 is
currently not assigned."

These need to be removed, or there will be a small error permanently in the humidity reading...

This is more efficient as well (since I pre-solve 125/65536), and a single multiplication is more efficient than using a division and a multiplication. Also since I am multiplying by a 'float', I don't have to separately convert the integer value to float, it is done as part of the multiplication.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Yes - it was handled
PostPosted: Thu Jun 30, 2016 2:06 am     Reply with quote

Yes MrT,

I had read through the post and have added the i2c_read(0);

I have also added your fix but I still do get a 19% more humidity value from the sensor.
_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 12630

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 3:50 am     Reply with quote

What are you using to make the comparison with?.
You do understand that the SHT21, reads relative humidity, not absolute humidity?.
Also, that it is not rated to operate above 80%RH.
"Normal operating range: 0-80%RH, beyond this limit sensor may read a
reversible offset with slow kinetics (+3%RH after 60h at humidity >80%RH). For more details please see Section 1.1 of the Users Guide."
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Yes - Relative humidity
PostPosted: Thu Jun 30, 2016 7:50 am     Reply with quote

I do have 3 hygrometers with me. All 3 of them shows humidity around 68% ish but the SHT21 module currently shows 94.50% Smile

Looks definitly like the problem of the driver.

By the way is the -6 below for adjustment purposes?

Quote:
humidum1= (-6 + (0.00190734*humidum);


The RH will be correct if I remove -6 and replace it with -26 Smile

I do not think its a good method but...
_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 12630

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 8:45 am     Reply with quote

No, it is the value specified in the data sheet.

I have to warn you to go back and get some of the sensors from a 'name' supplier. Unfortunately, there are quite a large number of these on the market, that were a large batch of manufacturers rejects, that were lifted from a dump, have been badged, and sold on places like ebay.
Also be careful when soldering them, the temperature specs are quite strict.

The proper units are laser trimmed as part of the last stage of manufacture, and at 20C, should be within +/-3% at up to 80%RH.

"It is of great importance to understand that a humidity
sensor is not a normal electronic component and needs to
be handled with care. Chemical vapors at high
concentration in combination with long exposure times
may offset the sensor reading.
For this reason it is recommended to store the sensors in
original packaging including the sealed ESD bag at
following conditions: Temperature shall be in the range of
10°C – 50°C and humidity at 20 – 60%RH (sensors that
are not stored in ESD bags). For sensors that have been
removed from the original packaging we recommend to
store them in ESD bags made of metal-in PE-HD11.
In manufacturing and transport the sensors shall be
prevented of high concentration of chemical solvents and
long exposure times. Out-gassing of glues, adhesive tapes
and stickers or out-gassing packaging material such as
bubble foils, foams, etc. shall be avoided. Manufacturing
area shall be well ventilated."

They are a relatively delicate part and need to be treated with care.
hmmpic



Joined: 09 Mar 2010
Posts: 188
Location: Denmark

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 10:36 am     Reply with quote

Suggestion, try to make a manual calculation of the msb and lsb result.

Code:
signed int16 r;
r= ((res/(65536/125.0))-5.5);


-res is a int16, and is the 16-bit result from the sensor.
-The reason for 5.5 and not 6.0 is i use int16 and only the int part will be used therefore adding 0.5 will make the rounding more right.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

You may be right Mr T
PostPosted: Thu Jun 30, 2016 10:39 am     Reply with quote

I bought these chips from Aliexpress.

Maybe these sensors are faulty, but how does both the sensors read almost identical values?

I'm a bit stuck now.
_________________
Regards,
Devil
hmmpic



Joined: 09 Mar 2010
Posts: 188
Location: Denmark

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 1:26 pm     Reply with quote

Suggestion: Is your calculated CRC from your reading (msb and lsb) the same as the CRC you read from the chip? If not....


What "@Ttelmah" wrote about sourcing the chip, I have one time ordered from ebay and have learned on that. Use, Mouser, Farnell, Digi-Key, TME or other...
Ttelmah



Joined: 11 Mar 2010
Posts: 12630

View user's profile Send private message

PostPosted: Thu Jun 30, 2016 1:31 pm     Reply with quote

Yes, and Aliexpress is exactly the sort of source that is likely to have 'unofficial' chips. The same reading may well imply they are from the same batch.
Ttelmah



Joined: 11 Mar 2010
Posts: 12630

View user's profile Send private message

PostPosted: Fri Jul 01, 2016 2:41 am     Reply with quote

Doing a conversion of the numbers you give, shows that in fact the values you are getting, would be pretty much what was expected, if bits 12 & 13 of the 16bit value were stuck high. This would add just under 24 to the numbers coming from the chip, but varying across the range (values that have bit 13 high would only get 8 added, while those that have bit 12 high would only get 16 added, while those with both these bits low would get the whole 24 added). I'd really say it is terribly likely that these are faulty chips, with the same fault in the internal memory, of a couple of bits 'stuck high'.....
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

that was what I was trying to find out.
PostPosted: Sun Jul 03, 2016 3:13 am     Reply with quote

Hi Mr T,

That was exactly what I was trying to find out.

Since I do not have a oscilloscope, I am trying to print it onto my LCD screen.
The MSB and LSB variable.

Will keep you posted.
_________________
Regards,
Devil
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Data from the sensor.
PostPosted: Sun Jul 03, 2016 8:51 am     Reply with quote

Hi All,

Here is the sample data I have collected.

Code:

Binary             Dec     RH
1011101100010100   47892   85.34632728
1011110001111000   48248   86.02534032
1011110001101000   48232   85.99482288
1011110001010000   48208   85.94904672
1011110001110000   48240   86.0100816

_________________
Regards,
Devil
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Faulty sensor may be..
PostPosted: Sun Jul 03, 2016 10:10 pm     Reply with quote

HI All,

After a lot of fiddling with the codes and displaying the output it looks like the sensor is giving out the bad data.

Heated up the sensor with a hair dryer and the humidity decreased.

Might be that the sensors that I bought were from a defective lot.

$4.60 lost for the 2 sensors Smile ...

I am not sure If I should close this thread, or wait untill I buy a new sensor and try it out.
_________________
Regards,
Devil
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

SHT21 - updated driver code.
PostPosted: Sun Jul 03, 2016 10:16 pm     Reply with quote

Hi All,

I am placing the udpated driver file here just incase if someone needs it.


Code:


////////////////////////////////////////////////////////////////////////////////
///                               sht21.c                                    ///
///            Driver for temperature and humidity sensor                    ///
///            Realizado por PicTrance                                       ///
///            junio del 2011, Puebla - México                               ///
///            Compiler: CCS                                                 ///
///         Updated July 04, 2016 --- devilfrmheaven www.ccsinfo.com         ///
///                                                                          ///
/// sht21_init()  - Iicializa el sensor sht21 -                              ///
///                                                                          ///
///                                                                          ///
/// sht21_temp(temperatura)      Get the temperature, lee tempeartura        ///
///                                                                          ///
/// sht21_humid(humedad)         Get the humidity, lee humedad               ///
///                                                                          ///
/// sht21_both(temp,humd)        Get the temperature and humidity            ///
///                              Lee temperatura y humedad                   ///
////////////////////////////////////////////////////////////////////////////////
#define sht21     0b10000000
#define writeuser 0b11100110
#define readuser  0b11100111
#define reset     0b11111110
#define temphold  0b11100011
#define humidhold 0b11100101
#define read      0b10000001
#define softreset 0b11111110

/*int8 msbhum, lsbhum, msbtemp, lsbtemp, checksum;
int16 temperies, humidum;
float temperiess, humidumm;*/

int8 checksum;

void sht21_init(void){
   i2c_start();
   i2c_write(sht21);
   i2c_write(softreset);
   i2c_stop();
   delay_ms(15);
}

void SoftResett(){
   i2c_start();
   i2c_write(sht21);
   i2c_write(softreset);
   i2c_stop();
   delay_ms(15);
}

void sht21_temp(float &temperies1){
   //float temperies1;
   int8 msbtemp, lsbtemp;
   int16 temperies;
   float temperiess;
   i2c_start();
   i2c_write(sht21);
   i2c_write(temphold);
   i2c_start();
   i2c_write(read);
   msbtemp = i2c_read();
   lsbtemp = i2c_read();
   checksum = i2c_read(0);
   //i2c_read(0);
   i2c_stop();
   temperies = make16(msbtemp,lsbtemp);//valiable = MAKE16(varhigh, varlow) junta las
                                  //2 variables in8 en una in16

   /*#ASM
      movf lsbtemp,0
      movwf 0x00A
      movf msbtemp,0
      movwf 0x00B
   #ENDASM*/
   
   temperiess=temperies;
   temperies1= (-46.85 + (175.72*(temperiess/65536)));
   SoftResett();
   //return temperies1;
}

void sht21_humid(float &humidum1){
   //float humidum1;
   int8 msbhum, lsbhum;
   int16 humidum;
   float humidumm;
   i2c_start();
   i2c_write(sht21);
   i2c_write(humidhold);
   i2c_start();
   i2c_write(read);
   //delay_ms(25);
   msbhum = i2c_read();
   lsbhum = i2c_read();
   checksum = i2c_read(0);

   //i2c_read(0);
   i2c_stop();
   
   lsbhum &= 0XFC; /// Thanks to Ttelmah @ www.ccsinfo.com
   humidum = make16(msbhum,lsbhum);     
   /*#ASM
      movf lsbhum,0
      movwf 0x00C
      movf msbhum,0
      movwf 0x00D
   #ENDASM*/
   
   humidum1= (-6 + (0.00190734*humidum)); ///// Thanks to Ttelmah @ www.ccsinfo.com
   SoftResett();
   //return humidum1;
}

void sht21_both(float &tmp, float &humd){
   /*float temp;
   float humedad;*/
   sht21_temp(tmp);
   delay_us(800);
   sht21_humid(humd);
}


_________________
Regards,
Devil
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group