Measures Temperatures with 1-Wire Digital Thermometer DS18B20 : MIND-TEK.NET



Measures Temperatures with 1-Wire Digital Thermometer DS18B20

DS18B20 is Dallas Semiconductor digital thermometer which can provide 9-12 bit centigrade temperature measurement. The DS18B20 communicates over 1-Wire bus, that require only 1 data line for communication with a microcontroller. It has an operating temperature range of -55C to 125C and is accurate to 0.5C over the range of -10C to 85C, pin description of DS18B20 TO-92 package as shown in Figure 1










Data Input/ Output pin



Optional Vdd pin

Figure 1 Pin description of DS18B20 TO-92 package

DS18B20 use Dallas 1-Wire bus protocol that implements bus communication using one control signal, each DS18B20 have unique 64 bit serial code, which allows multiple DS18B20 to function on the same 1-Wire bus, moreover DS18B20 can operate in parasite power mode, which can operate without an external power supply. Power is instead supplied through the 1-Wire pull up resistor via the DQ pin when the bus is high. This article will guild to the basic of using DS18B20 to operate in only normal mode, for more detail of parasite power mode operation please check the datasheet of DS18B20

Structure of DS18B20s memory as shown in Figure 2 The memory consist of an SRAM scratchpad with nonvolatile EEPROM storage for the high and low alarm trigger register (TH and TL) and configuration register

ds18b20 memory

Figure 2 Structure of DS18B20s memory

The temperature data is stored as a 16-bit sign-extended twos complement number in the temperature register as shown in Figure 3. The sign bit (S) indicate if the temperature is positive or negative: for positive number S = 0 and for negative numbers S = 1. If the DS18B20 is configured for 12-bit resolution, all bits in the temperature register will contain valid data. For 11 bit resolution, bit 0 is undefined, for 10-bit resolution, bits 1 and 0 are undefined, and for 9-bit resolution bits 2, 1 and 0 are undefined. Resolution of DS18B20 can be configure from configuration register, By default DS18B20 operate with 12 bit resolution

ds18b20 register

Figure 3 Temperature Register Format

1-Wire bus is half-duplex bus system that uses a single bus master to control one or more slave device. The DS18B20 is always working as slave. All data and commands are transmitted least significant bit first over 1-Wire bus

1-Wire bus requires an external pull up resistor of approximately 5kOhm; thus, the idle state for the 1-Wire bus is high. In case of multiple DS18B20 working in on the same 1-Wire bus, hardware can be configure as in Figure 4

1-wire circuit

Figure 4 Hardware configuration for multiple DS18B20 working on 1-Wire bus

Several signal types are defined by this protocol: reset pulse, presence pulse, write 0, write 1, read 0, read 1. The bus master initiates all of these signals, with the exception of the presence pulse

All communication with DS18B20 begins with an initialization sequence that consists of a reset pulse from the master followed by a presence pulse in response to the reset, it is indicating to the master that it is on the bus and ready to operate, all timing of reset and presence pulse as shown in Figure 5

ds18b20 initialization

Figure 5 Initialization Timing

There are 2 types of rite time slots: Write 1 time slots and Write 0 time slots as shown in Figure 6 All write time slots must be a minimum of 60 us in duration with a minimum of a 1 us recovery time between individual write slots

ds18b20 writing

Figure 6 Write time slot diagram

Both types of write time slots are initiated by the master pulling the 1-Wire bus low. To generate a Write 1 time slot, after pulling the 1-Wire bus low, the bus master must release the 1-Wire bus within 15 us. When the bus is released, the 5k pull up resistor will pull the bus high. To generate a Write 0 time slot, after pulling the 1-Wire bus low, the bus master must continue to hold the bus low for the duration of the time slot (at least 60 us)

The DS18B20 can only transmit data to the master when the master issue read time slots. Therefore, the master must generate read time slots immediately after issuing a Read Scratchpad [BEh]. All read time slots must be a minimum of 60 us in duration with a minimum of a 1 us recovery time between slot.

ds18b20 reading

Figure 7 Read time slot diagram

As shown in Figure 7. Read time slot is initiated by the master device pulling the 1-Wire bus low for a minimum of 1 us and then releasing the bus. After the master initiates the read time slot, the DS18B20 will begin transmitting a 1 or 0 on bus. The DS18B20 transmits a 1 by leaving the bus high and transmits a 0 by pulling the bus low. When transmitting a 0, the DS18B20 will release the bus by the end of the time slot, and the bus will be pulled back to its high idle state by pull up resistor. Output data from the DS18B20 is valid for 15 us after the falling edge that initiated the read time slot. Therefore the master must release the bus and then sample the bus state within 15 us from the start of the slot

