05 – How to measure temperature with a DS18B20 sensor in Picaxe?

The DS18B20 temperature sensor has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C. 

It is very easy to measure the temperature with a DS18B20 temperature sensor in Picaxe. There are two ways to read the temperature on a DS18B20 sensor.

Read Temperature rounded to the nearest full degree Celsius.

The first is by using the the “ReadTemp” command. This command carries out a full 12 bit conversion and then rounds it to the nearest full degree Celsius. This value is returned into a byte variable. Bit 7 of the returned value indicate the sign of the temperature reading. It is 0 for positive temperature values and 1 for negative temperature values. 

The example code was developed on the AXE091 Development Board for a Picaxe-18M2 microcontroller but I will supply the circuit diagram for illustration purposes.

The DS18B20 temperature sensor is connected to pin B.3 and a serial LCD is connected to pin C.6. The sample code is listed below.

You can download the program file, Reading the DS18B20 Temp sensor for 18M2 by right clicking on it and selecting ‘save link as’ from the popup menu.


 

Read 12bit Temperature to the second decimal in degree Celsius.

The second way to read the temperature from a DS18B20 is with the “ReadTemp12″ command. This command reads the number of temperature increments of a resolution of 0.0625°C. In 12 bit resolution the first 12 bits gives the temperature and the rest of the bits indicates the sign of the temperature. For positive temperatures the sign bits are 0 and for negative temperatures the sign bits are 1. 

The code uses only the MSB to determine the sign of the temperature. The example uses the same circuit diagram as the previous example and the code is shown below.

The ReadTemp12Sensor sub procedure is shown below.

You can download the program file, Reading the DS18B20 Temp sensor in 12bit for 18M2 by right clicking on it and selecting ‘save link as’ from the popup menu.