DS18B20 - Temperature Sensor

1. Basic circuit

DS18B20 is a digital temperature sensor with 1-wire interface. The sensor can be connected to any I/O pin of Ansteron Board. This library provides a simple function to read the temperature from sensor as well as low level read and write functions.

The sensor has adjustable resolutions from 9-bit to 12-bit which allows user application to compromise between convertion time and detail.

DS18S20 is similar to DS18B20 but there is a different library at this link.

Typical circuit:

A pull-up resistor (4.7K) on data line is required. Only pin 1 and 2 of the sensor are used, pin 3 can be connected to +5V or left unconnected.

2. Program

To add the library into the program, go to menu Build, select Compiler settings. In the dialog, add the library file into Additional libraries list. The file is in "lib" folder.

Use function DS18B20_read_temperature(pin); to command the sensor to perform a temperature reading then return the measurement. Input pin is the pin that the sensor is attached to (PIN_D7 as the circuit above). Return value is a 16 bit integer with value 0 means 0 degree Celsius. Each step of the value represents 0.0625 degree Celsius.

Temperature(degree Celsius)Digital value
... ...
0.125 2 (0x0002)
0.0625 1 (0x0001)
0.0 0 (0x0000)
... ...

To check if the temperature is negative or positive, check the 15th bit of the value by logically AND with 0x8000. If bit 15th is set, temperature is negative, if not, temperature is positive:

If the temperature is negative, its absolute value can be extracted by subtracting reading value from 65535 (0xFFFF) then add 1 (negative value starts at -0.0625); If the accuracy below 1 degree is not needed, simply divide the value by 16. The example below shows how to print temperature value to Serial Terminal.

Function DS18B20_sprint_temperature(buffer,value); can be used. It will print the temperature to a buffer and that can be sent directly to Serial or LCD.

Program output

To convert Celsius degree to Fahrenheit, use the formula [F]=[C]* 9/5 + 32. However, raw data from the sensor is in 1/16 degree step, to convert directly, the above formula turns into [F]=[C]*9/5+512.

Function DS18B20_C_to_F(value); can be used to convert raw data from the sensor. Both positive and negative temperature will work.

Note: Read temperature function takes up to 800ms to finish due to the time that sensor needed to do a temperature reading. Depends on resolution, the library will adjust delay time accordingly. See functions below.

3. Functions


This function sets the resolution of DS18B20 connected to given IO pin. The following constants: DS18B20_9BIT, DS18B20_10BIT, DS18B20_11BIT, DS18B20_12BIT can be used. Note that if there are more than 1 sensor connected, they must all have the same resolution or this function must be called each time, before reading sensors with different resolution settings.

Convertion time is shorter for lower resolution. It will take about 100ms at 9bit, 200ms at 10bit, 400ms at 11bit and 800ms at 12bit. The default resolution is 12bit. This function does not make the resolution setting permanent (not stored into sensor's EEPROM). At any resolution, the output temperature is always 1/16 degree per step and the lowest bits have no meaning at lower resolutions.


Perform a temperature convertion and return data when the convertion is done. The function does not check for error and it returns invalid data if DS18B20 is not properly connected. Function DS18B20_reset(pin); can be used to check the connection before reading from sensor.


Reset DS18B20 device on specified pin. Function returns 1 if DS18B20 is detected and 0 if not. Input pin is the same as other I/O functions (PIN_Cx, PIN_Bx, PIN_Dx)


Read a byte from DS18B20. Function returns random data if DS18B20 was not attached to specified pin.


Send a byte (usually command) to DS18B20. Refer device datasheet for the list of commands and how to use them.


Start a temperature convertion on DS18B20 connected to given pin. The program can then retrieve result using function below. Note that it takes up to 750ms for a convertion to complete (depends on resolution), reading result before a convertion is done may return invalid data. The function returns 1 if succeeded, 0 if DS18B20 was not detected on the pin.


Retrieve the temperature from the last convertion.


Convert Celsius to Fahrenheit. The function returns int value with 0 means 0 degree Fahrenheit, 0xFFFF is -0.0625 and so on. It's similar to the coding mentioned above.


Print coded temperature value as text to a buffer. For example, value 0 will be printed as "0.000", value 1 will be "0.062" and value 0xFFFF will be "-0.062". The buffer input is a pointer to char array that will hold result. Note that buffer should be big enough for output (10 bytes minimum). The output can then be sent to Serial with serial_message(buffer); or to be displayed on LCD with cLCD_message(buffer);

4. Note

Due to timing requirement of 1-Wire interface, all interrupts will be disabled while communication is going on. Between bytes sent and received, interrupts are re-enabled. Each byte transfer takes about 1 millisecond.

The library implements 1-Wire interface according to device specification and no error checking is needed for most cases. If temperature reading is critical for your application, error checking at communication level can be added to the library. Source file is included with the library and users are free to modify it as their needs.