Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:45 02 Jul 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 : Help with 1-Wire Temp reading.

Author Message
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:19pm 31 Mar 2016
Copy link to clipboard 
Print this post

Mentioned this in another thread.

I'm wondering what's involved in reading multiple DS18B20's on a single data pin.

Have read what's done on RPi in Silicon Chips article & get a vague idea, looked at the DS18B20 Data Sheet & see some relevant commands I don't quite understand.

Can anyone explain what would be required to:-

1. Read the serial numbers from the individual devices, presumably connected one at a time.

2. Then read a specific sensor on the bus based on it's serial number.

I've looked at Geoff's code in the MM Library, ONE_WIRE.BAS & assume if the serial numbers were know, the extra commands could be added to read a specific device.

  Quote  
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Demonstrate how to use the one-wire functions
' Geoff Graham July 2013
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' use a defined function to get the temperature
' The sensor is connected to the pin 18
Print "The temperature is:" GetTemp(18)


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function to get the temperature from a Dallas DS18B20.
' The DS18B20 is connected to the pin specified by PinNbr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetTemp (PinNbr)
Local T1, T2, b, t
OWReset PinNbr ' reset
OWWrite PinNbr, 1, 2, &hcc, &h44 ' start conversion
Pause 100
t = Timer
Do
If Timer - t > 1000 Then Error "Sensor not responding"
OWRead PinNbr, 4 , 1 , b ' conversion done?
Loop Until b = 1
OWWrite PinNbr, 1, 2, &hcc, &hbe ' command read data
OWRead PinNbr, 2, 2, T1, T2 ' get the data
GetTemp = ((T2 And &b111) * 256 + T1) / 16
If T2 And &b1000 Then GetTemp = -GetTemp ' adjust if negative
End Function


I am aware that the One wire command has been changed since to example was written
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 07:53pm 31 Mar 2016
Copy link to clipboard 
Print this post

Hi
Unfortunately the OWSEARCH command is no longer supported so you cannot
search for multiple devices on the same pin.

Below is some updated code to read a single sensor
If you really want to do this then maybe ask matherp
if this is possible with a CFunction



GetTemp 14, Temp, &H7F
Print "The temperature is:" Temp

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Subroutine to get the temperature from a Dallas DS18B20.
' Usage GetTemp (Pin number of DS18B20), (Temperature Variable), (Resolution)
' Resolution Table (Resolution Variable)
' &H1F = 9 bits 93.75ms Conversion Time
' &H3F = 10 Bits 187.5ms Conversion Time
' &H5F = 11 Bits 375ms Conversion Time
' &H7F = 12 Bits 750ms Conversion Time
' Temperature of 127 will be displayed
' in the case of a sensor fault
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetTemp (PinNbr, Value, Resolution)
Local TH, TL, b, t

OneWire Reset PinNbr ' reset
OneWire Write PinNbr, 2, 5, &HCC, &H4E, &H00, &H00, Resolution
OneWire Write PinNbr, 1, 2, &HCC, &H44

t=Timer
Do
If Timer - t > 1000 Then Error "Sensor Error"
OneWire Read PinNbr, 4, 1, b ' Conversion status
Loop Until b = 1

OneWire Write PinNbr, 1, 2, &HCC, &HBE
OneWire Read PinNbr, 2, 2, TL, TH

Value = ((TH And &b111) * 256 + TL) / 16
If T2 And &b1000 Then Value = -Value ' adjust if negative

End Sub


Regards
Jman
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 11:35pm 31 Mar 2016
Copy link to clipboard 
Print this post

Found this which might help me identify the sensors & code them manually, or possibly load the data in an array for later use.

Not sure what's missing, or how to put it all together.

Search routine

And this that will read multiple sensors.

Multiple OneWire devices on the same bus

Just not sure what needs modifying.

Any suggestions?

Thanks

Phil

 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 11:49pm 01 Apr 2016
Copy link to clipboard 
Print this post

Hi

After a little thought and a look at the datasheet see below for
some code that will get your sensors serial and crc

