DS1820 1-Wire Temperature Sensor

DS1820 1-Wire Temperature Sensor

Description


This project shows how to use the DS1820/DS18B20/DS18S20 1-Wire Digital Thermometer chip from Maxim/Dallas Semiconductor.

 

Hardware


The hardware configuration when using multiple 1-Wire temperature sensors like the DS1820 is very simple, as illustrated in the block diagram below. A single-wire bus is used for communication between the microcontroller and the temperature sensor. It is also possible to power the devices direclty via this 1-Wire bus. An almost unlimited number of 1-WireTM devices can be connected to the bus because each device has a unique 64-bit ROM code identifier which is used to address each sensor.

Block Diagram

DS1820 Microcontroller Interface Block Diagram

 

Software


The driver which is getting described here is written in C language for the CCS C Compiler and Microchip C18 compiler.

It supports single or multiple DS1820, DS18B20 and DS18S20 devices on one 1-Wire bus.

Driver configuration

To use the driver, you just have to include the header file ds1820.h into your code. Before the include of the driver, you have to specify which pin of the PIC microcontroller shall be used for the 1-Wire communication, i.e. which pin is the 1-Wire bus connected to. In the following example it is connected to the pin RB3.

/* --- configure DS1820 temparture sensor --- */
#define DS1820_DATAPIN  PIN_B3
#include "..\_drv\ds1820.h"

Search for devices on the bus

As mentioned earlier, multiple sensors can be connected to one bus. Hence we have to identify each device on the bus by it's address (unique 64-bit ROM code identifier). This is done via the "Search ROM algorithm". The algorithm is alread implemented in the driver, so the user just needs to call the corresponding driver API functions.

  • bool DS1820_FindFirstDevice(void);
  • bool DS1820_FindNextDevice(void);

This driver APIs have to be called to find the devices. They return TRUE if there are still unfound devices on the bus or FALSE if all devices have been found. Here a small example of how to use this functions.

/* if at leat one device is on the bus */
if ( DS1820_FindFirstDevice() )
{
do
{
/* do something with the sensor */
...
}
while ( DS1820_FindNextDevice() );  /* as long as not all devices have been found */
}

Getting the Temperature

The driver provides 2 APIs for getting the temperture from a sensor:

  • float DS1820_GetTempFloat(void);
  • sint16 DS1820_GetTempRaw(void)

DS1820_GetTempFloat returns the temperature value of the currently selected temperature sensor as float value with the unit degree centigrade.

DS1820_GetTempRaw returns a temperature raw value of the currently selected temperature sensor as a sint16 value. The return value has a resolution of 1/256°C. Some of you now would ask what does a resolution of 1/256°C mean? This means that 1 bit equals to 1/256°C = 0.0039°C.
So if the return value of DS1820_GetTempRaw is e.g. 5000, you have to divide it by 256 to get the temperature value in uints of degree centigrade, which would be 19.53°C.

Driver usage example

void main()
{
sint16 temperature_raw;     /* temperature raw value (resolution 1/256°C) */
float temperature_float;
char temperature[8];        /* temperature as string */
uint8 sensor_count;         /* sensor counter */
/* initialize PIC */
initialize();
printf("\n\r*** DS1820 Demo! ***\n\r");
/* main loop */
while (1)
{
/* set LED on measurement start */
LED1_On();
sensor_count = 0;
if ( DS1820_FindFirstDevice() )
{
do
{
/* get temperature raw value (resolution 1/256°C) */
temperature_raw = DS1820_GetTempRaw();
/* convert raw temperature to string for output */
DS1820_GetTempString(temperature_raw, temperature);
/* get temperature value as float */
temperature_float = DS1820_GetTempFloat();
/* print result to RS232 interface */
printf("Sensor %d: %s°C (temperature_float = %f), temperature_raw = %ld)\n\r",
sensor_count,
temperature,
temperature_float,
temperature_raw);
sensor_count ++;
}
while ( DS1820_FindNextDevice() );
sensor_count = 0;
}
/* measurement end, clear LED */
LED1_Off();
/* measure every 2 seconds */
delay_ms(2000);
}
}

Here some pictures of my demo setup. The microcontroller is connected to 4 temperature sensors.

DS1820 demo setup DS1820 demo screenshot

 

Download