Let's Workshop: DS18b20 Temperature Probe

Posted by Sebastian Karam on

Here is a quick introduction to using a DS18b20 based temperature probe. We aim to provide you with the tools you require to start measuring temperatures in all manner of environments.

This example will demonstrate the use of an Arduino UNO in requesting and interpretting data from a DS18b20 sensor. Once connected and the program loaded, the serial monitor will display a live temperature readout. This will use the OneWire library created by Paul Stoffregen and the DallasTemperature library created by Miles Burton.



Wire the components together as can be seen in the image below, taking care to match the pin numbers.


The code consists of the an include, definition, launch, setup and loop. First the OneWire library and the DallasTemperature library are linked to the code. The sensor pin is defined followed by the OneWire library and DallasTemperature library instances established. The setup launches the serial connection and sensor connection. The loop contains the control program that will run continuously after the setup. It begins by requesting the information from the sensor instance. It then prints the value to the serial monitor.

Load the code below into the Arduino IDE and upload it to your board.

  A simple program designed to setup and demonstrate the DallasTemperature library and 
  DS18b20 Temperature probe - BIAA100074
  The program uses the OneWire and DallasTemperature libraries to request and 
  output temperatures from a DS18b20 sensor to the serial monitor.
  modified 28 June 2019
  by Sebastian Karam - Flux Workshop
  The OneWire library created by Paul Stoffregen
  The DallasTemperature library created by Miles Burton

#include <OneWire.h>
#include <DallasTemperature.h> // Define the pin that the sensor pin #define ONE_WIRE_BUS 2 // Establish a oneWire instance OneWire oneWire(ONE_WIRE_BUS); // Establish Dallas Temperature instance DallasTemperature sensors(&oneWire); void setup(void) { Serial.begin(9600); // Open a serial communication line sensors.begin(); // Start up the DallasTemperature library } void loop(void){ sensors.requestTemperatures(); // Request temperatures from the sensors instance Serial.print("Temperature (Celsius): "); // Print to the serial monitor Serial.print(sensors.getTempCByIndex(0)); // Print the temperature from the library to serial monitor Serial.print("\n"); // Print to the serial monitor delay(1000); // Pause before restarting the loop }


With the board is loaded with the program and all the connections made the module will begin to output a temperature in celcius. The animation below shows a brief loop of the sensor being placed in cold cup of water then into a hot cup of water and then back to the cold.

What to try next?

  • Using multiple temperature sensors - changing the number "0" in "sensors.getTempCByIndex(0)"
  • Use the DallasTemperature library to produce the output in farenheit.
  • Poll the sensor frequently to monitor the temperature of beer throughout its fermentation - triggering heating and cooling systems based on the value.

Share this post

← Older Post Newer Post →

Leave a comment