The transaction sequence for accessing the DS18B20 is 3 steps as follows 1.Initialization 2.ROM Command 3.DS18B20 Function Command

All transaction on the 1-Wire bus begin with initialization sequence. The initialization sequence consist of a reset pulse transmitted by the bus master followed by the presence pulse transmitted by DS18B20. The presence pulse lets the bus master know that DS18B20 are on the bus and ready to operate

After the bus master had detected a presence pulse, it can issue a ROM command. There are 5 ROM commands, as follow SEARCH ROM [F0h], READ ROM [33h], MATCH ROM [55h], SKIP ROM [CCh], ALARM SEARCH [ECh]. In case of using only 1 DS18B20 on the bus only 2 ROM Command need, that is READ ROM, which allow the bus master to read the slaves 64-bit ROM code without using the Search ROM procedure. Another command is SKIP ROM which allow master to address all device on the bus simultaneously without sending out any ROM code information

After the bus master has used a ROM command, the master can issue one of the DS18B20 function command, which can access DS18B20s scratchpad memory or control initiate temperature conversions and determine the power supply mode. Function Command consist of CONVERT T [44h], WRITE SCRATCHPAD[4Eh], READ SCRATCHPAD[BEh], COPY SCRATCHPAD [48h], RECALL E2[B8h], READ POWER SUPPLY [B4h]

In example, DS18B20 is connected to microcontroller P89V51RD2 by using P1.1 operate as 1-Wire bus as shown in Figure 8. Status of operation shown out to HyperTerminal via 9600 bps serial connection

ds18b20 circuit

Figure 8 Hardware configuration of example

In software part of example, microcontroller will keep checking data from serial port. In case of coming data is r, microcontroller will read ROM Code from DS18B20 which the process as shown in Figure 9. At the Beginning of reading ROM code, microcontroller initialization by send reset pulse into 1-wire bus, then check for presence pulse from DS18B20. Initialization complete when master receive presence pulse, that indicating slave is on the bus and ready. After initialization complete, P89V51RD2 send Read ROM [33h], which is ROM Command to DS18B20, then read 8 bytes ROM Code from DS18B20 byte by byte. The first byte is family code, 2-7 bytes is serial number, and the last byte is CRC. All ROM Code which read from DS18B20 will send out to serial port and shown in HyperTerminal as shown in Figure 10

ds18b20 rom reading

Figure 9 Procedure of reading Rom code from DS18B20

hyperterminal ds18b20

Figure 10 Result of reading 64 bit ROM code from DS18B20

In case of coming data is s, microcontroller will read temperature data from DS18B20 and display temperature data to HyperTerminal as shown in Figure 12

Procedure of reading temperature data from DS18B20 can be separate in to 2 steps as shown in Figure 11. First is converting temperature, which DS18B20 converting current temperature in to digital format and write temperature data in to DS18B20 scratchpad, second is read scratchpad. In converting temperature, microcontroller will start with initialization, then sending SKIP ROM [CCh] command, which is ROM Command for control 1 DS18B20 on bus. Last step microcontroller send CONVERT T [44h] which is command for convert temperature in to digital data and write digital temperature data in to scratchpad. Converting take about 750ms, we can checking status of converting by loop checking status of 1-Wire bus. If DS18B20 is in converting process, 1-Wire bus will be low. If converting was complete, 1-Wire will back to high state as normal

ds18b20 reading temperature

Figure 11 Process of reading temperature data from DS18B20

After temperature conversion was complete, microcontroller read out temperature data from scratchpad by start initialization 1-Wire bus again. After initialization complete, microcontroller send SKIP ROM [CCh] command, then send READ SCRATCHPAD [BEh] command, which is function command for request reading scratchpad from DS18B20, last microcontroller read out 9 bytes data from DS18B20s scratchpad byte by byte. Temperature data is in the first and second byte that was read out. At last microcontroller send temperature data out to serial port and display in HyperTerminal as shown in Figure 12


hyperterminal ds18b20

Figure 12 Result of reading temperature data from DS18B20

Source code in C language can be downloaded from or download hex file from Datasheet of DS18B20 can be download from

73/206 Ratchathanee Paholyothin Rd. Klonghnung Klonghloung Pathumthanee 12120 Tel: 66-02516-5999 Fax:66-02516-8484