News:

;) This forum is the property of Proton software developers

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

dr-zin

Thanks for the responses, both of you.  I included your code tweaks, and the response is much more normal now.  Wow, RGV250, you are a hero member!  It was a code issue after all (which is good).  I have seen people show examples of fake sensors on YouTube, so I thought (selfishly) that that must have been the problem and not me.  Turns out my conversion from weird binary to decimal was whacked.  Thanks again.  My needs are only for sensing around ambient temp. range, so no special provisions for >boiling pt. or negative temps are required.

Since you have donated your time to help me, I'll fill you in on my needs:  I need to accurately read the temp. from 55 to 85 deg. F in whole number units (converts to ~12.8 - 29.4 Celsius/centigrade in 1/2 C intervals).  This is not a very heavy lift for this sensor.  Indeed, my first approach was to use a 10Kohm NTC thermistor in the frequency selection string of a 555 timer to make a temp. to freq. converter, use the PIC to measure the frequency produced, then use a LookDownL list to find the frequency range to correlate to F temps.  This is required because thermistors do not have a linear response, necessitating ugly math to convert directly.  This seemed to work, but it didn't have the repeatability I wanted.

So, I scrapped that idea and went to Plan B, which is the DS18B20 sensor.  This is overkill for my requirements, but seemed acceptable if I can get it to work.  When I ran into conversion problems, I thought I would have to scrap this idea and go to Plan C (LM334 current reg./temp. sensor which produces 10mV/deg.K output; scale result and measure using PIC A/D converter, then convert to F temp. scale---this is way, way, way too overkill for my needs, but once you go into the rabbit-hole of pain, you have to see it through or feel you've wasted weeks of your time).

Hopefully, now I can go on to use the temp. value for its intended purpose and get the project back on track.  At least the pain was "educational".  Thanks again gentle readers!