| |
Page 4 of 4 |
| Author |
Message |
Peter63 Senior Member
 Joined: 28/07/2017 Location: SwedenPosts: 196 |
| Posted: 05:35am 30 May 2026 |
Copy link to clipboard |
 Print this post |
|
Jepp |
| |
Bryan1
 Guru
 Joined: 22/02/2006 Location: AustraliaPosts: 2069 |
| Posted: 05:47am 30 May 2026 |
Copy link to clipboard |
 Print this post |
|
Well I decided to setup my 2040 Zero with that ili9488 now it's free and gave this DS1820 code a go, now I was having no luck so went back to the start of this thread and saw Tempr(GP9,500) was the fix to get the DS1820 working.
Now when I run the code.
50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 680°C 50 Temp = 154°C 50 Temp = 154°C 50 Temp = 154°C 50 > ? tempr (Gp9, 500) 19.25 >
Nothing changed in the code just copied it and pasted into MMEdit. Anyway the good thing the DS1820 looks to be working |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 06:13am 31 May 2026 |
Copy link to clipboard |
 Print this post |
|
Ok, so if you don't know which DS18x20 you have these two functions should work with any, including slow ones. In the first one you can optionally set the Conversion Time in mS. Usage:- Print DS18x20ct(mm.info(pinno GPxx)) ' the default Conversion Time is 750mS or Print DS18x20ct(mm.info(pinno GPxx, mS_delay) '1000mS should cover most.
The second doesn't need it as it checks if the conversion is finished before reading. If not finished it returns to the main loop. After reading it starts a new conversion, whish happens in the background as it returns to the main loop. Usage:- Print DS18x20(mm.info(pinno GPxx))
Function DS18x20ct(PinNbr As INTEGER, Delay As INTEGER) As FLOAT ' derived from DS18x20 test code by TassyJim Local INTEGER T1,T2,T3,CpC,F OneWire WRITE PinNbr,1,1,&h33 'read "family" ROM code OneWire READ PinNbr,0,1,F ':Print F, "16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20", OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44 ' start conversion, adjust for slow units If Delay < 10 then Delay = 750 ' maximum normal conversion time Pause Delay OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 1, 2, &hCC, &hBE ' command read data OneWire READ PinNbr, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC 'get the data 'process the data If F=16 Then DS18x20ct = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25 + (CpC-T3)/CpC If F=40 or F=34 Then DS18x20ct = Str2bin(int16,Chr$(T1)+Chr$(T2))/16 End Function
PinNbr = MM.Info(pinno GP7) Dim DStemp = DS18x20(PinNbr) 'purge old data and start new conversion Dim integer n
Do 'Main Loop t = timer Print n; DStemp = DS18x20(PinNbr) 'get Temp, if ready If DStemp <> 1000 Then Print Print "Temp =";DStemp;"`C" n = 0 EndIf Do : Loop until Timer > 49.9 inc n,50 Loop
Function DS18x20(PinNbr As INTEGER) As FLOAT ' Adapted from DS18x20 test code by TassyJim Local INTEGER done, T1, T2, T3, CpC, F DS18x20 = 1000 ' standard error code OneWire READ PinNbr, 4, 1, done ' is conversion finished? If done = 0 Then Exit Function ' if not return to program 'Get the data OneWire WRITE PinNbr,1,1,&h33 'read "family" ROM code OneWire READ PinNbr,0,1,F ':Print F; " 16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20", OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 1, 2, &hCC, &hBE ' command read data OneWire READ PinNbr, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC ' get the data ' process the data If F=16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25 + (CpC-T3)/CpC If F=40 or F=34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16 DS18x20 = Cint(Value * 10) / 10 'round to 1/10ths
'Start conversion, ready for next read OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44 ' start conversion End Function End |
| |
Bryan1
 Guru
 Joined: 22/02/2006 Location: AustraliaPosts: 2069 |
| Posted: 07:00am 31 May 2026 |
Copy link to clipboard |
 Print this post |
|
Thanks for that Phil just tried it
Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 85°C 50 Temp = 18.1°C 50 Temp = 18.1°C 50 Temp = 18.1°C 50 Temp = 18.1°C 50 Temp = 18.1°C 50
I did get these sealed can temp sensors about 15-16 years ago so they may be DS1820's that need the delay for it to work.
Anyway it is time to make a new thread with my project for the temp sensor.
Regards Bryan |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 12:32pm 31 May 2026 |
Copy link to clipboard |
 Print this post |
|
Have been puzzled by your output only showing "50" between reads. Each 50 is 50mS so you should get something like this.
50 100 150 200 250 300 350 400 450 500 Temp = 19.2`C 50 100 150 200 250 300 350 400 450 500 Temp = 19.3`C
Then when I tried a non-genuine DS18B20 I got the same as you. Further investigation showed it says it has completed the temperature conversion in 33mS. But it hasn't actually finished so the data you get is from a previous read. That is why you got a string of 85°C readings, then the correct temp.
For these non-genuine units you are better off using the MMBasic TEMPR START GPxx,3 before the TEMPR(GPxx) function. The ",3" gives maximum resolution and ensures TEMPR(GPxx) waits long enough before reading.
The example above shows TEMPR(GPxx) immediately following TEMPR START but it doesn't need to. You can put other code between to make use of the 600mS or so it takes to do a conversion. Edited 2026-06-01 08:00 by phil99 |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 10:56pm 31 May 2026 |
Copy link to clipboard |
 Print this post |
|
More experimenting with the nongenuine DS18B20.
It's conversion time is much faster than for the genuine unit. It does high res 1/16° conversions in less than 34mS. With the temperature changing rapidly it really does get new values that fast.
It's accuracy, however appears to be a little less, almost 1° high compared to a genuine unit and a pair of AHT10s.
Edit.
> 'Non-genuine DS18B20 > t2=timer:OneWire WRITE 9,1,2,204,68:t=timer:do:OneWire READ 9,4,1,d:loop until d :? timer-t,timer-t2 33.237 35.562 > ' So about 2.3mS to start conversion and 33.3mS to carry it out. >'genuine DS18B20 > t2=timer:OneWire WRITE 10,1,2,204,68:t=timer:do:OneWire READ 10,4,1,d:loop until d:?timer-t,timer-t2 479.366 481.694 > Edited 2026-06-01 11:09 by phil99 |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 08:57am 02 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
A number of DS18x20 can run from the same OneWire bus by using the unique ROM code each contains. The codes can be read by copying and pasting the command lines below into TeraTerm. Only one device may be on the bus while reading the code. Ignore the output numbers in the example
> clear : dim integer q,w,e,r,t,y,u,i,ROM, PinNo=mm.info(pinno GP6) > OneWire Reset PinNo : OneWire WRITE PinNo,1,1,&h33 : OneWire READ PinNo,2,8,q,w,e,r,t,y,u,i : ? ,q,w,e,r,t,y,u,i 40 63 68 87 4 225 60 77 > RC$ = chr$(q)+chr$(w)+chr$(e)+chr$(r)+chr$(t)+chr$(y)+chr$(u)+chr$(i) : ROM = str2bin(Uint64,RC$,big) :? ROM 2900111825584602189 > Its a bit clunky but OneWire doesn't use arrays or strings.
Insert your ROM code numbers and OneWire pin number in the program below. This example is for two but could be modified for more.
' running 2 DS18x20 sensors on the same pin. Get their ROM codes an replace those below Dim PinNbr = MM.Info(pinno gp6) Dim integer n, ROMcode(1) = (2909716863767585976, 2900111825584602189)
Dim DStemp0 = DS18x20(PinNbr, ROMcode(0)) 'purge old data and start new conversion Dim DStemp1 = DS18x20(PinNbr, ROMcode(1)) 'purge old data and start new conversion
Do ' Main Loop D = DS18x20(PinNbr, ROMcode(n)) If D<>1000 Then Print "Device";n; D;"`C", If n=1 Then Print Pause 1000 n = Not n Loop
Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT ' Adapted from DS18x20 test code by TassyJim Local INTEGER done, T1, T2, T3, CpC, Fam, a,b,c,d,e,f,g,h DS18x20 = 1000' standard error code OneWire READ PinNbr, 4, 1, done' is conversion finished? If done = 0 Then Exit Function ' if not return to program
If ROM Then a=ROM>>56 : b=ROM>>48 And 255 : c=ROM>>40 And 255 : d=ROM>>32 And 255 e=ROM>>24 And 255 : f=ROM>>16 And 255 : g=ROM>>8 And 255 : h=ROM And 255 Fam=a ': Print a,b,c,d,e,f,g,h OneWire WRITE PinNbr,1,1,&h55 ' reset then match ROM Code command OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h ' send ROM Code OneWire WRITE 9,0,1,&hBE ' read scratchpad command OneWire READ 9, 0, 8, T1,T2,T3,T4,T5,T6,T7,CpC' get the data Else OneWire WRITE PinNbr,1,1,&h33 ' read "family" ROM code OneWire READ PinNbr,0,1,Fam EndIf
' process the data 16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20 If Fam = 16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25+(CpC-T3)/CpC If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16 DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths
' Start conversion, ready for next read If ROM Then OneWire WRITE PinNbr,1,1,&h55 'reset then match ROM Code command OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h 'send ROM Code OneWire WRITE PinNbr,2,1,&h44 'Master issues Convert Temp. command Else OneWire RESET PinNbr' ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44' ' start conversion EndIf End Function End
Footnote added 2026-06-02 21:49 by phil99 A sample of the output.Device 0 24.19`C Device 1 31.69`C Device 0 23.38`C Device 1 31.56`C Device 0 22.69`C Device 1 31.44`C Device 0 22.13`C Device 1 31.25`C Device 0 21.63`C Device 1 31.13`C Device 0 21.25`C Device 1 31`C Device 0 20.88`C Device 1 30.88`C Device 0 20.63`C Device 1 30.75`C Device 0 20.38`C Device 1 30.63`C Device 0 20.19`C Device 1 30.5`C
Footnote added 2026-06-02 22:01 by phil99 Noticed a copy/paste error tat would affect the fractions of degrees for DS18S20 sensors. Replace this line:- OneWire READ 9, 0, 8, T1,T2,T3,T4,T5,T6,T7,CpC' get the data with OneWire READ 9, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC' get the data
Most of the T3s are dummys, only the one before CpC gives the fractions. |
| |
matherp Guru
 Joined: 11/12/2012 Location: United KingdomPosts: 11435 |
| Posted: 10:05am 02 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
phil99: what firmware version are you using? I'll post an update with flexible onewire data parameters for you to test |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 10:56am 02 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
PicoMiteVGA MMBasic RP2350A Edition V6.03.00RC12
Or plain RP2040 would also do.
Thanks very much for considering this. |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 05:39am 03 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
The latest PicoMite firmware - V6.03.00RC15 can now use strings and arrays in OneWire commands. Here is the previous program converted to use both. A more streamlined result and simpler to write.
' Running 2 DS18x20 sensors on the same pin. Get their ROM codes and replace those below. Dim PinNbr = MM.Info(pinno gp6) Dim integer n=0, ROMcode(1) = (2909716863767585976, 2900111825584602189)
Dim DStemp0 = DS18x20(PinNbr, ROMcode(0)) 'purge old data and start new conversion Pause 750 Dim DStemp1 = DS18x20(PinNbr, ROMcode(1)) 'purge old data and start new conversion
Do ' Main Loop D = DS18x20(PinNbr, ROMcode(n)) If D<>1000 Then Print "Device";n; D;"`C", If n=1 Then Print Pause 1000 n = Not n Loop
Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT ' Adapted from DS18x20 test code by TassyJim ' This version for MMbasic v6.03.00RC15 with array and string support for OneWire Local INTEGER done, T(7), Fam = ROM >> 56 'get family code from ROM code Local ROMcode$ = Bin2str$(Uint64,ROM,big) 'convert ROM code to string DS18x20 = 1000' standard error code OneWire READ PinNbr, 4, 1, done' is conversion finished? If Not done Then Exit Function ' if not return to program
If ROM Then OneWire WRITE PinNbr, 1, 1, &h55 ' reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ ' send ROM Code to the DS18x20 OneWire WRITE PinNbr, 0, 1, &hBE ' read scratchpad command Else OneWire WRITE PinNbr,1,1,&h33 'If no ROM code given assume only one DS18x20 connected OneWire READ PinNbr, 0, 1, Fam ' read "family" ROM code OneWire WRITE PinNbr, 1, 2, &hCC, &hBE ' command read data EndIf OneWire READ PinNbr, 0, 8, T() ': Math V_Print T()' get the data
' process the data. Fam = Family ROM codes - 16=DS1820/DS18S20, 34=DS18S22, 40=DS18B20 If Fam = 16 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1))\2 -0.25+(T(7)-T(6))/T(7)) If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1)))/16 DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths
' Start conversion, ready for next read If ROM Then OneWire WRITE PinNbr, 1, 1, &h55 'reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ 'send ROM Code OneWire WRITE PinNbr, 2, 1, &h44 'Master issues Convert Temp. command Else OneWire RESET PinNbr' ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44' ' start conversion EndIf End Function End Output.Device 0 13.06`C Device 1 22.56`C Device 0 23`C Device 1 22.56`C Device 0 23.06`C Device 1 22.56`C Device 0 23.06`C Device 1 22.56`C Device 0 23.06`C Device 1 22.5`C
Reading the ROM codes is simpler too.
> clear : dim integer ROM, PinNo=mm.info(pinno GP6) > OneWire WRITE PinNo, 1, 1, &h33 : OneWire READ PinNo, 2, 8, RC$ > ROM = str2bin(Uint64,RC$,big) : ? ROM 2909716863767585976 > Edited 2026-06-03 23:01 by phil99 |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 05:05am 04 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
Attempts to get the DS18B20s to work in Parasite Power mode have failed so here is a hardware solution that does work.
 The component values are not critical, try whatever you have. I have also tested 1.2kΩ and 10µF powering two DS18B20s. |
| |
phil99
 Guru
 Joined: 11/02/2018 Location: AustraliaPosts: 3268 |
| Posted: 01:09pm 05 Jun 2026 |
Copy link to clipboard |
 Print this post |
|
Yet another tweak. As before many DS18x20s can be on the OneWire bus if the unique ROM code of each is provided. If no ROM code is provided it assumes there is only one device on the bus and reads the temperature and reads its unique ROM code. The ROM code and Family code are then available in the global variables ROM_Code% and Family%. If your program uses OPTION EXPLICIT you will need to Dim them, if you are not supplying the ROM codes every time you call the function.
Command line usage:-
> clear : PinNo = mm.info(pinno GP6) : ? ds18x20(9,2900111825584602189) 21.375 > > clear : PinNo = mm.info(pinno GP6) : ? ds18x20(PinNo), ROM_Code%, Family% 21.4375 2900111825584602189 40 >
Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT ' Adapted from DS18x20 test code by TassyJim ' This version for MMbasic v6.03.00RC15 with array and string support for OneWire Local INTEGER done, T(7), Fam Local ROMcode$ = Bin2str$(Uint64,ROM,big) 'convert ROM code to string DS18x20 = 1000' standard error code If ROM > -1 Then Fam = ROM >> 56 OneWire READ PinNbr, 4, 1, done' is conversion finished? If Not done Then Exit Function ' if not return to program EndIf
If ROM > 0 Then OneWire WRITE PinNbr, 1, 1, &h55 ' reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ ' send ROM Code to the DS18x20 OneWire WRITE PinNbr,0,1,&hBE ' read scratchpad command Else OneWire WRITE PinNbr, 1, 1, &h33 'If no ROM code given assume only one DS18x20 connected OneWire READ PinNbr, 0, 8, ROMcode$ ' Read the ROM code OneWire WRITE PinNbr, 1, 2, &hCC, &hBE ' read data command ROM_Code% = Str2bin(Uint64,ROMcode$,big) ' Output the ROM code to a Global variable Fam = ROM_Code% >> 56 : Family% = Fam ' extract "family" code to a Global EndIf OneWire READ PinNbr, 0, 8, T() ' : Math V_Print T()' get the temperature data
' process the data, Family ROM codes - 16=DS1820/DS18S20, 34=DS18S22, 40=DS18B20 If Fam = 16 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1))\2 -0.25+(T(7)-T(6))/T(7)) If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T(0))+Chr$(T(1)))/16 If Fam = 0 Then Value = 1001 DS18x20 = Value 'round to 1/100ths
' Start conversion, ready for next read If ROM Then OneWire WRITE PinNbr, 1, 1, &h55 'reset then match ROM Code command OneWire WRITE PinNbr, 0, 8, ROMcode$ 'send ROM Code OneWire WRITE PinNbr, 8, 1, &h44 'Master issues Convert Temp. command Else OneWire RESET PinNbr' ' reset before command OneWire WRITE PinNbr, 8, 2, &hCC, &h44' ' start conversion EndIf ' Print "conversion started" End Function |
| |
| |
Page 4 of 4 |