OWL2 to DS1620 temperature sensor

(c) 1998 , 2001 EME Systems, Berkeley CA U.S.A.
 <stamp index> <home>

Contents (updated 9/10/2001)

DS1620 temperature sensor


The DS1620 is a digital thermometer in an 8-pin package from Dallas Semiconductor. It hooks to the OWL2c or BASIC stamp using a three-wire SPI interface and can measure temperatures from -55 up to +125 degrees Celsius. I have written a tutorial on the DS1620 in unit 1 of the Earth Measurements series. There are many other sources of information about the DS1620, including the data sheet and application notes on the Dallas Semiconductor web site, and application notes from Scott Edwards Electronics.

Standard resolution from the DS1620 is 9 bits, or +/- 0.5 degree Celsius over a range of -55 to +125 degreees Celsius (-67 to +257 degrees Fahrenheit). That is the resolution acheived the sources mentioned above. However, they do not all work properly with negative temperatures. Here is a routine that covers all the bases. It reads all nine bits of the data, including the sign, then it extends the sign so that the result is a 16 bit twos complement number. Then it multiplies by 5 to get degrees C*10, and displays the result with the decimal point with half degree resolution. Note how the debug command breaks down the value so that it can print the decimal degrees when the temperature is either above or below zero (more explanation). The program also converts Celsius to whole degrees Fahrenheit (one degree F is close to 1/2 degree C) and then prints that result too. Note that Celsius is converted first to Kelvin, so that the division can be applied to a positive number. The Stamp cannot divide negative numbers directly.

' {$STAMP BS2pe}
' {$PBASIC 2.5}

x VAR WORD ' define a general purpose variable
sign VAR x.BIT15 ' sign bit of x
degC VAR WORD ' define a variable to hold degrees Celsius
degF VAR WORD ' to hold degrees Fahrenheit
' note: DS1620 has been preprogrammed once
' for mode 2: continuous conversions, with CPU
' high 13 ' start
' shiftout 15,14,lsbfirst,[12,2] ' set mode 2
' low 13 ' stop
OUTS=%0000000000000000 ' define the initial state of all pins
DIRS=%1111111111111111 ' as low outputs

