Arduino Nano - Temperature Sensor

This tutorial instructs you how to use Arduino Nano and the waterproof DS18B20 1-wire temperature sensor to measure the temperature. This sensor is cost-effective, simple to use, and has a neat appearance among other alternatives.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of 1-Wire DS18B20 Temperature Sensor

The Temperature Sensor Pinout

The DS18B20 temperature sensor has three pins that need to be connected:

  • GND pin: needs to be linked to GND (0V)
  • VCC pin: needs to be linked to VCC (5V or 3.3V)
  • DATA pin: is the 1-wire data bus and should be connected to a digital pin on Arduino Nano.

The sensor typically comes in two varieties: TO-92 package (which resembles a transistor) and a waterproof probe. We will be utilizing the waterproof probe in this tutorial.

DS18B20 temperature sensor pinout

Connecting the DS18B20 temperature sensor with Arduino requires a pull-up resistor, making it a bit of a hassle. However, some manufacturers make it easier by offering a wiring adapter that comes equipped with a built-in pull-up resistor and a screw terminal block, reducing the hassle and making it more convenient.

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Arduino Nano and temperature sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a the wiring adapter (recommended).
The wiring diagram between Arduino Nano and DS18B20

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring. We also tested it and worked well.

How To Program For DS18B20 Temperature Sensor

  • Include the following library:
#include <OneWire.h> #include <DallasTemperature.h>
  • Declare an object for OneWire, corresponding to the pin connected to the sensor's DATA pin. Also declare an object for DallasTemperature.
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library
  • Initialize the sensor.
DS18B20.begin(); // initialize the sensor
  • Issue the command to obtain temperatures.
DS18B20.requestTemperatures();
  • Check the temperature in Celsius.
temperature_C = DS18B20.getTempCByIndex(0);
  • Calculate Fahrenheit from Celsius:
temperature_F = temperature_C * 9 / 5 + 32;

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 2 // The Arduino Nano pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library float temperature_C; // temperature in Celsius float temperature_F; // temperature in Fahrenheit void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. DS18B20.begin(); // initialize the sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature_F = temperature_C * 9 / 5 + 32; // convert Celsius to Fahrenheit Serial.print("Temperature: "); Serial.print(temperature_C); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(temperature_F); // print the temperature in Fahrenheit Serial.println("°F"); delay(500); }

Detailed Instructions

  • Connect an USB cable to the Arduino Nano and the PC.
  • Open the Arduino IDE and select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas”, then locate the DallasTemperature library created by Miles Burton.
  • Press the Install button to install the DallasTemperature library.
Arduino Nano Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
Arduino Nano onewire library
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button on Arduino IDE to send the code to Arduino Nano.
  • Place the sensor in hot and cold water, or hold it in your hand.
  • Check out the result on the 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