Sensors
Ultrasonic tape measure
Point it at a wall and read the distance in centimetres, without touching anything.
- Easy
- about 40 min
- ESP32-S3
- 6 parts
- 8 wires
- 61 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
It sends a click too high to hear and times the echo. Sound covers a centimetre in about 29 millionths of a second, so the sketch has to time very short moments precisely.
What you learn
Timing a pulse with pulseIn(), turning a time into a distance, and averaging readings.
How it works
input
HC-SR04 (3.3 V)
decides
ESP32-S3
output
OLED 0.96 in
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 6 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 8
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
sonic-ruler.ino
// Ultrasonic tape measure
// Point it at a wall and read the distance in centimetres.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int PIN_SONAR_TRIG = 5;
const int PIN_SONAR_ECHO = 6;
const int PIN_OLED_SDA = 8;
const int PIN_OLED_SCL = 9;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float readCm() {
digitalWrite(PIN_SONAR_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_SONAR_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_SONAR_TRIG, LOW);
unsigned long us = pulseIn(PIN_SONAR_ECHO, HIGH, 30000);
// Sound travels about 0.0343 cm per microsecond, there and back.
return us * 0.0343f / 2.0f;
}
void setup() {
pinMode(PIN_SONAR_TRIG, OUTPUT);
pinMode(PIN_SONAR_ECHO, INPUT);
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
}
void loop() {
// Average three readings: one echo can bounce off the wrong thing.
float total = 0;
int good = 0;
for (int i = 0; i < 3; i++) {
float cm = readCm();
if (cm > 2 && cm < 400) {
total += cm;
good++;
}
delay(30);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("Distance");
oled.setTextSize(3);
oled.setCursor(0, 24);
if (good > 0) {
oled.print(total / good, 1);
oled.setTextSize(2);
oled.print(" cm");
} else {
oled.print("---");
}
oled.display();
delay(150);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SSD1306: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.