Interfacing and Reading from the DS18B20+

OpthomasRhymes

New Member
Hi guys and gals

I am working on a college project which requires a fan to be operated when the temperature is within a certain range. I am coplety new to electronics and pics. I have been working on for around five weeks now and have made lost of progress. I have managed to integrate and program the fan to work via a darlington driver and have created a timed alarm as per the project requirements. The issue I am having is getting the temperature reading from the DS18B20. I have connected it with the 4.7k resistor between the power and data wire but am unsure how to get the reading.

my code looks like this:


readtemp c.1, b1

if b1 > 25 then

high b.2

endif

Obviously I am not using or processing the data from the DS18B20 correctly, but after many hours I am still at a bit of a loss. This is the final stage of the project and if I can get it sorted I can then started sorting the whole of my program out and piece itall together. Any help with the codewould be so helpful

Thanks for reading. I have attatched a very simple (and rather crude) daigram for this part of my system, sorry if it is a bit basic!ds18b20.jpg
 

geoff07

Senior Member
I suggest you learn about the debug and the sertxd commands. These will help you find out what the program is doing, and the value returned from the sensor, which you will need to read repeatedly. Then it will become clearer what is going wrong. The code looks ok, though you might ask what happens once the temperature has gone up and then falls.
 

OpthomasRhymes

New Member
Thanks geoff. My code will evenually begin with a loop that continually read to temp and breaks out if the lowest parameter is hit. I have lloked at debug but not made too much sense of it as of yet
 

sniper887

Member
Thanks geoff. My code will evenually begin with a loop that continually read to temp and breaks out if the lowest parameter is hit. I have lloked at debug but not made too much sense of it as of yet
Just put DEBUG in your code below the readtemp line and you'll see in the debug window what the value of b1 is when you program your code. With your code in a loop the value of b1 will repeatedly update with what the DS18B20+ is measuring. Then you can compare it to your setpoint and turn the fan on or off as needed. I'll typically use one if-then statement to turn on the fan if the temperature is above the setpoint, and another if-then statement to turn off the fan if the temp is below setpoint. If the temperature is equal to setpoint nothing will be changed.
 

hippy

Ex-Staff (retired)
Welcome to the PICAXE forum.

Note that the DS18B20 data line requires a 4K7 pull-up which isn't shown in your circuit diagram. PICAXE Manual 2 under the READTEMP command shows an example of that.

Your LED should also have a current limiting resistor as well if that is also missing.
 

Goeytex

Senior Member
This demo code may give you some ideas of how it may be done in one simple loop. Notice how sertxd is used to display data in the PE terminal. I generally prefer sertxd to debug as it uses less resources.

Rich (BB code):
'======================================================================
;Example Program for simple on/off temperature control 
;Demostrates the use of "sertxd and the terminal for testing / debugging.
'=======================================================================

symbol Temp_C = b1
symbol iteration = W1
symbol Fan = b.2

Init: 
iteration = 1 ; Set loop counter  to 1 
low fan       ; Start with fan off 

Main:
Do  
      readtemp c.1,Temp_C      ;Read the temperature
      sertxd ("Iteration ",#iteration,cr,lf)
      sertxd ("Temperature: ",#Temp_C, " Celsius",cr,lf)   ;Display temp in Terminal 
                              
      if Temp_C > 25 then  
            high fan             ;Turn on the fan
            sertxd ("Fan is on ",cr,lf,cr,lf)
      else  if Temp_C <= 25 then
           low fan               ;Turn the fan off
           sertxd ("Fan is off ",cr,lf,cr,lf) 
      end if
      pause 1000  ;Slow it down for testing / change pause as needed
      inc iteration    ; increment the loop counter
Loop

'Note:  This code does not add hysteresis to the thermostat functionally.  It may be prudent to add about 2 degrees of hysteresis to prevent the fan from constantly cycling.
 

OpthomasRhymes

New Member
Thanks guys I have it sorted. I think I had a faulty DS18B20 as I have put a new in the same slot with the exact same wiring and I am getting a reading now. Thanks very much for all of you input and ideas
 
Top