Option Explicit
Dim OW1,OW2,OW3,OW4,OW5,OW6,OW7,OW8
Dim PinNbr
PinNbr = 17

OneWire Reset Pinnbr
OneWire Write PinNbr, 1, 1, &H33 ' Issue Read ROM command
OneWire Read PinNbr, 2, 8, OW1,OW2,OW3,OW4,OW5,OW6,OW7,OW8

Print "Temp Sensor Should be 28 / ";Hex$(OW1);"H"
Print "Ser# ",Hex$(OW2,2);Hex$(OW3,2);Hex$(OW4,2);Hex$(OW5,2);Hex$(OW6,2);Hex$(OW7,2);"H"
Print "CRC ";Hex$(OW8,2);"H"


Make a note of the details then use the code below to read your sensors
I have tried this with two sensors and it seems to work ok.
You will need to change the serial and crc to your sensor values
IE &H55,&H28,XX,XX,XX,XX,XX,XX,XX,&H44 were xx are your sensor values


Option Explicit
Dim OW1,OW2,OW3,OW4,OW5,OW6,OW7,OW8
Dim LTemp, HTemp, Value, PinNbr, Status
PinNbr = 17

OneWire Reset Pinnbr

'Put 1st sensor serial and crc here &H55,&H28,XX,XX,XX,XX,XX,XX,XX,&H44
OneWire Write PinNbr, 1, 10, &H55,&H28,&H39,&HE5,&H2B,&H06,&H00,&H00,&H58,&H44
StatusCheck 'check sensor is ready
OneWire Write PinNbr, 1, 10, &H55,&H28,&H39,&HE5,&H2B,&H06,&H00,&H00,&H58,&HBE
OneWire Read PinNbr, 2, 2, LTemp, HTemp

TempCalc 'Work out the temp in C
Print "1st Sensor ";Value;"C"

'Put 2nd sensor serial and crc here
OneWire Write PinNbr, 1, 10, &H55,&H28,&H63,&HDB,&H2A,&H06,&H00,&H00,&H10,&H44
StatusCheck 'check sensor is ready
OneWire Write PinNbr, 1, 10, &H55,&H28,&H63,&HDB,&H2A,&H06,&H00,&H00,&H10,&HBE
OneWire Read PinNbr, 2, 2, LTemp, HTemp

TempCalc 'Work out the temp in C
Print "2nd Sensor ";Value;"C"

End
'=========================================================
Sub StatusCheck
Timer=0 : Status=0
Do
If Timer > 1000 Then Error "Sensor Error"
OneWire Read PinNbr, 4, 1, Status ' Conversion status
Loop Until Status = 1
End Sub

'=========================================================
Sub TempCalc
Value = ((HTemp And &b111) * 256 + LTemp) / 16
If HTemp And &b1000 Then Value = -Value ' adjust if negative
End Sub

'=========================================================



Hope this helps

Regards
Jman
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 01:26am 02 Apr 2016
Copy link to clipboard 
Print this post

  jman said   Hi

After a little thought and a look at the datasheet see below for
some code that will get your sensors serial and crc

Make a note of the details then use the code below to read your sensors
I have tried this with two sensors and it seems to work ok.
You will need to change the serial and crc to your sensor values
IE &H55,&H28,XX,XX,XX,XX,XX,XX,XX,&H44 were xx are your sensor values


Thanks Jman.

I run the first routine & it returns:-

Temp Sensor Should be 28 / 28H
Ser# FFA667A81501H
CRC 6FH

13 Plus 3 characters Vs 14 in the string above.

Just not sure how that fits in with the line below.



'Put 1st sensor serial and crc here &H55,&H28,XX,XX,XX,XX,XX,XX,XX,&H44
OneWire Write PinNbr, 1, 10, &H55,&H28,&H39,&HE5,&H2B,&H06,&H00,&H00,&H58,&H44


Phil
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:15am 02 Apr 2016
Copy link to clipboard 
Print this post

Hi Phil

Your sensor details inserted would be as below
Remember when checking for the serial numbers you can
only have ONE sensor connected at a time


&H55,&H28,&HFF,&HA6,&H67,&HA8,&H15,&H01,&H6F,&H44


