ESP8266 - Ultrasonic Sensor - OLED

This tutorial instructs you how to acquire the distance from an ultrasonic sensor and display it on an OLED.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×SSD1306 I2C OLED Display 128x64
1×Ultrasonic Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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.

Overview of OLED and Ultrasonic Sensor

If you are not familiar with OLED and Ultrasonic Sensor (including pinout, functioning, programming, etc.), the following tutorials can help:

Wiring Diagram

  • If powering ESP8266 via USB port:
The wiring diagram between ESP8266 NodeMCU and Ultrasonic Sensor OLED

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • If powering ESP8266 via USB port but power is not enough, use an external power source for OLED display and sensor
The wiring diagram between ESP8266 NodeMCU and Ultrasonic Sensor OLED external power

This image is created using Fritzing. Click to enlarge image

  • If powering ESP8266 via Vin pin:
The wiring diagram between ESP8266 NodeMCU and Ultrasonic Sensor OLED Vin pin

This image is created using Fritzing. Click to enlarge image

ESP8266 Code - Ultrasonic Sensor - OLED

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ultrasonic-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define TRIG_PIN D5 // The ESP8266 pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN D6 // The ESP8266 pin connected to Ultrasonic Sensor's ECHO pin Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C String distance_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 distance_str.reserve(10); // to avoid fragmenting memory when using String } void loop() { // Produce a 10-microsecond pulse to the TRIG pin. digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Measure the pulse duration from the ECHO pin long duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance float distance_cm = 0.017 * duration_us; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); distance_str = String(distance_cm, 2); // two decimal places distance_str += " cm"; Serial.println(distance_str); // print the temperature in Celsius to Serial Monitor oled_display_center(distance_str); // display temperature on OLED } 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • 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 install the library.
ESP8266 NodeMCU OLED library
  • You will be prompted to install additional library dependencies.
  • To install all of them, click the Install All button.
ESP8266 NodeMCU Adafruit GFX sensor library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the IDE to transfer the code to the ESP8266.
  • Move your hand in front of the sensor.
  • Check out the outcome on the OLED display and in the Serial Monitor.

※ NOTE THAT:

The code below will enable automatic horizontal and vertical centering of text on an OLED display. To learn more, please see How to vertical/horizontal center on OLED.

Video Tutorial