Introduction: PICAXE - DS18B20 Temperature Sensor to LCD

This is my first Picaxe design, program and build project. This project connects a DS18B20 sensor to a parallel LCD using a PICAXE 20X2. It measures the room temperature and reads it out to the LCD every second or so. This project will also measure negative temperatures outdoors in the winter.

It may be possible to do this with a lesser processor than the 20X2, however I happened to have some of these available and had experimented with this processor on a breadboard beforehand, so this is what I used.

Before starting I'd like to acknowledge the excellent articles and code examples provided by Ron Hackett in his many Nuts & Volts magazine articles and "Evil Genius" book, which I used to teach myself about Picaxe processors. Ron's articles motivated me to get moving and do some projects! You may also find lots of code examples and help on his website www.jrhackett.net .

Step 1: Parts List

Parts List:

PICAXE 20X2 Microcontroller
16 X 2 LCD - UniQ/eVision GC-1602I1 or equivalent
DS18B20 Temperature Sensor
5V wall wart - SPS10A-001 or equivalent
(You can also use a battery and clip, e.g. 3 x AAA instead of a wall wart - but do not exceed 5V!!)
A jack which matches the power connector on your wall wart
2.5 mm Stereo Jack - Temperature sensor connection
2.5 mm Stereo Plug -Temperature sensor connection
3.5 mm Stereo Jack - Programming connection
PICAXE USB Programming Cable AXE027
5K Potentiometer
10k resistor
22k resistor
4.7k resistor
Solder
Hook up wire
Scrap Cat5 cable - for connecting the LCD to the circuit board or equivalent multi-conductor cable
2" x 3 " protoboard
4.4" X 2.5" x 1.5" (approx) plastic project box
4 Hex Spacers about 1/2" long
4 Nuts & bolts to fit in the Spacers
Cable ties
Hot Glue

Step 2: The Circuit

The circuit is reasonably simple. I left J1 the programming jack attached to the circuit but accessible inside the box only as once it is programmed you probably don't need to make a lot of changes. Although it is not shown in the drawing, the temperature sensor is mounted in a stereo 2.5 mm plug and this plugs in to a jack on the side of the case. You could also mount it on the circuit board and after a while it will come to approximately room temperature. The Circuit diagram was created using Dip Trace. This is a very easy to use program and free for amateur use. Find it at www.diptrace.com

Step 3: Breadboard It First

If you don't have a lot of experience with PICAXE or LCDs you may want to build this on a breadboard first to get it working. It is a lot less effort to build it on a breadboard and get it working before you do the harder work of mounting it all in a box.

In this case I had a spare LCD, which is used for other experiments, don't be afraid to solder jumper wires to the LCD to connect it to the board, even if you only have one. You can easily de-solder them later, and they are reasonably robust as long as you follow basic
ant-static precautions. Here you see how simple it is - just the LCD, PICAXE, Temp sensor and programming jack.

Step 4: The Program

Below the program is pasted in. You can basically follow how it works by reading the program comments. More experienced programmers will probably know how to write simpler and more efficient code. What can I say, this is my first attempt, and it seems to work.


' ************** TemperatureSensor.bas **************

'     This program runs on a PICAXE-20X2 at 4MHz which reads the temperature from a
' DS18B20 temperature sensor & sends it to an HD44780 compatible 16x2 LCD display.

' Developed by Simon Carter March 10, 2013.

' *** Constants ***
 symbol EnablePin = C.7     ' LCD Enable pin connected to C.7
 symbol RegSelPin = C.4     ' LCD Register Selelect pin connected to C.4
 symbol TempPin   = C.1     ' Temperature sensor data connected to C.1
' *** Variables ***
 symbol   char =  b0     ' character to be sent to LCD
 symbol  index = b1     ' used as counter in For-Next loops
 symbol  degc  = b2     ' the temperature value received from the DS18B20 sensor
 symbol  intgval=b3     ' used to temporarily hold each integer value extracted from the number received from the sensor
 symbol    degflag = b4     ' a flag used when dealing with negative temperature ranges. 0 = Postive range, 1 = Negative range
 symbol   degf = b5     ' used for holding degrees farenheight values
' *** Directives ***
 #com 3       ' specify download port
 #picaxe 20X2      ' specify processor
 #no_data       ' save time downloading
 #terminal off      ' disable terminal window

' *** Table ***
 Table  0, ("The Temperature ")

' *** Main Program ***
  dirsB = %11111111      ' set all portB pins as outputs
  dirsC = %10111001      ' set  C.6 and C.2, C.1 as inputs
  pullup  %00000010      ' enable C.6 internal pullup resistor 