Regards
Jman
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:59am 02 Apr 2016
Copy link to clipboard 
Print this post

  jman said   Hi Phil

Your sensor details inserted would be as below
Remember when checking for the serial numbers you can
only have ONE sensor connected at a time


&H55,&H28,&HFF,&HA6,&H67,&HA8,&H15,&H01,&H6F,&H44


Regards
Jman


Thanks again,

My rusty memory really showing here. I was considering the "H" in both strings to be part of the serial$ & CRC.

It's been a while....

Was aware of the single device limitation for obtaining the Serial#'s

Cheers.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:22am 04 Apr 2016
Copy link to clipboard 
Print this post

Still not having complete success here.

Still only have a single device on Pin 22, and it's working Ok with the TEMPR() command.

I can read the ID of the sensor Ok with either of Jmans routines, but am not reading the temp successfully.
> RUN
LTemp 255
HTemp 255
1st Sensor -127.938C
>


Seems like ONEWIRE READ is returning 255 for the 2 variables.

Option Explicit
Dim OW1,OW2,OW3,OW4,OW5,OW6,OW7,OW8
Dim LTemp, HTemp, Value, PinNbr, Status
PinNbr = 22

OneWire Reset Pinnbr

'Put 1st sensor serial and crc here &H55,&H28,XX,XX,XX,XX,XX,XX,XX,&H44
OneWire Write PinNbr, 1, 10, &H55,&H28,&HFF,&HA6,&H67,&HA8,&H15,&H01,&H6F,&H44
StatusCheck 'check sensor is ready
OneWire Write PinNbr, 1, 10, &H55,&H28,&HFF,&HA6,&H67,&HA8,&H15,&H01,&H6F,&HBE
OneWire Read PinNbr, 2, 2, LTemp, HTemp

print "LTemp", LTemp
print "HTemp", HTemp

TempCalc 'Work out the temp in C
Print "1st Sensor ";Value;"C"

'Put 2nd sensor serial and crc here
'OneWire Write PinNbr, 1, 10, &H55,&H28,&H63,&HDB,&H2A,&H06,&H00,&H00,&H10,&H44
'StatusCheck 'check sensor is ready
'OneWire Write PinNbr, 1, 10, &H55,&H28,&H63,&HDB,&H2A,&H06,&H00,&H00,&H10,&HBE
'OneWire Read PinNbr, 2, 2, LTemp, HTemp

'TempCalc 'Work out the temp in C
'Print "2nd Sensor ";Value;"C"

End
'=========================================================


Have my correct ID & just commented out the 2nd sensor code & added Print to LTemp & HTemp.

Any ideas where I'm going wrong?

Cheers

Phil
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:43am 05 Apr 2016
Copy link to clipboard 
Print this post

Ok,

Back to basics regarding the above post.

Do the DS18B20's work as multiple devices on a single bus in parasitic mode?

Just noticed in Table 3, Page 12 of the Data Sheet that it's mentioned that the 44h command is not applicable for parasite powered DS18B20s.

So am I correct in now thinking I need the devices correctly powered & data on it's own wire.

Thanks.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:55am 05 Apr 2016
Copy link to clipboard 
Print this post

More confused now......

The flow chart on page 14 seems to indicate that the 44h command can be used in parasite power mode.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:09pm 20 Apr 2016
Copy link to clipboard 
Print this post

Finding the examples above to be very useful in overcoming the need to cable each sensor back to the MicroMite.

Currently considering a 2nd project for household heating & ventilation control, which would benefit from a large number of sensors, and be a nightmare to implement if each had to wire back to the MicroMite individually.

Has me thinking whether the code would be worth porting to a cFunction or 2.

Maybe one to find the sensors on a given pin and a second to read them individually or as a group.

I do notice Jman Dim'ed some variables in the code above that were not used....

Option Explicit
Dim OW1,OW2,OW3,OW4,OW5,OW6,OW7,OW8
Dim LTemp, HTemp, Value, PinNbr, Status
PinNbr = 22


Cheers

Phil

 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025