Arduino Nano - Temperature Sensor - OLED

This tutorial instructs you how to use Arduino Nano to acquire the temperature from a DS18B20 one wire sensor and show it on an OLED display.

Arduino Nano DS18B20 Temperature Sensor OLED

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×SSD1306 I2C OLED Display 128x64
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 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. 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 OLED and DS18B20 Temperature Sensor

If you are unfamiliar with OLED and DS18B20 Temperature Sensor (pinout, how it works, how to program ...), the following tutorials can provide you with more information:

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Arduino Nano and Temperature Sensor OLED

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a terminal adapter (recommended).
The wiring diagram between Arduino Nano and DS18B20 Temperature Sensor OLED

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.

Arduino Nano Code - Temperature from DS18B20 Temperature Sensor and display it on OLED

/* * 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-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <OneWire.h> #include <DallasTemperature.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define SENSOR_PIN 2 // The Arduino Nano pin connected to DS18B20 sensor's DQ pin Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library String temperature_str; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display DS18B20.begin(); // initialize the sensor temperature_str.reserve(10); // to avoid fragmenting memory when using String } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature_str = String(temperature_C, 2); // two decimal places temperature_str += "°C"; Serial.println(temperature_str); // print the temperature in Celsius to Serial Monitor oled_display_center(temperature_str); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Detailed Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “SSD1306” and locate the SSD1306 library by Adafruit.
  • Then, press the Install button to complete the installation.
Arduino Nano OLED library
  • You will be prompted to install some additional library dependencies.
  • To install all of them, click the Install All button.
Arduino Nano Adafruit GFX sensor library
  • Search for “Dallas”, and 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 with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Place the sensor in hot and cold water, or hold it in your hand.
  • Check out the result on the OLED display.

※ NOTE THAT:

The code in question will center the text both horizontally and vertically on the OLED display.

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!