' *** Initialize the LCD ***        
 pause 200       ' pause 200 mS for LCD initialization as per HD44780 specs
 char = 56       ' setup for 8-bits, 2 lines & 5X8 display
  gosub CmdLCD      ' send instruction to LCD
 char = 12       ' display on, cursor off
 gosub CmdLCD      ' send instruction to LCD  
  char = 1       ' clear display,go home to line 1 of LCD display 
  gosub CmdLCD      ' send instruction to LCD
 wait 1

' *** Send text - "Temperature"to line 1 of the LCD *** 
  for index = 0 to 15
  readtable index, char     ' read text characters in the Table
  gosub TxtLCD      ' send text in the Table to LCD
  next index

' *** Main loop - read DS18B20 sensor ***
do  

   char = 194       ' move cursor to the start of line two 
  gosub CmdLCD      ' send the instruction to LCD   
   readtemp TempPin, degc     ' read the temperature value received from the DS18B20 sensor on pin C1.0
  pause 800       ' pause >750 mS for sensor as per DS18B20 specs
'  degc = 24          insert temperature test values here to check code function
  degflag = 0       ' reset the flag to 0 i.e. assume positive temperature range to start with
  gosub BelowZeroC      ' check for below zero temperatures      
  gosub IntgOnly      ' extract 10's and 1's integers from DS18B20 data
  gosub DegCsign         ' generate deg C sign
  gosub CheckFlag      ' check if dealing with temperatures in the minus range and adjust degc value
  gosub BelowZeroF      ' check for below zero temperatures 
  gosub DegFsign      ' check for below zero temperatures 
         ' generate deg F sign  
loop

' *** Subroutines ***

CmdLCD:
 low RegSelPin      ' set up LCD for a command byte
 goto LoadLCD        

TxtLCD:        ' set up LCD for text byte
 high RegSelPin      

LoadLCD:        ; execute loading byte into LCD
 outpinsB = char      ' load byte onto outpinsB
 pulsout EnablePin,1     ' load bytes onto LCD
 return    

LoadIntgLCD:       ' execute loading temperature text byte that has been converted to an integer      
 high RegSelPin      ' set up LCD for text byte
 outpinsB = intgval     ' load byte onto outpinsB
 pulsout EnablePin,1     ' execute loading byte into LCD    
 return

IntgOnly:        ' extract 100's, 10's and 1's integers from DS18B20 data
 for index = 2 to 0 step -1    ' set index to extract 100's integer first then 10's integer then 1's
 let intgval = degc dig index    ' Use DIG function to extract integers



      if intgval = 0 and index =2 then let intgval =%00100000 'Blank any leading "0" on display 
 else if intgval = 0 then let intgval =%00110000 ' Convert integers to LCD character codes
 else if intgval = 1 then let intgval =%00110001
 else if intgval = 2 then let intgval =%00110010
 else if intgval = 3 then let intgval =%00110011
 else if intgval = 4 then let intgval =%00110100
 else if intgval = 5 then let intgval =%00110101
 else if intgval = 6 then let intgval =%00110110
 else if intgval = 7 then let intgval =%00110111
 else if intgval = 8 then let intgval =%00111000
 else if intgval = 9 then let intgval =%00111001

 endif

 gosub LoadIntgLCD      ' execute loading temperature text byte that has been converted to an integer 
 next index
 return

DegCsign:        ' create Degrees C text on display
 intgval =%11011111     ' set character to be sent to LCD to "Degree" Character Code
 gosub LoadIntgLCD      ' send characters to LCD

 intgval =%01000011     ' set character to be sent to LCD to "C" Character Code for Centigrade
 gosub LoadIntgLCD      ' send characters to LCD

 intgval =%00100000     ' set character to be sent to LCD to " " Character Code for a space before the farenheight value
 gosub LoadIntgLCD      ' send characters to LCD
 return

DegFsign:        ' create Degrees F text on display
 intgval =%11011111     ' set character to be sent to LCD to "Degree" Character Code
 gosub LoadIntgLCD      ' send characters to LCD

 intgval =%01000110     ' set character to be sent to LCD to "F" Character Code for Farenheight
 gosub LoadIntgLCD      ' send characters to LCD
 return

 intgval =%00100000     ' set character to be sent to LCD to " " Character Code for a space after farenheight value to clear when minus sign used
 gosub LoadIntgLCD      ' send characters to LCD

 intgval =%00100000     ' set character to be sent to LCD to " " Character Code for a space after farenheight value to clear when intgval>100 used
 gosub LoadIntgLCD      ' send characters to LCD


