Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:30 29 Apr 2025 Privacy Policy
Jump to

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 : CMM2: DS18B20 Temp sensor problem

     Page 6 of 7    
Author Message
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2396
Posted: 11:42am 09 Aug 2020
Copy link to clipboard 
Print this post

  TassyJim said  According to https://github.com/cpetrich/counterfeit_DS18B20
Your rom code is wrong due to the "20 02" last two numbers which should be zeros.


from the above link:
"If the ROM does not follow the pattern 28-xx-xx-xx-xx-00-00-xx then the DS18B20 sensor is a clone [5]."
and
"References
5. Own investigations 2019, unpublished."

the only statement about the content of the ROM made by the DS18B20 datasheet is:
"The least significant 8 bits of the ROM code contain the DS18B20’s 1-Wire family code: 28h. The next  48  bits  contain  a  unique  serial  number.  The  most  significant 8 bits contain a cyclic redundancy check (CRC) byte  that  is  calculated  from  the  first  56  bits  of  the  ROM  code."

based upon the DS18B20 datasheet, and without further input directly from Maxim, i'd find it hard to accept as gospel the "00 00" assertion; Maxim and/or DS could have in the past released any (large or small) number of DS18B20 with non-zero values in the 6th and 7th byte of the ROM.

i also have doubts about the forensic usefulness of the default contents of EEPROM - these values could innocently be changed by Maxim during manufacture and testing, during sample testing post-manufacturing, or by anyone in the supply chain who may have wished to test devices they handled. Maxim may have a number of plants capable of manufacturing DS18B20, and different plants could use their own individual testing and calibration routines that leave the EEPROM contents in set to a pattern identified as 'fake' .

apart from abnormal behaviour (including inaccuracy), the only sure way to identify a non-maxim part is to split the case open and examine the silicon die.


cheers,
rob   :-)
Edited 2020-08-09 21:47 by robert.rozee
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2943
Posted: 12:13pm 09 Aug 2020
Copy link to clipboard 
Print this post

Hi Rob, All,

I have been wondering about the ROM as surely if you go to the effort to create a ‘clone’ could you not just clone the ROM code.

IF the one I bought from Jaycar is a fake it seems to have an almost identical temperature report to the genuine one.. (see my previous post with the two chips tied together.)

What is the zero degrees issue?  I can test them in a fridge/freezer/ice if you like.

Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3297
Posted: 12:22pm 09 Aug 2020
Copy link to clipboard 
Print this post

  bigmik said  What is the zero degrees issue?

Paul_L once suggested to me that a very good way of testing/comparing perhaps dodgy waterproof DS18B20s was to put crushed ice in a glass, pour in a modest amount of chilled water, wait a few minutes for the ice to bring the water to very close to freezing, and insert the probes. That should be very close to 0C if you have the mix right, and a well-established universal calibration point.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6210
Posted: 09:49pm 09 Aug 2020
Copy link to clipboard 
Print this post

  robert.rozee said  
  TassyJim said  According to https://github.com/cpetrich/counterfeit_DS18B20
Your rom code is wrong due to the "20 02" last two numbers which should be zeros.


Rob, you left out the bit about conversion times
  Quote  Based on the timing for 12 bit conversion, your device is group A2.


While I agree that the rom code 'could' be valid, it is unlikely in the extreme that Maxim will have produced a batch of chips that do the conversions in half the time, and then gone back to the normal time in subsequent batches.

I suspect that the clone manufacturers chose rom codes well outside the expected range to prevent the chance of duplicate codes being discovered in the immediate future.

The chips that Mick and I purchased from the Jaycar supply chain, all have slower timing than the usual times seen with known good chips, but the timing is still within published specs so 'could' be valid, but suspicious.

I buy through Ebay.
I expect clones, recycled chips, out-of-spec rejects...

Jim
VK7JH
MMedit
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2943
Posted: 02:42am 10 Aug 2020
Copy link to clipboard 
Print this post

Hi Jim, All,


I did the ice water trick and both sensors reported the same readings within 0.25C all the way down from the ambient of 19.5C down to where they stopped updating at 0.5C

Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5
Mouser =  0.5   Jaycar =  0.5



The Jaycar unit seems to respond quicker to the change but only by the 0.25C then the Mouser unit caught up and they were together again until the Jaycar once again responded to drop before the Mouser one

As far as I can tell the 2 sensors as are accurate as each other.

