Tuesday, September 29, 2009

DS18S20 : 1-Wire Digital Thermometer and mikroC OneWire Library

The DS18S20 is a 1-Wire digital thermometer device from MAXIM that provides 9-bit Celsius temperature measurements and communicates over a 1-Wire bus with a central microprocessor. It also has in-built alarm function with nonvolatile user-programmable upper and lower trigger points. The operating temperature range of the device is –55°C to +125°C with an accuracy of ±0.5°C over the range of –10°C to +85°C. Each DS18S20 has a unique 64-bit serial code, which allows multiple DS18S20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18S20s distributed over a large area.

Device Overview
The figure below shows a block diagram of the DS18S20. The 64-bit ROM stores the device’s unique serial code. The scratchpad memory contains the 2-byte temperature register that stores the digital output from the temperature sensor. In addition, the scratchpad provides access to the 1-byte upper and lower alarm trigger registers (TH and TL). The TH and TL registers are nonvolatile (EEPROM), so they will retain data when the device is powered down.


The core functionality of the DS18S20 is its direct-to-digital temperature sensor. The temperature sensor output has 9-bit resolution, which corresponds to 0.5°C steps. The output data is calibrated in degrees centigrade and is stored as a 16-bit sign-extended two’s complement number in the temperature register. The power-on-reset value of the temperature register is +85°C. The sign bits (S) indicate if the temperature is positive or negative: for positive numbers S = 0 and for negative numbers S = 1. A table below shows some examples of digital output data and corresponding temperature reading.

Step-by-Step Operation Sequence
Now we will discuss one of the simplest ways for accessing the DS18S20. We assume that there is only one such device on bus, and say the bus is connected to RB.0 pin of a PIC16F628A. We will also discuss the built-in OneWire library functions available in mikroC for PIC 2009.

1) All transactions on the wire bus should begin with an initialization sequence. The bus master (microcontroller) should transmit a reset pulse first, and in its response, the DS18S20 will send the presence pulse indicating that it is on the bus and ready to operate. During the initialization sequence the bus master transmits (TX) the reset pulse by pulling the 1-Wire bus low for a minimum of 480μs. The bus master then releases the bus and goes into receive mode (RX). When the bus is released, the 4.7kΩ pullup resistor pulls the 1-Wire bus high. When the DS18S20 detects this rising edge, it waits 15μs to 60μs and then transmits a presence pulse by pulling the 1-Wire bus low for 60μs to 240μs.
The mikroC function to reset the DS18S20 is Ow_Reser(&PORTB, 0).

2) Since there is only one device on the bus, no need to search, read and match the 64-bit ROM address. Rather, the master device can skip the whole ROM sequence by sending SKIP ROM [CCh] command. If there were more than one device on the bus, the master can use SEARCH ROM [F0h], READ ROM [33h], and MATCH ROM [55h] commands to address one specific device on the same bus. The mikroC function to issue SKIP ROM command is Ow_Write(&PORTB, 0, 0xCC).

3) Next use CONVERT T [44h] command to initiate a single temperature conversion. Following the conversion, the resulting thermal data is stored in the 2-byte temperature register in the scratchpad memory. The mikroC function to issue CONVERT T command is Ow_Write(&PORTB, 0, 0x44).

4) Use READ SCRATCHPAD [BEh] command to read the contents of the scratchpad. The data transfer starts with the least significant bit of byte 0 and continues through the scratchpad until the 9th byte (byte 8 – CRC) is read. The master may issue a reset to terminate reading at any time if only part of the scratchpad data is needed. The mikroC function to issue READ SCRATCHPAD command is Ow_Write(&PORTB, 0, 0xBE). After that you can use Ow_Read(&PORTB, 0) to read one byte of data via the one wire bus. In order to read both the bytes of temperature register, use Ow_Read command twice.
 OneWire Library in mikroC for PIC

To explore more about DS18S20 device, read the datasheet.


  1. Any time children's temperatures reach or exceed 101º Fahrenheit it should be reported immediately to the pediatrician. High temperatures may indicate the beginning of a serious infection requiring medical attention. Many households now have digital thermometers which came with specific instructions.

    nitrile exam gloves

  2. hye mr raj..
    im hv some problem to interface with PIR sensor (PIR sensor (#555-28027)). im use PORTA.0 as a input then when the sensor detect,it will turn ON the LED at PORTB.0(pin B0).. can u show me,how exactly the coording..

  3. Thanks for the post.

    For AVR/PIC/8051 Projects And Tutorials
    Embedded Projects Blog

  4. Thanks for sharing information about measuring device. Digital Thermometer


Microcontroller-based Embedded Systems Design