Send a pulse, time the echo, divide by two, convert to centimetres. Unlike an analog sensor this is a timing measurement, so you meet pulse timing and microsecond resolution. One safety point: the HC-SR04 is a 5V part and its echo pin outputs 5V, while both boards expect 3.3V — use a divider on that line rather than wiring it straight.
Every item is in the kit — nothing extra to buy.
All 24 parts in one box, with this project and 15 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
06 — Ultrasonic tape measure
Board: ESP32. Parts: HC-SR04, LCD 1602 (I2C backpack), 3 x 1 kΩ from the resistor pack.
Library: "LiquidCrystal I2C" (Frank de Brabander).
Wiring:
HC-SR04: VCC -> VIN (5 V), GND -> GND, TRIG -> GPIO 5
ECHO -> 1 kΩ -> GPIO 18, and from GPIO 18 two 1 kΩ in series -> GND
(ECHO outputs 5 V; the divider gives the ESP32 3.3 V. Do not skip it.)
LCD: VCC -> VIN (5 V), GND -> GND, SDA -> GPIO 21, SCL -> GPIO 22
Distance = (echo time in microseconds) / 58. Sound goes there and back, so
the time is halved, and at ~343 m/s that works out to 58 us per centimetre.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
LiquidCrystal_I2C lcd(0x27, 16, 2);
float readCm() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL); // 30 ms timeout ~ 5 m
if (us == 0) return -1; // nothing in range
return us / 58.0;
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.init();
lcd.backlight();
lcd.print("Tape measure");
}
void loop() {
// Average 5 readings so one bad echo does not make the number jump.
float sum = 0; int n = 0;
for (int i = 0; i < 5; i++) {
float d = readCm();
if (d > 0) { sum += d; n++; }
delay(30);
}
lcd.setCursor(0, 1);
if (n == 0) {
lcd.print("out of range ");
Serial.println("out of range");
} else {
float cm = sum / n;
lcd.print(cm, 1); lcd.print(" cm ");
Serial.print(cm, 1); Serial.println(" cm");
}
delay(150);
}
The DHT11 sends both temperature and humidity down a single data wire using its own timing protocol, so a library does the decoding for you. Wire three pins, install the…
Show live readings on the LCD and sound the buzzer when the room crosses a threshold you set. The interesting part is what happens at the boundary: a naive version chatt…
Take the distance reading and turn it into beep rate: slow far away, faster as you close in, solid at the stop point. Short to build, and it teaches you to turn a number…