It took a good 10 minutes for both units to settle at 0.5C

Kind Regards

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 02:40am 14 Aug 2020
Copy link to clipboard 
Print this post

  lizby said  
  bigmik said  What is the zero degrees issue?

Paul_L once suggested to me that a very good way of testing/comparing perhaps dodgy waterproof DS18B20s was to put crushed ice in a glass, pour in a modest amount of chilled water, wait a few minutes for the ice to bring the water to very close to freezing, and insert the probes. That should be very close to 0C if you have the mix right, and a well-established universal calibration point.

Funny thing about standards -- most of them aren't standardized -- except for the ice point of water.

It works best if you use distilled water (filtered rain water?), use an insulated coffee mug, ice chips not big ice cubes, stir the ice water for 30 seconds before inserting the probe, and insert the probe in the center of the mug without touching the sides or bottom. You should be able to achieve within 0.2°C of a true 0°C easily.

Paul in NY
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2943
Posted: 03:04am 14 Aug 2020
Copy link to clipboard 
Print this post

Hi Paul,

I used filtered ice water from our fridge and ice dispenser...  After about 15min neither sensor dropped below 0.5c

I actually did another test that I didn’t report, I used a can of freeze spray and they once again followed each other within 0.5 and 0.25C (dropping was a bit more inconsistent (no more than 1.5C but I put that down to me spraying one more than the other chip, it is hard to equally spray each DS18B20 even though I bound them together)...

At about -27.5C (a bit like a Polak summer??) I stopped and let them both recover and the spread between the known good Mouser and the suspect Jaycar chip was no more than 0.25 99% of the time with the odd one 0.5 difference  between chips and quite often reading Exactly the same.

My conclusion is that the Jaycar chip IF it is a fake is an extremely good fake and is most definitely suitable to be used as a DS18B20.

Kind Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 05:23pm 14 Aug 2020
Copy link to clipboard 
Print this post

I don't understand why you guys go chasing the cheapest questionable source for these sensors. You're probably buying sensors that didn't pass final inspection. I suspect that the production yield after final inspection is less than 30%!

Bandgap sensors are difficult to make, requiring the calibration of the current density in two p-n diode junctions and measuring the difference between them. Why would you deliberately buy seconds when Mouser has over 3,000 of the real thing in stock at a price of < $4.00 each.

The only difficult part is finding an appropriately sized closed end stainless steel or copper tube to insert the TO92 case into, or just mounting them by their wires on a little terminal board.

Paul in NY
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2943
Posted: 03:44am 15 Aug 2020
Copy link to clipboard 
Print this post

Hi Paul,

The object is NOT to buy cheap it is to determine exactly what you will get for your money.

It is very confusing, for instance this listing from Mouser.

Mouser DS18B20

Includes 2 pages of offerings for DS18B20,

'Manufacturers' include many different sources.

For example

Seeed Studio
Maxim
Adafruit
Sparkfun
DFRobot
ANAVI Technology
DLP Design..

Then Mouser has a fairly expensive freight cost To AUS unless you spend $60 which I do when I buy from them.
Mind you I really like Mouser I get my parts usually 3-4 days after I order which is fantastic coming from the `REAL' antipodes.

Here in Aus we really are stuck with RS Components (great to deal with and free freight no matter order QTY), but often have to buy in bags of 5, 10 or more.
Element14 which offers free freight over I think $50.
Both the above are generally (not always) expensive prices and I have found recently that even though they say a qty is in stock, often they are placed on back order which could be 2-3 months away.

Then there are Jaycar and Altronics who whilst cheaper have a very limited repertoire of stock but they are generally in range of almost everywhere, if you are in the cities.

Then there is really only a smattering of other suppliers with the stuff we tinker around with and mostly do not stock much at all.

Kind Regards

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2403
Posted: 12:45am 10 Mar 2025
Copy link to clipboard 
Print this post

TassyJim's test program with some mods for the PicoMite.
'DS18x20 test code by TassyJim
' Input "Type Pin number ",PinNbr
Input "Type PicoMite GPxx number for the DS18B20 (include the GP) "; GPPin$
Dim INTEGER PinNbr = MM.Info(pinno GPPin$)

Print
Print
Print "Family name: ";FamilyName$(PinNbr)
Print "Rom Code:    ";getRomCode$(PinNbr)
Print "ScratchPad:  ";getScratchpad$(PinNbr)

If powerMode(PinNbr) Then
  Print "Power mode   External"
Else
  Print "Power mode   Parasitic"
EndIf

Print
For n = 9 To 12
  Print
  x = setResolution(PinNbr, n)
  Print "ScratchPad:  ";getScratchpad$(PinNbr)
  Timer = 0
  Print "Temperature: ";getTemp(PinNbr);" ";Timer;"mS,";n;" bit"
Next n

Print : Print "Tempr() without start", TEMPR(PinNbr)
TEMPR START PinNbr,0 : Print "Tempr Start Pin,0", TEMPR(PinNbr)
TEMPR START PinNbr,1 : Print "Tempr Start Pin,1", TEMPR(PinNbr)
TEMPR START PinNbr,2 : Print "Tempr Start Pin,2", TEMPR(PinNbr)
TEMPR START PinNbr,3 : Print "Tempr Start Pin,3", TEMPR(PinNbr)
End


Function getRomCode$(PinNbr As INTEGER)
' get ROM Code - useful if you want more than 1 device on wire
Local INTEGER a1,a2,a3,a4,a5,a6,a7,a8
OneWire WRITE PinNbr,1,1,&h33 'read ROM code
OneWire READ PinNbr,0,8,a1,a2,a3,a4,a5,a6,a7,a8
getRomCode$ = Hex$(a1,2)+" "+Hex$(a2,2)+" "+Hex$(a3,2)+" "+Hex$(a4,2)+" "+Hex$(a5,2)+" "+Hex$(a6,2)+" "+Hex$(a7,2)+" "+Hex$(a8
,2)
End Function

Function getScratchpad$(PinNbr As INTEGER)
'ONEWIRE Write PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8  'read from scratchpad
OneWire WRITE PinNbr,1,2,&hcc,&hbe
OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h
getScratchpad$ = Hex$(a,2)+" "+Hex$(b,2)+" "+Hex$(c,2)+" "+Hex$(d,2)+" "+Hex$(e,2)+" "+Hex$(f,2)+" "+Hex$(g,2)+" "+Hex$(h,2)
End Function

Function powerMode(PinNbr As INTEGER) As INTEGER
' check whether power external (1) or parasitic (0)
OneWire RESET PinNbr
OneWire WRITE PinNbr, 1,2,&hcc, &hb4
OneWire READ PinNbr,4,1,powerMode
End Function

Function getFamily(PinNbr As INTEGER) As INTEGER
getFamily = Val("&h"+Left$(getRomCode$(PinNbr),2))
End Function

Function FamilyName$(PinNbr As INTEGER)
Local INTEGER fn
fn = getFamily(PinNbr)
Select Case fn
  Case 16
    FamilyName$ = "DS1820/DS18S20"
  Case 34
    FamilyName$ = "DS18S22"
  Case 40
    FamilyName$ = "DS18B20"
  Case Else
    FamilyName$ = "Unknown"
End Select
End Function

Function setResolution(PinNbr As INTEGER,r As INTEGER)
r = r-9
If r < 0 Then r = 0
If r > 3 Then r = 3
r = r*32+31
OneWire WRITE PinNbr, 1,5,&hcc, &h4E, &hFF,&hFF,r
End Function

Function getTemp(PinNbr As INTEGER) As FLOAT
Local INTEGER fn, t, T1, T2, a,b,c,d,e,f,g,h
Local FLOAT Value
fn = getFamily(PinNbr)
power = powerMode(PinNbr)
OneWire RESET PinNbr                        ' reset before command
OneWire WRITE PinNbr, 8, 2, &hcc, &h44      ' start conversion

'read external when b goes high, for parasitic just wait
If power = 0 Then
  Pause 750
Else
  t = Timer
  Do
    If Timer - t > 1000 Then
      Value = 1000
      Exit Do
    EndIf
    OneWire READ PinNbr, 4 , 1 , b            ' conversion done?
  Loop Until b = 1
EndIf
If Value = 0 Then ' we have not timed out yet
  OneWire WRITE PinNbr, 1, 2, &hcc, &hbe      ' command read data
  OneWire READ PinNbr, 2, 2, T1, T2           ' get the data
  OneWire RESET PinNbr
  Select Case fn
    Case 16 'DS18S20 or DS1820
      OneWire WRITE PinNbr,1,2,&hcc,&hbe
      OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h
      If T2 And &h80 Then       'if MSB of T2=1 then negative
        'Read 12bit resolution and adjust
        'truncate 0.5deg value (or use integer division \)
        T1 = T1 And &hFE
        T1=T1 / 2                   'make whole degrees
        'add compensation values read from scratchpad
        T1=T1*16                  'make lsb 1/16 degree
        T1 = T1 -4 +(16 - g)    'add 12 bit value
        'take 2s complement
        T1 = (T1 Xor &hFF) + 1
        Value = -T1/16                'make decimal value in degrees
      Else                              'positive temp
        Value = T1 / 2                '9bit value
        'Read 12bit resolution and adjust
        T1 = T1 And &hFE      'truncate 0.5deg(or use integer division \)
        Value = T1/2- 0.25 + (16 - g) /16   '12 bit value
      EndIf
    Case 34, 40 ' DS18S22 or DS18B20
      If T2 And &b1000 Then       'negative temp
        'make 2s complement (1s complement+1)
        T2 = (T2 Xor &hFF)
        T1 = (T1 Xor &hFF)+1
        If T1=1 Then T2=T2+1        'add the carry if required
        Value = -((T2 And &b111) * 256 + T1) / 16
      Else                            'positive temp
        Value = ((T2 And &b111) * 256 + T1) / 16
      EndIf
    Case Else
      Value = 1000
  End Select
EndIf
getTemp= Value
End Function

And the output with my non-genuine units.
> RUN
Type PicoMite GPxx gp6


Family name: Unknown
Rom Code:    29 61 64 12 3D D4 A4 B8
ScratchPad:  7B 01 FF FF 7B FF FF FF
Power mode   External


ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  1000  17.838mS, 9 bit

ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  1000  17.851mS, 10 bit

ScratchPad:  BD 80 FF FF AF FF FF FF
Temperature:  1000  17.827mS, 11 bit

ScratchPad:  7B 01 FF FF 7F FF FF FF
Temperature:  1000  17.815mS, 12 bit

Tempr() without start    23.625
Tempr Start Pin,0        23.625
Tempr Start Pin,1        23.625
Tempr Start Pin,2        23.375
Tempr Start Pin,3        23.375
> RUN
Type PicoMite GPxx gp7


Family name: DS18B20
Rom Code:    28 3F 44 57 04 E1 3C 4D
ScratchPad:  80 01 00 00 7F A5 A5 66
Power mode   External


ScratchPad:  80 01 FF FF 1F A5 A5 66
Temperature:  22.5  495.694mS, 9 bit

ScratchPad:  68 01 FF FF 3F A5 A5 66
Temperature:  22.75  495.831mS, 10 bit

ScratchPad:  6C 01 FF FF 5F A5 A5 66
Temperature:  22.75  495.681mS, 11 bit

ScratchPad:  6C 01 FF FF 7F A5 A5 66
Temperature:  22.8125  495.811mS, 12 bit

Tempr() without start    1000
Tempr Start Pin,0        1000
Tempr Start Pin,1        22.75
Tempr Start Pin,2        1000
Tempr Start Pin,3        22.8125
>

Edited 2025-03-10 10:50 by phil99
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6210
Posted: 02:18am 10 Mar 2025
Copy link to clipboard 
Print this post

Found time to test before leaving.

local retailer (Jaycar)

Family name: DS18B20
Rom Code:    28 AA 42 A3 41 14 01 D5
ScratchPad:  51 01 00 00 7F FF 60 10
Power mode   External

ScratchPad:  51 01 FF FF 1F FF 1F 10
Temperature:  21  109.795mS, 9 bit

ScratchPad:  50 01 FF FF 3F FF 3F 10
Temperature:  21  199.835mS, 10 bit

ScratchPad:  50 01 FF FF 5F FF 5F 10
Temperature:  20.875  379.825mS, 11 bit

ScratchPad:  4E 01 FF FF 7F FF 7F 10
Temperature:  21  739.858mS, 12 bit

Tempr() without start   21      210.887
Tempr Start Pin,0       21      110.06
Tempr Start Pin,1       21      209.799
Tempr Start Pin,2       21      409.839
Tempr Start Pin,3       21      809.776

> RUN
Known good.

Family name: DS18B20
Rom Code:    28 62 D8 F1 05 00 00 18
ScratchPad:  50 05 4B 46 7F FF 0C 10
Power mode   External


ScratchPad:  50 05 FF FF 1F FF 0C 10
Temperature:  22  94.519mS, 9 bit

ScratchPad:  60 01 FF FF 3F FF 10 10
Temperature:  21.75  169.149mS, 10 bit

ScratchPad:  5C 01 FF FF 5F FF 04 10
Temperature:  21.75  318.568mS, 11 bit

ScratchPad:  5C 01 FF FF 7F FF 04 10
Temperature:  21.625  617.607mS, 12 bit

Tempr() without start   21.75   210.933
Tempr Start Pin,0       22      109.848
Tempr Start Pin,1       21.75   209.812
Tempr Start Pin,2       21.75   409.785
Tempr Start Pin,3       21.625  809.801


The module purchased form Jaycar 5 years ago is slower that the specs but still just fast enough for the picomite code.
It may give errors in 9 and 10 bit resolution.

Jim
VK7JH
MMedit
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2403
Posted: 05:51am 10 Mar 2025
Copy link to clipboard 
Print this post

More playing with Jim's program. Added some comments to help add unknown / non-genuine devices.
'DS18x20 test code by TassyJim
Print
' Input "Type Pin number ",PinNbr
Input "Type PicoMite GPxx number for the DS18B20 (include the GP) "; GPPin$
Dim INTEGER PinNbr = MM.Info(pinno GPPin$)

Print
Print "Family name: ";FamilyName$(PinNbr)
Print "Rom Code:    ";getRomCode$(PinNbr)
Print "ScratchPad:  ";getScratchpad$(PinNbr)

If powerMode(PinNbr) Then
  Print "Power mode   External"
Else
  Print "Power mode   Parasitic"
EndIf

Print
For n = 9 To 12
  Print
  x = setResolution(PinNbr, n)
  Print "ScratchPad:  ";getScratchpad$(PinNbr)
  Timer = 0
  Print "Temperature: ";getTemp(PinNbr);"`C  ";Timer;"mS,";n;" bit"
Next n

Print :t=Timer : Print "Tempr() without Start", TEMPR(PinNbr);" `C",Timer-t;" mS"
TEMPR START PinNbr,0 :t=Timer :ds18=TEMPR(PinNbr):t=Timer-t : Print "Tempr Start pin,0", ds18, t
TEMPR START PinNbr,1 :t=Timer :ds18=TEMPR(PinNbr):t=Timer-t : Print "Tempr Start pin,1", ds18, t
TEMPR START PinNbr,2 :t=Timer :ds18=TEMPR(PinNbr):t=Timer-t : Print "Tempr Start pin,2", ds18, t
TEMPR START PinNbr,3 :t=Timer :ds18=TEMPR(PinNbr):t=Timer-t : Print "Tempr Start pin,3", ds18, t
Print
Print "*** The first RomCode value (a1) can be used to add extra devices to the FamilyName & getTemp functions"
Print "look for *** comments in those functions"
Print
End


Function getRomCode$(PinNbr As INTEGER)
' get ROM Code - useful if you want more than 1 device on wire
'*** The first value returned (a1) can be used to add extra devices to the FamilyName & getTemp functions
'look for *** comments in those functions
Local INTEGER a1,a2,a3,a4,a5,a6,a7,a8
OneWire WRITE PinNbr,1,1,&h33 'read ROM code
OneWire READ PinNbr,0,8,a1,a2,a3,a4,a5,a6,a7,a8
getRomCode$ = Hex$(a1,2)+" "+Hex$(a2,2)+" "+Hex$(a3,2)+" "+Hex$(a4,2)+" "+Hex$(a5,2)+" "+Hex$(a6,2)+" "+Hex$(a7,2)+" "+Hex$(a8
,2)
End Function

Function getScratchpad$(PinNbr As INTEGER)
'ONEWIRE Write PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8  'read from scratchpad
OneWire WRITE PinNbr,1,2,&hcc,&hbe
OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h
getScratchpad$ = Hex$(a,2)+" "+Hex$(b,2)+" "+Hex$(c,2)+" "+Hex$(d,2)+" "+Hex$(e,2)+" "+Hex$(f,2)+" "+Hex$(g,2)+" "+Hex$(h,2)
End Function

Function powerMode(PinNbr As INTEGER) As INTEGER
' check whether power external (1) or parasitic (0)
OneWire RESET PinNbr
OneWire WRITE PinNbr, 1,2,&hcc, &hb4
OneWire READ PinNbr,4,1,powerMode
End Function

Function getFamily(PinNbr As INTEGER) As INTEGER
getFamily = Val("&h"+Left$(getRomCode$(PinNbr),2))
End Function

Function FamilyName$(PinNbr As INTEGER)
Local INTEGER fn
fn = getFamily(PinNbr)
Select Case fn
  Case 16  '&H10
    FamilyName$ = "DS1820/DS18S20"
  Case 34  '&H22
    FamilyName$ = "DS18S22"
  Case 40  '&H28
    FamilyName$ = "DS18B20"
  Case 41  '&H29
    FamilyName$ = "a non-genuine DS18B20"
  '*** Add extra cases here using the first value returned by Function getRomCode
  Case Else
    FamilyName$ = "Unknown"
End Select
End Function

Function setResolution(PinNbr As INTEGER,r As INTEGER)
r = r-9
If r < 0 Then r = 0
If r > 3 Then r = 3
r = r*32+31
OneWire WRITE PinNbr, 1,5,&hcc, &h4E, &hFF,&hFF,r
End Function

Function getTemp(PinNbr As INTEGER) As FLOAT
Local INTEGER fn, t, T1, T2, a,b,c,d,e,f,g,h
Local FLOAT Value
fn = getFamily(PinNbr)
power = powerMode(PinNbr)
OneWire RESET PinNbr                        ' reset before command
OneWire WRITE PinNbr, 8, 2, &hcc, &h44      ' start conversion

'read external when b goes high, for parasitic just wait
If power = 0 Then
  Pause 750
Else
  t = Timer
  Do
    If Timer - t > 1000 Then
      Value = 1000
      Print "*** Timeout ***"
      Exit Do
    EndIf
    OneWire READ PinNbr, 4 , 1 , b            ' conversion done?
  Loop Until b = 1
'   Print Timer-t;" mS conversion time",
EndIf
If Value = 0 Then ' we have not timed out yet
  OneWire WRITE PinNbr, 1, 2, &hcc, &hbe      ' command read data
  OneWire READ PinNbr, 2, 2, T1, T2           ' get the data
  OneWire RESET PinNbr
  Select Case fn
    Case 16 'DS18S20 or DS1820
      OneWire WRITE PinNbr,1,2,&hcc,&hbe
      OneWire READ PinNbr,0,8,a,b,c,d,e,f,g,h
      If T2 And &h80 Then       'if MSB of T2=1 then negative
        'Read 12bit resolution and adjust
        'truncate 0.5deg value (or use integer division \)
        T1 = T1 And &hFE
        T1=T1 / 2                   'make whole degrees
        'add compensation values read from scratchpad
        T1=T1*16                  'make lsb 1/16 degree
        T1 = T1 -4 +(16 - g)    'add 12 bit value
        'take 2s complement
        T1 = (T1 Xor &hFF) + 1
        Value = -T1/16                'make decimal value in degrees
      Else                              'positive temp
        Value = T1 / 2                '9bit value
        'Read 12bit resolution and adjust
        T1 = T1 And &hFE      'truncate 0.5deg(or use integer division \)
        Value = T1/2- 0.25 + (16 - g) /16   '12 bit value
      EndIf
    Case 34, 40 ' DS18S22 or DS18B20
      If T2 And &b1000 Then       'negative temp
        'make 2s complement (1s complement+1)
        T2 = (T2 Xor &hFF)
        T1 = (T1 Xor &hFF)+1
        If T1=1 Then T2=T2+1        'add the carry if required
        Value = -((T2 And &b111) * 256 + T1) / 16
      Else                            'positive temp
        Value = ((T2 And &b111) * 256 + T1) / 16
      EndIf
    Case 41 '&H29, a non genuine DS18B20
      If T2 And &b1000 Then       'negative temp
        'make 2s complement (1s complement+1)
        T2 = (T2 Xor &hFF)
        T1 = (T1 Xor &hFF)+1
        If T1=1 Then T2=T2+1        'add the carry if required
        Value = -((T2 And &b111) * 256 + T1) / 16
      Else                            'positive temp
        Value = ((T2 And &b111) * 256 + T1) / 16
      EndIf
    '*** Add extra cases here using the first value returned by Function getRomCode
    Case Else 'Unknown device
       ' *** use the output of this to create the scale factor of the unknown device
      'Value = 1000
      If T2 And &b1000 Then       'negative temp
        'make 2s complement (1s complement+1)
        T2 = (T2 Xor &hFF)
        T1 = (T1 Xor &hFF)+1
        If T1=1 Then T2=T2+1        'add the carry if required
        Value = -((T2 And &b111) * 256 + T1) / 1'<-This is where you put the scale factor
      Else                            'positive temp
        Value = ((T2 And &b111) * 256 + T1) / 1'<-This is where you put the scale factor
      EndIf
  End Select
EndIf
getTemp= Value
End Function

Example output. This one always reads 85° until after a Tempr Start pin,2 or 3 command then is ok till next power cycle.
> RUN

Type PicoMite GPxx number for the DS18B20 (include the GP) ? gp6

Family name: a non-genuine DS18B20
Rom Code:    29 61 64 12 3D D4 A4 B8
ScratchPad:  51 05 FF FF 7F FF FF FF
Power mode   External


ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  85.0625`C   17.934mS, 9 bit

ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  85.0625`C   17.951mS, 10 bit

ScratchPad:  A8 82 FF FF AF FF FF FF
Temperature:  680`C   17.91mS, 11 bit

ScratchPad:  51 05 FF FF 7F FF FF FF
Temperature:  85.0625`C   17.935mS, 12 bit

Tempr() without Start    85 `C   210.435 mS
Tempr Start pin,0        85      103.408
Tempr Start pin,1        85      203.84
Tempr Start pin,2        27.875  403.835
Tempr Start pin,3        27.875  803.819

*** The first RomCode value (a1) can be used to add extra devices to the FamilyName & getTemp functions
look for *** comments in those functions

> RUN

Type PicoMite GPxx number for the DS18B20 (include the GP) ? gp6

Family name: a non-genuine DS18B20
Rom Code:    29 61 64 12 3D D4 A4 B8
ScratchPad:  BF 01 FF FF 7B FF FF FF
Power mode   External


ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  27.9375`C   17.964mS, 9 bit

ScratchPad:  FF FF FF FF FF FF FF FF
Temperature:  27.9375`C   17.942mS, 10 bit

ScratchPad:  DF 80 FF FF AF FF FF FF
Temperature:  223`C   17.93mS, 11 bit

ScratchPad:  BF 01 FF FF 7F FF FF FF
Temperature:  27.9375`C   17.942mS, 12 bit

Tempr() without Start    27.875 `C       210.452 mS
Tempr Start pin,0        27.875  104.167
Tempr Start pin,1        27.875  203.82
Tempr Start pin,2        27.6875         403.821
Tempr Start pin,3        27.6875         803.819

*** The first RomCode value (a1) can be used to add extra devices to the FamilyName & getTemp functions
look for *** comments in those functions

Edit.
Prompted by Volhout's mention of the RP2350 pin issue I reduced the pullup resistor from 10k to 1.2k. Now the Rom Code value of a1 is &H28, as it should be. Was &H29.
It no longer reads 85°C in Jim's program but still requires an initial Tempr Start to get Tempr() to read correctly.
Edited 2025-03-10 20:08 by phil99
 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 11:41am 10 Mar 2025
Copy link to clipboard 
Print this post

Very helpful, many thanks for sharing! Will try later on
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2403
Posted: 04:33am 13 Mar 2025
Copy link to clipboard 
Print this post

For anyone with a DS18x20 device that doesn't work with Tempr() but does give sensible results with Jim's test program here is a Function derived from Jim's program.

Usage:-
 Print DS18x20(pin); "`C"
Or
 Temp = DS18x20(mm.info(pinno GPxx)) : Print Temp; "`C"

There are some comments to help adapt it to your device.

Function DS18x20(PinNbr As INTEGER) As FLOAT
' Extracted from DS18x20 test code by TassyJim
' Run that program and find the values that get your device to work.
' Copy those values into this Function
 Local INTEGER  t, T1, T2, done
 Local FLOAT Value=0

 OneWire RESET PinNbr                        ' reset before command
 OneWire WRITE PinNbr, 8, 2, &hcc, &h44      ' start conversion

 t = Timer
 Do               'There should be a result in less than 1000mS
   If Timer - t > 1000 Then
     Value = 1000 'standard error code
     Print "*** Timeout ***",
     Exit Do
   EndIf

 OneWire READ PinNbr, 4 , 1 , done       ' conversion done?
 Loop Until done
'  Print Timer - t;" mS conversion time", 'un-comment this to see how long it took
 If Value = 0 Then ' we have not timed out yet, so assume data is valid
   OneWire WRITE PinNbr, 1, 2, &hcc, &hbe      ' command read data
   OneWire READ PinNbr, 2, 2, T1, T2           ' get the data

   OneWire RESET PinNbr
    If T2 And &b1000 Then       'negative temp
      'make 2s complement (1s complement+1)
      T2 = (T2 Xor &hFF)
    T1 = (T1 Xor &hFF)+1

    If T1=1 Then T2=T2+1        'add the carry if required
      Value = -((T2 And &b111) * 256 + T1) / 16 'adjust this if the temp. is wrong
     Else                            'positive temp
      Value = ((T2 And &b111) * 256 + T1) / 16 'adjust this if the temp. is wrong
    EndIf
 EndIf
 DS18x20 = Value
End Function
 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 07:52am 13 Mar 2025
Copy link to clipboard 
Print this post

Many thanks, I appreciate that!

I found, that my sensor is reliably answering after 600ms for 9 and 10 bit.
Knowing that, I code regarding it. Your function does not deliver earlier I guess.
So the best would be, that tempr() would have parameters to overwrite the datasheet defaults. Anyway, tempr() is not usable at all for me only tempr start.
So if I would really want to have the reading right now, maybe this function comes into play.In any case it's good to know how it is done and you gave the shortcut..thanks!
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2403
Posted: 08:07am 13 Mar 2025
Copy link to clipboard 
Print this post

If you post the results of TassyJim's test program here I may be able to adapt the Function to your unit.

As it is now the Function simply waits as long as necessary to get a reading (up to 1S). However if the 'x' in DS18x20 isn't 'B' or 'S' some changes may need to be made.
 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 08:27am 13 Mar 2025
Copy link to clipboard 
Print this post

First breakfast :)

