Arduino UNO R4 - Temperature Sensor

This tutorial instructs you how to use the DS18B20 1-wire temperature sensor with the Arduino UNO R4. In detail, we will learn:

Arduino UNO R4 temperature sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4
1×(Recommended) Power Splitter For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

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 One Wire Temperature Sensor DS18B20

Pinout

The DS18B20 temperature sensor comes with three pins.

  • GND pin: Connect this to GND (0V).
  • VCC pin: Connect this to VCC (5V or 3.3V).
  • DATA pin: This is the 1-Wire Data bus. Connect it to a digital pin on the Arduino UNO R4.

The sensor comes in two types: the TO-92 package, which looks like a transistor, and the waterproof probe. In this tutorial, we will use the waterproof probe type.

DS18B20 temperature sensor Pinout

To connect a DS18B20 temperature sensor to an Arduino UNO R4, you need a pull-up resistor, which can be complicated. Some manufacturers provide a wiring adapter with a pull-up resistor and a screw terminal block already included, which simplifies the setup.

Wiring Diagram

  • Breadboard wiring diagram
The wiring diagram between Arduino UNO R4 Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with the wiring adapter
The wiring diagram between Arduino UNO R4 DS18B20

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor with a wiring adapter. This adapter makes it simple to connect because it includes a resistor, so you don't need an extra one.

How To Program For DS18B20 Temperature Sensor

  • Include the library:
#include <OneWire.h> #include <DallasTemperature.h>
  • Create a OneWire object and a DallasTemperature object for the pin connected to the sensor's DATA pin.
OneWire oneWire(SENSOR_PIN); // Initialize a new OneWire instance on the sensor pin DallasTemperature DS18B20(&oneWire); // Create a DallasTemperature object linked to the OneWire instance
  • Set up the sensor:
DS18B20.begin(); // Start the sensor initialization process
  • Send the command to check temperatures:
DS18B20.requestTemperatures();
  • Check the temperature in Celsius.
temperature_C = DS18B20.getTempCByIndex(0);
  • (Optional) Change Celsius temperature to Fahrenheit.
temperature_F = temperature_C * 9 / 5 + 32;

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 4 // The Arduino UNO R4 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library void setup() { Serial.begin(9600); // initialize serial DS18B20.begin(); // initialize the sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius float 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the DS18B20 1-wire temperature sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Search for DallasTemperature and find the library by Miles Burton.
  • Click the Install button to add the DallasTemperature library.
Arduino UNO R4 Dallas Temperature library
  • You need to install a library dependency
  • Click the Install All button to install the OneWire library.
Arduino UNO R4 onewire library
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to send the code to Arduino UNO R4.
  • Place the sensor in hot and cold water, or hold it in your hand.
  • Check the results 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

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!