detecting READTEMP errors

RSanger

New Member
How can one detect if the DS18B20 sensor has been disconnected or is not working - detect error in readtemp result?
THANKS!
 

hippy

Technical Support
Staff member
Reading the 1-wire serial number with READOWSN would probably be the most reliable way to tell it's connected and responding as expected -

Code:
Symbol DS18B20 = B.0

Do
  Pause 2000
  ReadOwSn DS18B20
  SerTxd( #b6,  " ", #b7,  " ", #b8,  " ", #b9,  " "    )
  SerTxd( #b10, " ", #b11, " ", #b12, " ", #b13, CR, LF )
Loop
That seems to read 'all zeroes' for me when nothing connected, no pull-up, 'all 255' with pull-up, and checking the b13 CRC should detect other errors.

The DS18B20 does, as noted by inglewoodepete, return 85C when temperature reading isn't working as it should, but I am not sure how you could determine if working but not giving reliable or correct results.
 

hippy

Technical Support
Staff member
Not exhaustively tested ...

Code:
Symbol DS18B20    = B.0

Symbol val        = b0
Symbol crc        = b1
Symbol bitNumber  = b2

Do
  Pause 2000

  b6  = 0 : b7  = 0 : b8  = 0 : b9  = 0
  b10 = 0 : b11 = 0 : b12 = 0 : b13 = 0

  ReadOwSn DS18B20

  SerTxd( #b6,  " ", #b7,  " ", #b8,  " ", #b9,  " " )
  SerTxd( #b10, " ", #b11, " ", #b12, " ", #b13, " " )

  If b6 <> 40 Then
    Sertxd( "Fail", CR, LF )
  Else 
  
    crc = 225
    val = b7  : Gosub CrcAdd
    val = b8  : Gosub CrcAdd
    val = b9  : Gosub CrcAdd
    val = b10 : Gosub CrcAdd
    val = b11 : Gosub CrcAdd
    val = b12 : Gosub CrcAdd

    If crc <> b13 Then
      SerTxd( "Fail", CR, LF )
    Else

      ReadTemp DS18B20, val
      If val = 85 Then
        SerTxd( "Fail", CR, LF )
      Else
        SerTxd( "Okay = ", #val, "C", CR, LF )
      End If

    End If
  End If

Loop

CrcAdd:
  For bitNumber = 0 To 7
    crc = crc ^ val & 1 * $118 ^ crc / 2
    val = val / 2
  Next
  Return
 
Last edited:

RSanger

New Member
WOW. I never heard of ReadOwSn.... not in the manual. But I suppose that's what the forum is for! Is there a list of commands that aren't in the manual?
And actually I'm using the READTEMP12 command.... which seems to give "0" when the sensor is disconnected...
 

Technical

Technical Support
Staff member
If either readtemp command is giving 0 without sensor then the pull up resistor is not connected correctly either.
 

geoff07

Senior Member
I am not sure how you could determine if working but not giving reliable or correct results.
The only way to do this is to have three and vote. Even that isn't absolute.

Whilst I'm on, you don't need to use readtemp, which blocks for a long period. Instead initiate the read and then go back and get the data, like this:
Code:
   symbol DB18S20_skip_rom       =   $CC 
   symbol DB18S20_convert_T      =   $44
   symbol DB18S20_read_T         =   $BE

read_temps: 'reads all the temps, called every second
'read the result of the last conversion at least 750mS ago
'uses bit mapping of w0 and b0, b1
   owout HI_hw_temp,  %1, (DB18S20_skip_rom, DB18S20_read_T)
   owin  HI_hw_temp,  0,  (b0, b1)
   call convert_raw_temp_to_tenths_deg  
   W_hw_temp  = W_temp_deg        'save for use            
'----
   owout HI_ret_temp, %1, (DB18S20_skip_rom, DB18S20_read_T)
   owin  HI_ret_temp, 0,  (b0, b1)
   call convert_raw_temp_to_tenths_deg  
   W_ret_temp  = W_temp_deg        'save for use          
'---
   owout HI_ext_temp, %1, (DB18S20_skip_rom, DB18S20_read_T)
   owin  HI_ext_temp, 0,  (b0, b1)
   call convert_raw_temp_to_tenths_deg  
   W_ext_temp  = W_temp_deg        'save for use            
'----
   owout HI_go_temp,  %1, (DB18S20_skip_rom, DB18S20_read_T)
   owin  HI_go_temp,  0,  (b0, b1)
   call convert_raw_temp_to_tenths_deg  
   W_go_temp  = W_temp_deg        'save for use            
init_read_temps: 'start the conversion process for next time
   owout HI_hw_temp,  DB18S20_mode, (DB18S20_skip_rom, DB18S20_convert_T)
   owout HI_ret_temp, DB18S20_mode, (DB18S20_skip_rom, DB18S20_convert_T)
   owout HI_ext_temp, DB18S20_mode, (DB18S20_skip_rom, DB18S20_convert_T)
   owout HI_go_temp,  DB18S20_mode, (DB18S20_skip_rom, DB18S20_convert_T)
   return
convert_raw_temp_to_tenths_deg:
   'deal with negative temps - if bit 15 = 1
   if W_temp_deg BIT 15 SET then 'deal with negative temps
      W_temp_deg = 0
   endif
   W_temp_deg = W_temp_deg * 25/40 'convert raw DS18B20 readings to 1/10 deg
   B_temp_dec_deg = W_temp_deg // 10
   return
This is part of my home heating scada system, but the code could do the three sensors in a voting setup. Mine are all on separate I/O pins but you could daisy-chain.
 

westaust55

Moderator
The 85 degC value is the &#8220;power on reset&#8221; indicator.
That indicates the DS18B20 is connected and powered-up but no temp conversion has been initiated.

Multiple 1-wire devices can only be connected to a single I/O pin when using an X1 or X2 part which has the additional commands to address indivdual 1-wire devices.
 
Top