DegFcalc:        ' derive Farenheight value for positive Farenheight readings
 degc=degc*9/5+32      ' calculate Farenheight value from Centigrade value
 gosub IntgOnly      ' begin process to extract 100's, 10's and 1's integers for Farenheight value
 debug
 return

DegFcalcPos:       ' derive Farenheight value for positive Farenheight readings when celcius is negative
 degc=degc*9/5      ' calculate Farenheight value from Centigrade value
 degc=32-degc      ' calculate Farenheight value from Centigrade value
 gosub IntgOnly      ' begin process to extract 100's, 10's and 1's integers for Farenheight value
 return

DegFcalcNeg:       ' derive Farenheight value for negative Farenheight readings
 degc=degc*9/5-32      ' calculate Farenheight value from Centigrade value
 gosub IntgOnly      ' begin process to extract 100's, 10's and 1's integers for Farenheight value
 return

MinusSign:
 intgval ="-"      ' set character to be sent to LCD to "minus sign" Character Code
 gosub LoadIntgLCD
 return

BelowZeroC:        ' check for below zero deg C
 select case degc
 case 0 to 125      ' ignore this case - temperature in the normal positive range

 case 129 to 183      ' the case where celsius is in the minus range send a minus sign character to LCD      
 degc=degc-128      ' correct for the DSDS18B20 setting MSB to "1" or 128 for minus temperature condition
 degflag = 1       ' set flag indicating negative temperature condition
 gosub MinusSign      ' the case where celsius is in the minus range send a minus sign character to LCD
 endselect
 return

BelowZeroF:
 select case degc
 case 0 to 124      ' ignore this case - temperature in the normal positive range
 gosub DegFcalc

 case 129 to 146      ' the case where only celsius is in the minus range            
 degc=degc-128      ' correct for the DSDS18B20 setting MSB to "1" or 128 for minus temperature conditio
 gosub DegFcalcPos

 case 147 to 183         ' the case where farenheight is in the minus range send a minus sign character to LCD
 degc=degc-128           ' correct for the DSDS18B20 setting MSB to "1" or 128 for minus temperature condition
 gosub MinusSign      ' the case where farenheight is in the minus range send a minus sign character to LCD
 gosub DegFcalcNeg
 endselect
 return

CheckFlag:
 if degflag = 1 then let degc = degc+128  ' Reset the correction for the DSDS18B20 setting MSB to "1" or 128 for minus temperature condition
  endif
  return

Step 5: Program the PICAXE

Once you have built the circuit on a Breadboard you can program the PICAXE using the AXE027 USB cable. You will need to down load the cable usb driver and programming software from the Picaxe web site at www.picaxe.com/software Instal the Picaxe Programming Editor and AXE027 USB Cable Driver on you PC. Then either copy and paste the program provided or open the attached .bas file or write your own if you are feeling ambitious. 

Step 6: Build It

Once you have you PICAXE programmed and working on the breadboard it is time to build it.

Step 1 is to cut a hole in the plastic box for the LCD to fit in. This is not easy to do neatly but you can do it by measuring the LCD carefully, applying masking tape to the box and drawing the outline of the LCD carefully. Next use a sharp exacto knife and a steel straight edge as a guide to keep the line straight to cut out the hole in the box. Remove the tape finished.

Step 2 is to drill holes in the box for the power jack, temperature sensor jack and holes to bolt the standoffs and circuit board to the box.

Step 3 is to solder the parts onto the circuit board and connect them as per the circuit diagram. You can install the PICAXE programming jack right on the circuit board or on a short pig tail and then hot glue it in a convenient spot in the box so that it does not move around or accidently short anything out.

Step 4 is to connect the LCD to the circuit board using any appropriate multiconductor wire. I used a couple of pieces of scrap CAT5 cable but just about anything will work including individual strands of wire.

Step 5 is to connect the circuit gnd and + 5 to the power jack and the wires from the circuit board to the temperature sensor jack making sure that you note which pin connects to where on the circuit board. Use heat shrink tubing on exposed connections.

Step 6 is to mount the temperature sensor inside a 2.5 mm stereo plug. Just connect the 3 pins of the sensor to the 3 connection pins in the plug and make sure they correspond to the correct connections on the circuit board. Here I used heat shrink to keep the pins from shorting out. Next screw the cap on the the plug and fill the end with hot glue. 

Step 7 is to put the LCD into the hole in the plastic case and hot glue it in to place - tack at either end is enough. Install the power and temperature sensor jacks into the holes you drilled earlier. Bolt the circuit board into place using the pre-drilled holes at each corner of the circuit board.

Step 7: Files

Attached the PICAXE Manual from RevEd. In the zip file are; the Dip Trace circuit diagram file, a jpg of the circuit diagram, the program.bas file,

sncarter