Arduino - Temperature Sensor

There are many type of temperature sensors can works with Arduino such as LM35, TH02, HDC1000 or HTS221... In this tutorial, we are going to learn how to use waterproof DS18B20 temperature sensor with Arduino. This sensor is inexpensive, easy to use and look neat.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Temperature Sensor DS18B20
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About One Wire Temperature Sensor - DS18B20

Pinout

DS18B20 temperature sensor has three pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to VCC (5V or 3.3V)
  • DATA pin: is 1-Wire Data bus. It should be connected to a digital pin on Arduino.

The sensor usually has two forms: TO-92 package (looks like a transistor) and waterproof probe. We use the waterproof probe form in this tutorial.

DS18B20 temperature sensor Pinout

Connecting a DS18B20 temperature sensor to an Arduino requires a pull-up resistor, which can be a hassle. However, some manufacturers simplify the process by offering a wiring adapter that has a built-in pull-up resistor and a screw terminal block, making it much easier.

Wiring Diagram

  • Wiring diagram with breadboard
Arduino Temperature Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with adapter (recommended)
Arduino DS18B20 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Real wiring diagram with adapter
Arduino DS18B20 adapter wiring diagram

This image is created using Fritzing. Click to enlarge image

We suggest purchasing a DS18B20 sensor that comes with a wiring adapter for easy connection. The adapter has a built-in resistor, eliminating the need for a separate one in the wiring.

How To Program For DS18B20 Temperature Sensor

  • Include the library:
#include <OneWire.h> #include <DallasTemperature.h>
  • Declare OneWire and DallasTemperature object corresponding to the pin connected to sensor's DATA pin
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
  • Initialize the sensor:
sensors.begin(); // initialize the sensor
  • Send the command to get temperatures:
sensors.requestTemperatures();
  • Read temperature in Celsius:
tempCelsius = sensors.getTempCByIndex(0);
  • (Optional) Convert Celsius to Fahrenheit:
tempFahrenheit = tempCelsius * 9 / 5 + 32;

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library float tempCelsius; // temperature in Celsius float tempFahrenheit; // temperature in Fahrenheit void setup() { Serial.begin(9600); // initialize serial tempSensor.begin(); // initialize the sensor } void loop() { tempSensor.requestTemperatures(); // send the command to get temperatures tempCelsius = tempSensor.getTempCByIndex(0); // read temperature in Celsius tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit Serial.print("Temperature: "); Serial.print(tempCelsius); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(tempFahrenheit); // print the temperature in Fahrenheit Serial.println("°F"); delay(500); }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to Tools Manage Libraries
Arduino add library
  • Search “OneWire”, then find the OneWire library by Paul Stoffregen
  • Click Install button to install OneWire library.
Arduino onewire library
  • Search “Dallas”, then find the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
Arduino Dallas Temperature library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Put the sensor on hot and cold water, or grasp the sensor by your hand
  • See the result on Serial Monitor.
COM6
Send
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Temperature Sensor on Commercial Products

The Best Arduino Starter Kit

※ OUR MESSAGES