News:

Let's find out together what makes a PIC Tick!

Main Menu

DS18B20 (Fake?)

Started by dr-zin, Sep 15, 2025, 10:52 PM

Previous topic - Next topic

dr-zin

Hello friends,

I have purchased and hooked up a DS18B20 (from an online order batch of 5).  The code for reading the temp was mostly lifted from Top204's 7/22 post, and verified with code segments from other posters as well, so I assume I am reading it properly.  I am using a PIC16F627 with internal 4 MHz clock.  The program basically powers up the sensor, reads in the code stream, processes it into temp (in deg. C), and displays it to an LCD (20-04) display that updates once/sec.

It appears to work exactly as needed, but the temp displayed drifts radically, starting at about 30 deg. (which is higher than ambient) and, if I lightly place my finger on the TO-92 case, it rapidly zooms up to 60-65 degrees over about 20 seconds.  I know I'm hot, but nobody is THAT hot!  Similarly, the temp. plummets when I place an ice cube in a zip lock bag against the case, but that is more or less expected.

I have tried 2 from the batch of five (purchased from a US eBay reseller, but who knows the actual source?) and gotten the exact same behavior.  Should I try the rest?  Has anyone seen this behavior from this sensor?  Should I spring for a few sensors from reputable US suppliers?  Are these fake???  Any insights or experiences you would like to share would be appreciated.  Thanks, Paul

RGV250

Hi,
Are you sure it is a "B" as from memory there is a difference with 1820 and 18S20. If i get time I will have a look at the datasheets to confirm this.

Bob

RGV250

Hi,
Perhaps you could post your code, I have looked and the "B" version has programmable resolution (9-12bits) which might be the issue?

Bob

dr-zin

Thank you for the quick response.  I'm sure of the product type being marked as "B".  Feel free to proctor my code (included below), but I was looking for others who have (or have never) had a problem with this product who might assure me that this is a known issue that has been seen on dud/fake units, or perhaps that the 16F627A is known to not have the horsepower to handle this sensor.  I actually wanted to use it with a 12F675, but switched temporarily to a 16F627A so I could use the extra pins to monitor the output on the LCD display.  The drift and span problems with light increases in temperature change seem to be a bug, and since I don't know the provenance of the sensors, I wanted a sanity check to confirm my suspicions.  Thanks.

Code Section:

Device = 16F627A

Config FOSC_INTOSCIO, WDTE_OFF, PWRTE_OFF, MCLRE_ON, BOREN_OFF, LVP_OFF, CPD_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Xtal = 4
All_Digital = True
Declare LCD_Type = 0
Declare LCD_RSPin = PORTB.5
Declare LCD_ENPin = PORTB.3
Declare LCD_Interface = 4
Declare LCD_Data7_Pin = PORTA.3
Declare LCD_Data6_Pin = PORTA.2
Declare LCD_Data5_Pin = PORTA.1
Declare LCD_Data4_Pin = PORTA.0
Declare LCD_Lines = 4
 
Setup:
TRISA = $00
TRISB = $0f
Symbol DQ = PORTA.4
Dim Result As Word
Dim ResultW As Byte
Dim ResultD As Byte
Dim C As Byte
Print $FE, 1
DelayMS 30

Main:

GoSub DS18B20

GoTo Main

DS18B20:
                          ' Sends via mode = 1 a bus initialization consisting of a Reset = 0 for> 480uS
OWrite DQ, 1, [$CC, $44]  ' Send Skip Rom command ($ CC) + ($ 44) Convert T command. Converts temperature and stores in
Repeat                    ' 2 Byte of the ScratchPad (Byte 0 and Byte 1) the value
DelayMS 10                ' Wait until conversion is complete
ORead DQ, 4, [C]          ' Continue reading Low pulses Until. 4 stands for Mode (No Reset Bit Mode)
Until C <> 0              ' the DS1820 has finished the conversion because the bus from 0 has gone to 1
Cls
OWrite DQ, 1, [$CC, $BE]  ' Send Read ScratchPad command
ORead DQ, 0, [Result.Byte0, Result.Byte1]           ' Read the temperature data only from the DS18B20 device
Result = Result * 10                                ' Multiply the raw temperature value by 10
Result = Result / 16                                ' Convert to Celsius
ResultW = Result >> 1
ResultD = Result << 2
Print At 1,1, Dec2 ResultW,".",Dec1 ResultD," deg. C"
DelayMS 1000           
Return

End

RGV250

Hi,
In a simulation I get pretty much the same, I have tried to modify some code for a "S20 but there are issues, it only shows integers?

Bob

RGV250

#5
Hi,
Try this
ResultW = Result / 10                               ' Calculate the Integer portion
ResultD = Result - ResultW * 10                     ' Calculate the decimal portion
Print At $FE,1, Dec2 ResultW,".",Dec1 ResultD," deg. C"

>>1 is divide by 2
<<2 is multiply by 4

Bob

RGV250

Hi,
If you want to show temperature over 99.9 you need Dec3

Bob

xvovanx

#7
Delete this line - Result = Result * 10

Here is my formula for calculating temperature and +/-. Always works flawlessly!
TEMP:  
                             
OWrite DQ,1,[$CC,$44]        ' Послать датчику DS18S20 команду cброса/старта измерения температуры
Repeat                       ' Начать внутренний цикл (до тех пор пока не выполнится условие Until)                                                                                                
    DelayMS 10               ' Ждать окончания преобразования пока не придет бит, сигнализирующий    
    ORead DQ,4,[TEMP_OK]     ' об окончании преобразования                                           
Until TEMP_OK>0              ' Выйти из цикла, если от DS18S20 поступил бит конца преобразования.  
OWrite DQ,1,[$CC,$BE]        ' Послать команду чтения значения температуры из ОЗУ датчика DS18S20 
ORead DQ,2,[TEMP_C.LowByte,TEMP_C.HighByte]' Прочитать значение температуры и записать полученные
                             ' два байта данных в переменную TEMP_LCD
If TEMP_C.11=1 Then          ' если бит отриц.полярности температуры =1, то
    TEMP_MINUS=1             ' устанавливаем флаг отрицательной температуры
    TEMP_C=(~TEMP_C+1)/16    ' расчет (-)температуры
    TEMP_REAL=0-TEMP_C       ' отнимаем от нуля для получения реального отрицательного значения
Else                         ' температуры (используется для сравнения со значением t'C из меню)
    TEMP_MINUS=0             ' сброс флага отрицательной температуры                              
    TEMP_C=TEMP_C/16         ' расчет (+)температуры
    TEMP_REAL=TEMP_C
EndIf

Return

If TEMP_MINUS=1 Then     
    Print At 2,1,"-",Dec2 TEMP_C
Else                     
    Print At 2,1," ",Dec2 TEMP_C
EndIf