Circuits / Sensors
Arduino UNO: LED alarm under 15 cm (HC-SR04)
The program compares the distance it measured with a limit and lights an LED; pulseIn returns 0 when no echo comes, which is not "close"

Tap a part to see its voltages and currents. It is a model, not a measurement.
What do you need?
- Development board Arduino UNOAliExpress Amazon
- Ultrasonic distance sensor HC-SR04AliExpress Amazon
- Resistor 220 ΩAliExpress Amazon
- Red LEDAliExpress Amazon
- Jumper wires × 6AliExpress Amazon
- BreadboardAliExpress Amazon
The AliExpress links are affiliate links: if you buy through them ohmio.dev gets a commission, at no extra cost to you. Affiliate links
This page is a guide: the numbers come from a simulation, and real parts vary. Check your parts before you power the circuit. Safety and responsibility
How does an Arduino turn a distance into an alarm?
The sketch measures as the plain HC-SR04 meter does, a 10 µs pulse on TRIG, pulseIn on ECHO, microseconds ÷ 58, and then it compares. If the distance is more than 0 and less than 15 cm, it sets D13 high and the red LED lights; otherwise it sets it low. In the model an object at 14 cm lights the LED with 12.2 mA through R1 (220 Ω); at 15 cm and at 16 cm the LED is off, because the test is strictly less than 15. The 0 test matters: when no echo returns, pulseIn gives 0, and without the check an empty room would look like an object touching the sensor. A 450 cm target, out of range, prints 0 and the LED stays off. The 12.2 mA is under the 20 mA recommended for one UNO pin. D13 also drives the board's own small L LED, so both light together.
Values from the Protoboard ISO simulation: a model, not a bench measurement.
Hear about the next circuit
One email when a new circuit is out, about twice a month. Nothing else.