HIGH 13 ' select the DS1620
SHIFTOUT 15,14,LSBFIRST,[238] ' "start conversions" command
LOW 13 ' do the command
DO ' going to loop once per second
HIGH 13 ' select the DS1620
SHIFTOUT 15,14,LSBFIRST,[170] ' send the "get data" command
SHIFTIN 15,14,LSBPRE,[x\9] ' get the data, including sign
LOW 13 ' end the command
x.BYTE1 = -x.BIT8 ' extend the sign to 16 bits
degC=x*5 ' convert to 'C*10 (resolution 0.5 'C)
' & show the result on the PC screen:
DEBUG "degC=", REP "-"\degC.BIT15, DEC ABS degC/10, ".", DEC1 ABS degC, CR
degF= degC+2732*9/5-4598 ' 'C*10 to 'F (first to Kelvin*10)
DEBUG "degF=", REP "-"\degF.BIT15, DEC ABS degF/10,".", DEC1 ABS degF, CR
PAUSE 1000 ' 1 second pause
LOOP ' read & display temperature again

One more note about applying the DS1620: In the continuous conversion mode, the chip is quite sensitive to noise on the power supply and signal lines. The effect of the noise is to stop the conversion process. It can be restarted by issuing a "start conversions" command, hex $EE. The first conversion is ready after an interval that may take as much as one second of conversion time.

DS1620 temperature sensor in high resolution mode


The application notes for the DS1620 show how to achieve a much higher level of resolution, better than 0.05 degree. The exact procedure for accomplishing this is not well documented. Here is a BASIC Stamp program that takes the extra steps necessary to read the count per degree and the count remaining registers in the DS1620 and to calculate the improved temperature value. Note that the basic accuracy of the DS1620 is still +/- 0.5 degree. But the higher resolution allows you to see small shifts in temperature with much greater sensitivity.

The DS1620 must be put in its single-shot mode before it can read the count-per-degree and count-remaining registers. This is done by sending a hex command $03 to the configuration register. The configuration register is nonvolitile eeprom, so that setting is maintained even when the power is removed, or when the DS1620 is moved from one system to another. The configuration sequence is shown commented out in the following program. Normally it will be executed only once at the outset of a project. But you do have to run those lines at least once to put the DS1620 in the single-shot/CPU mode. Also, the first time you send the $03 command to the stamp, you have to cycle the power to the DS1620 off, and then on again before the change takes effect. (IMPORTANT. I learned that last little tidbit after some hard head scratching!)

The $EE command sent to the DS1620 intiates one conversion, which can take up to one whole second. After a conversion is started, the program reads the status register repeatedly until it shows that a conversion is complete. This could be replaced with a dumb delay, but a lot of time would be lost in order to accomodate the longest possible delay.

Once the temperature conversion is complete, the main 9-bit data is read, and then the sign is extended, and the result, degC, is displayed to +/- 0.5 degrees C.

  degC=x*5             ' compute to 10ths, +/- 0.5 degree C

Then the program reads the count remaining and the count per degree registers, here named remain and slope. The formula

  x=slope-remain*100/slope	' 1/100ths degree

then calculates the fractional part of a degree. The value of slope will be in the neighborhood of 100 to 150, while remain takes on values from zero up to slope as the temperature varies across one degree Celsius. The result of the computation, x, is a value from 0 up to 99 that quantifies an increasing temperature in 1/100ths of a degree. Then the formula


combines the 100ths with the original main temperature value. Let's look at the computation step by step. The original value we have for degC is resolved to 0.5 degree. Adding 550 shifts the temperature scale up by 55.0 degrees C. The number (degC+550) is always positive. For example, -29.5 --> +25.5 after that step. We do that so the division by ten will come out right. (The stamp does not divide negative numbers properly). The division by 10 drops the ones digit (tenths of a degree), so for example, 25.5 is truncated to 25. Then multiplying by 100 leaves zeros in the tens and zeros digits, for example 25-->2500. Finally we add in the value of x, from 0 to 99, so for example we come up with 2567. Finally, we subtract 5500 to restore the negative scale down to -55.00 degrees C, and we subtract another 25 to account for the 0.25 degree offset that the DS1620 put on its values. To clarify this latter offset, the DS1620 main routine (+/-0.5 degrees C) outputs 0.0 degrees for temperatures from -0.25 up to +0.25, and it output 0.5 degrees for temperatures from 0.25 up to 0.75 and so on. The high resolution routine has to remove this 1/2 lsb offset bias. I have tested the following routine at temperatures both above and below freezing. The routines for displaying negative numbers with a decimal point are described elsewhere.

' {$STAMP BS2pe}
' {$PBASIC 2.5}
' DS1620 high resolution version
' gets data to +/- 0.01 degree Celsius resolution
' this routine covers the -55.00 to +125.00 range
' Tracy Allen, http://www.emesystems.com
' P13=chip select
' high activates the DS1620 to receive data
' P14=serial data clock
' P15=serial SPI data in and out
' P0 is an optional piezo xducer for feedback
remain VAR WORD
slope VAR WORD


' the following setting in eeprom needs to be done only once
' be sure to cycle the power to the DS1620 after changing!
'high 13
' shiftout 15,14,lsbfirst,[$C,$3]
'low 13
PAUSE 100 ' allow time

DEBUG"testing DS1620 in hi res mode",CR

' main loop of this test program
ds1620: ' start a conversion
LOW 13
busy: ' wait for data to be ready (up to one second!)
LOW 13
' debug bin8 ?x ' to show the status
IF x.BIT7=0 THEN busy ' x.bit7 goes high when done

HIGH 13 ' request data byte and get it
SHIFTIN 15,14,LSBPRE,[x\9]
LOW 13
x.BYTE1 = -x.BIT8 ' extend sign
degC=x*5 ' compute to 10ths, +/- 0.5 degree C
DEBUG REP "-"\degC.BIT15,DEC ABS degC/10,".",DEC1 ABS degC
FREQOUT 0,20,3800 ' signal result ready on piezo beeper

HIGH 13 ' get the count remaining DS1620
SHIFTIN 15,14,LSBPRE,[remain\9]
LOW 13

HIGH 13 ' get the counts per degree
SHIFTIN 15,14,LSBPRE,[slope\9]
LOW 13

x=slope-remain*100/slope ' compute 1/100ths degree
degC=degC+550/10*100+x-5525 ' adjust temperature to 1/100ths
DEBUG TAB,REP "-"\degC.BIT15,DEC ABS degC/100,".",DEC2 ABS degC

'debug tab,dec remain,32,dec slope,cr ' show the special values
LOOP ' loop again

Here is alternative code for resolution to 0.1 degree C:

'  x=slope-remain*10/slope	' 1/10ths degree
' degC=degC+550/10*10+x-552
' debug tab,rep "-"\degC.bit15,dec abs degC/10,".",dec1 abs degC


DS1620 as a standalone thermostat


The DS1620 allows you to store an upper and lower threshold temperature values in non-volitile eeprom inside the DS1620. When the temperature equals or exceeds the upper threshold, pin 7 goes high. When the temperature equals or falls below the lower threshold, pin 6 goes high. Pin 5 implements hysteresis for control of a fan or heater: it goes high when the temperature is >=upper threshold, and stays high until the temperature is <=lower threshold.

Here is how to set the thresholds. They are nine bit, twos complement temperature values. The ninth bit is the sign. The binary values represent the temperature times two, that is, a threshold of 25.5 would be stored as the binary value 51. Any of the modes of the DS1620 can be used with the thresholds. In the single shot modes, the thermostat output pins change state only at the end of each conversion. In the continuous conversion modes, the output pins change state autonomously. In the standalone mode, the DS1620 can act as a thermostat with no additional chips or CPU required. In standalone mode, tie pins 2 and 3 on the DS1620 low in order to give continuous operation. Use a buffer between the output pins and any load, to avoid self heating the DS1620.

' {$STAMP BS2pe}
' {$PBASIC 2.5}
' Demo BS2 routine to put the DS1620 in standalone mode
' and to set the lower and upper threshold registers.
' to -5.5 and +7.5 degrees C
' P13=chip select
' high activates the DS1620 to receive data
' P14=serial data clock
' P15=serial SPI data in and out
' ----------------------------------
' configure DS1620 for standalone mode
LOW 13
PAUSE 10 ' let it write (be sure to allow time!)
' ----------------------------------
' set the lower threshold= -5.5 deg C
SHIFTOUT 15,14,LSBFIRST,[2,-11\9] ' lower threshold=-11/2=-5.5 deg C
' the twos complement number is sent correctly as 9 bits
LOW 13
PAUSE 10 ' let it write
' ----------------------------------
' set the upper threshold= +7.5 deg C
SHIFTOUT 15,14,LSBFIRST,[1,15\9] ' upper threshold=15/2=7.5 deg C
LOW 13

' ----------------------------------
' At this point you can turn off the DS1620
' an move it to another circuit.
' The configuration and settings are fixed in eeprom
' you only need to program the eeprom once.
' (until you want to change the configuration or thresholds!)
' DS1620 pins 2 & 3 should be tied low in the application circuit.
' DS1620 pin 7 is high for temperature >= 7.5
' DS1620 pin 6 is high for temperature <= -5.5
' DS1620 pin 5 is high >=7.5, low <=-5.5, hysteresis in between
' -------

 .<top> <index> <home> logo< mailto:info@emesystems.com >