Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : SHT21 Temp and Humidity sensor.
Author | Message | ||||
cs41 Newbie Joined: 08/08/2016 Location: AustraliaPosts: 27 |
Has anyone successfully used the SHT 21 digital humidity and temp sensor with a Micromite. The DHT series seem to have a problem where they are in high humidity environments for a considerable time and so wondering about using an SHT21 which seems to be a better sensor. Thanks cs41 |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9030 |
Not me personally, but if the SHT21 uses the same data protocol as the DHT21, then you should just be able to treat it as an DHT21 - even though it is not. If you have one, I would just hook it up and query it using the DHT21 functions, and see if you get a sensible response. I could be totally wrong with that assumption, but that would be where I would start. Smoke makes things work. When the smoke gets out, it stops! |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5867 |
The SHT21 uses the same code as the HTU21 I think they all suffer from long time exposure to 100% humidity. My current setup uses BME280 and SHT21. It has only been running for 12 months so too early to tell how long the sensors will last. Code for SHT21: OPTION BASE 0 DIM AS INTEGER TDATA(3) '2 bytes of temp data plus CRC DIM AS INTEGER HDATA(3) '2 bytes of humidity data plus CRC ON ERROR SKIP 1 I2C OPEN 400,1000 SETTICK 10000, doRead doRead DO k$ = INKEY$ LOOP UNTIL k$ = "x" or k$ = "X" I2C CLOSE END SUB doRead HTU21D PRINT TIME$;" ";STR$(Temperature,4,1);"c ";STR$(RELHUMID,4,0);"% ";STR$(RFHUMID,4,0);"%" END SUB SUB HTU21D 'TEMPERATURE SENSOR, This is the SUB routine for talking to the HTU21 I2C WRITE &H40,0,1,&HF3 'HTU21 addr= 40 hex, request temp = F3 hex PAUSE 60 I2C READ &H40,0,3,TDATA() 'reads temperature TEMP1!=TDATA(0)*256 + TDATA(1) 'combines both data bytes IF checkCRC(TEMP1!,TDATA(2)) THEN Temperature = -999 ELSE TEMP1! = TEMP1! AND &hFFFC ' mask off status bits Temperature=(175.72*(TEMP1!/65536))-46.85 'HTU21 formula for temp ENDIF TEMPCOMP= (Temperature-25)*0.15 'for RH temp comp I2C WRITE &H40,0,1,&HF5 'request humidity PAUSE 30 I2C READ &H40,0,3,HDATA() HUMIDACT!=HDATA(0)*256 + HDATA(1) IF checkCRC(HUMIDACT!,HDATA(2)) THEN RELHUMID= -999 ELSE HUMIDACT! = HUMIDACT! AND &hFFFC ' mask of status bits RELHUMID= ((HUMIDACT!/65536)*125)-6 ' measured humidity RFHUMID= RELHUMID + TEMPCOMP 'Temperature compensated humidity ENDIF END SUB FUNCTION checkCRC(message_from_sensor, check_value_from_sensor) ' Give this function the 2 byte message (measurement) and the check_value byte from the HTU21D ' If it returns 0, then the transmission was good LOCAL INTEGER i, remainder, divisor divisor = &h988000 'This is the 0x0131 polynomial shifted to farthest left of three bytes ' remainder = message_from_sensor << 8 'Pad with 8 bits because we have to add in the check value remainder = remainder OR check_value_from_sensor ' Add on the check value ' FOR i = 0 TO 15 'Operate on only 16 positions of max 24. The remaining 8 are our remainder and should be zero when we're done. IF (remainder AND (1 << (23 - i)) ) THEN ' Check if there is a one in the left position remainder = remainder XOR divisor ENDIF divisor = divisor >> 1 ' Rotate the divsor max 16 times so that we have 8 bits left of a remainder NEXT i checkCRC = remainder END FUNCTION Search the forum for SHT21. There was a comparison of various sensors a few years ago. Jim VK7JH MMedit  MMBasic Help |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1757 |
I have been using HTU21D and have found them much more reliable that DHT22. I have had two running for about a year with no problems. The code I use is... Sub HTU21D 'This is the SUB routine for talking to the HTU21 I2C write &H40,0,1,&HF3 'HTU21 addr= 40 hex, request temp = F3 hex Pause 100 I2C read &H40,0,2,TDATA() 'reads temperature TEMP1=TDATA(0)*256 + TDATA(1) 'combines both data bytes I2C write &H40,0,1,&HF5 'request humidity Temperature=(175.72*(TEMP1/65536))+ -46.85 'HTU21 formula for temp TEMPCOMP= -.15*(25-ROOF) 'for RH temp comp Pause 50 I2C read &H40,0,2,HDATA() HUMIDACT=HDATA(0)*256 + HDATA(1) RELHUMID= ((HUMIDACT/65536)*125)+ -6 ' measured humidity RFHUMID= RELHUMID + TEMPCOMP 'Temperature compensated humidity End Sub I must say this is not my code, DaveC166 gave it to me. Edited 2020-01-23 16:52 by palcal "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
cs41 Newbie Joined: 08/08/2016 Location: AustraliaPosts: 27 |
Thanks everyone for helpful comments. Will try these and see how they work in our application. many thanks for helpful comments as always. cs41 |
||||
DaveC166 Regular Member Joined: 13/09/2012 Location: New ZealandPosts: 44 |
I gave up on Ebay sourced DHT22's 3 years ago, due to the erratic humidity readings especially when the temperature was under 20 deg C. The 8 DHT22's were from 3 different sellers The Ebay sourced HTU21D's have been working without any glitches for three years. Tassyjim's code solves the only thing i couldnt workout, how to use the CRC checksum. The only slight hassle with HTU21D modules is the need for a dust cover over the port in the chip |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1757 |
Well I should have kept my mouth shut, my outdoor HTU21D is reading 119%. It has been very wet here lately, over 250mm. of rain in the last 3 days. Don't know if that has caused the trouble, the sensor is well protected. @DaveC166 What do you mean by a dust cover, wouldn't that affect the reading? "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1757 |
I see in the Datasheet there is an (F) version of the chip that has the dust cover factory fitted. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5867 |
It might recover in time when the humidity drops. Try bringing it inside for a few days. There is a heater built into them which can be used to see if the humidity sensor is working. A bit of heat should lower the humidity. Luck they are relatively cheap to buy. Jim VK7JH MMedit  MMBasic Help |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1757 |
I'll bring it in when it stops raining, I have a spare. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1757 |
I bought one of these, may be better for my outdoor sensor. HTU21D "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
DaveC166 Regular Member Joined: 13/09/2012 Location: New ZealandPosts: 44 |
HI Pacal, The HTU21D modules are supposed to have a dust proof foam cover over the sample port in the chip, as dust and moisture will effect the accuracy of the readings. I screwed up one HTU21D by exposing it to steam. The module did work correctly after it dried out. I have ordered a couple of the protective case Htu21D's as per your link. the mounting holes will come in handy. I presume it is best to mount them so the filter is vertical ?. |
||||
Print this page |