Arduino Nano 33 IoT - Temperature Sensor - OLED

This guide teaches you how to get the temperature reading from a DS18B20 sensor that uses one wire and display it on an OLED screen.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
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
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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.
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 Nano 33 IoT, please check out these tutorials:

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

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT DS18B20 Temperature Sensor OLED

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor with a wiring adapter for a simple connection. The adapter already has a built-in resistor, so you don't have to add one separately.

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

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-temperature-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <OneWire.h> #include <DallasTemperature.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define SENSOR_PIN 2 // The Arduino Nano 33 IoT pin connected to DS18B20 sensor's data pin Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_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; 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.reserve(10); // to avoid fragmenting memory when using String } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float tempCelsius = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature = String(tempCelsius, 2); // two decimal places temperature += char(247) + String("C"); Serial.println(temperature); // print the temperature in Celsius to Serial Monitor oledDisplayCenter(temperature); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Type SSD1306 in the search box and look for the SSD1306 library by Adafruit.
  • Click the Install button to add the library.
Arduino Nano 33 IoT OLED library
  • You may be asked to install some extra libraries.
  • Click the Install All button to install all the libraries.
Arduino Nano 33 IoT Adafruit GFX sensor library
  • Type DallasTemperature in the search box, then find the library named DallasTemperature by Miles Burton.
  • Click the Install button to add the DallasTemperature library.
Arduino Nano 33 IoT Dallas Temperature library
  • You might be asked to add a needed tool. Click the Install All button to add the OneWire library.
Arduino Nano 33 IoT onewire library
  • Copy the code above and open it using the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino Nano 33 IoT.
  • Place the sensor in hot and cold water, or hold the sensor in your hand.
  • Check the results on the OLED screen.

※ NOTE THAT:

This code automatically centers the text on the OLED display from side to side and from top to bottom.

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!