News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

SHT-31

Started by steyn, Feb 09, 2022, 01:10 AM

Previous topic - Next topic

steyn

Hi, i am trying to read from the SH-31 temp/humid sensor. I am using sample code i found on here but it does not seem to communicate with the sensor...any help would be much appreciated!

"Device = 18F46K22
Declare Xtal = 12   
Declare LCD_DTPin = PORTB.0   
Declare LCD_RSPin = PORTB.4
Declare LCD_ENPin = PORTB.5
Declare LCD_Interface = 4   
Declare LCD_Lines = 4
Declare LCD_Type = 0
Declare SDA_Pin PORTA.1
Declare SCL_Pin PORTA.2
'Declare Hbus_Bitrate  400
Declare Slow_Bus On
Declare All_Digital = True
TRISA = %00001111
ANSELA = %00000000
ADCON1 = %10000000'%10001111
'TRISA=%00000011
TRISB=%00000000
TRISC=%00000000
TRISD=%00000000
TRISE=%00000000
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0   
Dim NEM_HAM As Word
Dim ISI_HAM As Word
Dim CRC1 As Byte
Dim CRC2 As Byte
Dim CRC_CALC As Byte
Dim ISI As Float
Dim RH As Float       
Dim DERECE        As Word
DERECE = $DF
NEM_HAM = 0
ISI_HAM = 0
ISI = 0   
RH = 0
DelayMS 500

ANA:
GoSub sensoroku:
Cls
PORTD.3=1
Print At 1,1,"Nem: ",Dec2 RH ," %RH"
Print At 2,1,"ISI: ",Dec2 ISI," ",DERECE,"C"
DelayMS 2000
PORTD.3=0
DelayMS 1000
GoTo ANA

sensoroku:
BStart
BusOut %10001000 ,[$2C,$0D]
DelayMS 50
BusIn  %10001001 ,[ISI_HAM.HighByte,ISI_HAM.LowByte,CRC1,NEM_HAM.HighByte,NEM_HAM.LowByte,CRC2]
BStop
RH = 100 * (NEM_HAM.HighByte * 256 + NEM_HAM.LowByte) / 65535
ISI = ISI_HAM.HighByte * 256 + ISI_HAM.LowByte
ISI = -45 + (175 * ISI / 65535)
Return
End"

I am getting 3.13 %RH and -39.50 deg C on the display and they not changing

Thanks
Erick

Giuseppe MPO

Did you put pullup resistors?

steyn

Thanks for the reply Giuseppe...yes i used the dfrobot board thats got all that on board
 

shantanu@india

Can't you use hardware MSSP module? Anyway bit banging should also work... just try to display the raw values received from the SHT31 in the LCD first in binary form. The individual bytes and CRC should cross match if reception is correct.
Regards
Shantanu

steyn

Thanks for the advise...i think that i am not communicating with the device atm :)

steyn

I would really like to get the original code to work for me if possible

steyn

Success! i was looking at the datasheet and found info on addressing...all sorted thanks :)

top204

#7
There are a few things incorrect with the above code. For example:

BStart
BusOut %10001000, [$2C, $0D]
DelayMS 50
BusIn %10001001, [ISI_HAM.HighByte, ISI_HAM.LowByte, CRC1, NEM_HAM.HighByte, NEM_HAM.LowByte, CRC2]
BStop


The BStart and BStop commands are not needed and may cause issues down the line. The Full BusOut and BusIn commands send the I2C Start and Stop commands themselves, and the BStart and BStop and BusAck commands etc, are only required if using the BusIn and BusOut commands as individual commands with a single parameter or assignment.

steyn

Thanks Les...much appreciated. I will make the changes and see how it goes :)

steyn

All good, i am however getting a difference between the humid of two modules...about 6%. The temperature values between them are very close. Might just get a third sensor to compare

top204

#10
With the humidity reading, make sure both sensors are from the same container or environment when testing.

If you have some silica packets, place both sensors in the same container with the packets to draw out the same amount of moisture from them for a few hours. Then test them directly after each other.

Residual humidity inside them can alter the values, so they will not give the same readings.

steyn