Arduino Mega - Temperature Sensor - OLED

This guide shows you how to use an Arduino Mega to read the temperature from a DS18B20 sensor and display it on an OLED screen.

Arduino Mega DS18B20 Temperature Sensor OLED

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Breadboard
1×Jumper Wires

Or you can buy the following 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 OLED and DS18B20 Temperature Sensor

If you're new to using the OLED, DS18B20 Temperature Sensor, and Arduino Mega, please check out these tutorials:

These tutorials explain how OLED and DS18B20 Temperature Sensor work, their pinouts, how to connect them to the Arduino Mega, and how to program Arduino Mega to work with the OLED and DS18B20 Temperature Sensor.

Wiring Diagram

The wiring diagram between Arduino Mega DS18B20 Temperature Sensor OLED

This image is created using Fritzing. Click to enlarge image

We suggest buying a DS18B20 temperature sensor with a wiring adapter for easy setup. The adapter has a resistor, so you don’t need to buy another one for the wiring.

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

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-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 8 // The Arduino Mega pin connected to DS18B20 sensor 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 += char(247) + String("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

Follow these steps one by one.

  • Connect the OLED and the temperature sensor to the Arduino Mega board following the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Type SSD1306 in the search box, then find the SSD1306 library by Adafruit.
  • Click Install to add the library.
Arduino Mega OLED library
  • You need to install some extra libraries.
  • Click the Install All button to install all the libraries you need.
Arduino Mega Adafruit GFX sensor library
  • Type "DallasTemperature" in the search box and find the DallasTemperature library by Miles Burton.
  • Press the Install button to install the DallasTemperature library.
Arduino Mega Dallas Temperature library
  • You need to install the required library.
  • Click the Install All button to install the OneWire library.
Arduino Mega onewire library
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Put the sensor in hot or cold water, or hold it in your hand.
  • Look at the OLED screen to see the results.

※ NOTE THAT:

The code automatically puts the text in the middle of the OLED screen, both left-right and up-down.

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!