Arduino Nano - Ultrasonic Sensor - OLED
This tutorial instructs you how to use Arduino Nano obtain the distance from an ultrasonic sensor and show it on an OLED display.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Buy Note: If you want a bigger OLED display, use the 2.42 inch OLED Display 128x64 .
Overview of OLED and Ultrasonic Sensor
If you are unfamiliar with OLED and Ultrasonic Sensor (including pinout, functionality, and programming), the following tutorials can provide you with the necessary information:
- Arduino Nano - OLED tutorial
- Arduino Nano - Ultrasonic Sensor tutorial
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
Arduino Nano Code - Ultrasonic Sensor - OLED
Detailed Instructions
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search for “SSD1306” and locate the SSD1306 library provided by Adafruit.
- Press the Install button to complete the installation.

- You will be prompted to install additional library dependencies.
- To install them all at once, click the Install All button.

- Copy the code and open it with the Arduino IDE.
- Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
- Move your hand in front of the sensor.
- Check the result on the OLED and the Serial Monitor.
※ NOTE THAT:
This code will horizontally and vertically center the text on an OLED display. For more information, please refer to How to vertical/horizontal center on OLED.