Arduino DS18B20 Temperature Sensor Project

In this project, a simple thermometer will be constructed using a DS18B20 one-wire digital temperature sensor module that is interfaced with an Arduino. The temperature data will then be printed in both Celsius & Fahrenheit on a 0.96″ i2c OLED display. 

Table of Contents

Explanation of DS18B20 Arduino Project

The main advantage of the DS18B20, made by Dallas Instruments, is its one-wire interface that allows for a simple connection to a microcontroller, utilizing just one signal pin.

In addition, due to the fact that each sensor contains a unique 64-bit serial code, multiple sensors can be wired together and connected to a single Arduino digital pin for the integration of temperature data from many sources/sensors. 

Plus, because the sensor used in this project is housed within a module form and not a standalone sensor, a 4.7KΩ pull-up resistor is not needed and the sensor module can be connected to the Arduino directly. As you will see in the code, it is important that you download two libraries that are needed for the function of the DS18B20 sensor module: the OneWire and the DallasTemperatuer libraries. You should be able to do this via the Library Manager that is integrated into the Arduino IDE.

Similar to some of our other Arduino projects, an OLED display provides an efficient, well-lit method to display any textual information with sharpness and responsivity. The i2c interface makes connecting the OLED display to the Arduino extremely straightforward as only four pins are needed for the OLED to be hooked up to the Arduino. 

Additionally, with the support of Adafruit libraries, programming the OLED to display whatever is needed can be done with ease. Alongside some of our projects, a thermometer project like this proves to be versatile for a wide range of applications, including weather stations, automobile devices, smart watches, etc. Another aspect that you could consider to improve upon this project is to combine multiple sensors/modules together and then display data on an OLED display.

Project Preparation Phase

Although this is a simple electronics project, you will need to prepare the following components for the project before you start

  • Arduino Nano (other Arduino-compatible boards will work)
  • USB cable (compatible with the Arduino board)
  • Breadboard
  • Male-Male Jumper Wires (7)
  • 0.96″ 128×64 i2c OLED Display
  • DS18B20 Digital Temperature Sensor Module

Arduino DS18B20 Wiring

Depending on your Arduino board, you may or may not require a breadboard to plug your board in. In this example, an Arduino Nano is used, thus requiring a breadboard but if you are using an Arduino Uno, for example, the jumper wires can be plugged in from the components on the breadboard directly to the board pins. However, the wiring from the DS18B20 sensor module and the OLED to the Arduino board remains the same. A circuit diagram is also featured below.

DS18B20 Sensor Module: Connect the signal (S) pin of the sensor to D2, the positive (+) pin to +5v and the negative (-) pin to GND.

OLED: Connect SDA (serial data) to A4, SCL/SCK (serial clock) to A5, VDD/VCC (supply voltage) to +5v and GND to GND.

3.Now, you can plug in your Arduino board via the USB cable to the computer.

arduino ds18b20 wiring circuit diagram

Arduino Temperature Sensor DS18B20 Code

				
					#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define screen_width 128
#define screen_height 64
#define OLED_RESET 4
Adafruit_SSD1306 display(screen_width, screen_height);
void setup(void)
{
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 display.clearDisplay();
 sensors.begin();
 Serial.begin(9600);
}
void loop(void){
 sensors.requestTemperatures();
 Serial.print("Temperature (C): ");
 Serial.print(sensors.getTempCByIndex(0));
 Serial.print("   Temperature (F): ");
 Serial.println(sensors.getTempFByIndex(0));
 display.clearDisplay();
 display.setTextSize(1);
 display.setTextColor(SSD1306_WHITE);
 display.setCursor(0, 15);
 display.print("Temperature:");
 display.setCursor(80, 15);
 display.print(sensors.getTempCByIndex(0)); //Change to sensors.getTempFByIndex(0) for fareinheit
 display.setCursor(110, 15);
 display.print(" C");
 display.setCursor(80, 35);
 display.print(sensors.getTempFByIndex(0));
 display.setCursor(110, 35);
 display.print(" F");
 display.display();
 delay(1000);
}
				
			

About the Code

The code starts by including two key libraries for the function of the DS18B20 sensor: the OneWire and DallasTemperature libraries. Please make sure you download these libraries from the Library Manager that is built-in to the Arduino IDE. The digital pin for the DS18B20 sensor (D2) is also defined in the following line and the sensor is set up with these two libraries on the last two lines of the first block.

The next two blocks of code deal with setting up the OLED display and if you have seen our previous projects involving an OLED, this should be familiar to you. Three libraries are declared that are needed for the function of the OLED: the Wire, Adafruit GFX and Adafruit SSD1306 libraries. Then, the screen width and height of 128×64 are defined, followed by setting up the OLED reset pin (A4) and finally setting up the display with the libraries.

In the void setup section, we begin by initiating the OLED display and clearing the display from any pre-existing text/visuals. The DS18B20 sensor is then initiated and we begin serial communication by setting a baud rate of 9600 bauds.

The first part of the void loop section deals with printing temperature information from the sensor to the serial monitor. The first line is necessary when using this sensor to request temperature data from the sensor. Then, the temperature in Celsius and Fahrenheit can be printed using the sensors.getTempCByIndex(0) or sensors.getTempFByIndex(0) functions, respectively. 

Now, in order to display the temperature information on the OLED display, we start by clearing the display once again, setting the text size to 1 and setting the text colour to white. The following two blocks are responsible for positioning the cursor to certain points on the display and printing out text as well as temperature information received from the sensor, again using the same functions as mentioned above. The code ends by using the display.display() function to ensure that all the changes are put in effect and a delay of 1 second is added so that the display is updated with new temperature data every second.

Summarize

This project definitely highlights the capabilities of the DS18B20 Temperature Sensor Module in providing a simple, accurate sensor for detecting real-time temperature information. The fact that this sensor is conveniently housed in a module with a built-in pull-up resistor, alongside only using a one-wire interface, makes using this module extremely easy, especially when working with microcontrollers. Once again, this project additionally highlights the versatility of an OLED display in providing a sharp, real-time and reliable display for any sort of information or visuals. To expand upon this project, as mentioned above also, you could consider adding in more sensors (humidity, gas, UV sensors, etc.) to build a weather station or possibly interface the sensor to an ESP8266 to wireless upload real-time temperature data to a server or another device. From absolute beginners when it comes to Arduino to experience electronic hobbyists, a project like this is a great starting point that introduces so many aspects of this hobby.

Related Articles:

Raspberry Pi DS18B20 Temperature Sensor

More content you may be interested in

what is esp32
What is an ESP32

Dive into the world of microcontrollers with TechSparks’ comprehensive guide to the ESP32! Discover the power of this versatile chip, renowned for its integration, low

esp32 ledc
ESP32 LEDC: Precise Control for Diverse Applications

Dive into the world of precise LED control with ESP32’s LEDC module, revolutionizing microcontroller-based PWM technology for versatile applications. Explore the enhanced capabilities, flexible programming

How to Enable Multi-Core on ESP32 Microcontroller
How to Enable Multi-Core on ESP32 Microcontroller

This comprehensive guide elucidates the process of enabling multi-core functionality on the ESP32 microcontroller, leveraging its dual-core architecture for enhanced parallel processing capabilities. Through detailed

Scroll to Top