Arduino UNO R4 - Ultrasonic Sensor - OLED
In this guide, we will learn how to measure distance using an ultrasonic sensor and display it on an OLED screen.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 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
Learn about OLED and Ultrasonic Sensor (pinout, functions, programming) in these tutorials:
- Arduino UNO R4 - OLED tutorial
- Arduino UNO R4 - 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 Uno R4 and other components.
Arduino UNO R4 Code - Ultrasonic Sensor - OLED
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the Arduino Uno R4 board to the ultrasonic sensor and OLED display according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Click on the Libraries icon located on the left side of the Arduino IDE.
- Type “SSD1306” in the search box, and look for the SSD1306 library created by Adafruit.
- Press the Install button to add the library.

- You will need to install additional library dependencies.
- Click the Install All button to install all the required libraries.

- Copy the code and open it in Arduino IDE
- Press the Upload button in Arduino IDE to send the code to Arduino UNO R4
- Wave your hand in front of the sensor
- Check the result on OLED and Serial Monitor
※ NOTE THAT:
The code centers the text both horizontally and vertically on the OLED display. For more details, see the guide on how to center text on OLED at How to vertical/horizontal center on OLED.