Does it make a difference to?
tempr start(xx):pause xx:? tempr(xx)
 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 09:20am 13 Mar 2025
Copy link to clipboard 
Print this post

your function works very nice:
> RUN
473.178 mS conversion time      22.4375`C
473.595 mS conversion time      22.4375`C
473.79 mS conversion time       22.4375`C
473.998 mS conversion time      22.4375`C
473.204 mS conversion time      22.4375`C
473.388 mS conversion time      22.4375`C
473.607 mS conversion time      22.4375`C
473.853 mS conversion time      22.4375`C
474.02 mS conversion time       22.4375`C
473.178 mS conversion time      22.5`C
474.091 mS conversion time      22.5`C
>
 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 09:31am 13 Mar 2025
Copy link to clipboard 
Print this post

My 600ms I have lived with could be tuned a bit :)

Type PicoMite GPxx number for the DS18B20 (include the GP) ? gp15

Family name: DS18B20
Rom Code:    28 E9 CA 43 D4 E1 3C 7D
ScratchPad:  68 01 55 05 7F A5 A5 66
Power mode   External


ScratchPad:  68 01 FF FF 1F A5 A5 66
Temperature:  22`C   493.538mS, 9 bit

ScratchPad:  60 01 FF FF 3F A5 A5 66
Temperature:  22.25`C   493.751mS, 10 bit

ScratchPad:  64 01 FF FF 5F A5 A5 66
Temperature:  22.375`C   493.908mS, 11 bit

ScratchPad:  66 01 FF FF 7F A5 A5 66
Temperature:  22.4375`C   493.918mS, 12 bit

Tempr() without Start    1000 `C         209.257 mS
Tempr Start pin,0        1000    99.321
Tempr Start pin,1        22.25   204.063
Tempr Start pin,2        1000    399.376
Tempr Start pin,3        22.4375         803.979

*** The first RomCode value (a1) can be used to add extra devices to the FamilyName & getTemp functions
look for *** comments in those functions

>

 
dddns
Senior Member

Joined: 20/09/2024
Location: Germany
Posts: 212
Posted: 09:36am 13 Mar 2025
Copy link to clipboard 
Print this post

> do:tempr start gp15,1:pause 474:? tempr(gp15):loop
22.25
22.25
22.25
22.25
22.25
22.25
22.25
22.25
22.25
>


> do:tempr start gp15,1:pause 472:? tempr(gp15):loop
1000
1000
1000
1000
1000
1000
1000
>

 
     Page 6 of 7    
Print this page
© JAQ Software 2025