Fiverr gig
$15 — I will help you with your project and simulate it for you
Hire me
🔬 Sensor Tutorials

Arduino Ultrasonic

EmbedLab TeamApril 4, 20254 min read
Arduino Ultrasonic

Learn how to use an ultrasonic distance sensor with an Arduino Uno to detect nearby objects and control an LED. This beginner-friendly tutorial includes the circuit diagram and complete code explanation.


📌 Project Overview

This Arduino project demonstrates how to integrate an HC-SR04 ultrasonic distance sensor with an LED using an Arduino Uno. The setup measures the distance to nearby objects and turns on an LED if an object is within a specified range (e.g., 50 cm). It’s a great starting point for learning about Arduino sensors, distance measurement, and real-time feedback systems.


🔧 Components Needed

  • Arduino Uno

  • HC-SR04 Ultrasonic Sensor

  • LED

  • 220Ω Resistor

  • Breadboard and Jumper Wires

  • USB Cable for Programming


⚙️ How It Works

The ultrasonic sensor emits a pulse using the trigPin and listens for the echo on the echoPin. The duration of the echo is used to calculate the distance to an object. If the object is within the 50 cm threshold, the LED (connected to ledPin) is turned on.


🧠 Key Concepts Covered

  • Interfacing ultrasonic sensors with Arduino

  • Using pulseIn() to measure echo time

  • Converting echo duration to distance

  • Controlling outputs based on sensor data

  • Serial Monitor debugging

  • Real-world application: basic obstacle detection


🔌 Circuit Diagram

schematic




💻 Arduino Code: Ultrasonic Sensor with LED

// Define pin connections const int trigPin = 9; const int echoPin = 10; const int ledPin = 8; // Distance threshold (in centimeters) const int movementThreshold = 50; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); Serial.println("Starting distance sensor..."); } void loop() { // Send ultrasonic pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure echo duration long duration = pulseIn(echoPin, HIGH); // Calculate distance in cm int distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); // LED control logic if (distance > 0 && distance <= movementThreshold) { Serial.println("Object detected within range. Turning LED on."); digitalWrite(ledPin, HIGH); } else { Serial.println("No object detected or out of range. Turning LED off."); digitalWrite(ledPin, LOW); } delay(200); // Prevent sensor overload }

🔄 Applications of This Project

  • DIY home security systems

  • Proximity alerts

  • Line-following or obstacle-avoiding robots

  • Smart automation (lights, alarms)

  • Educational STEM kits


🧩 Keywords You’re Targeting

To help Google rank and understand this article, here are some keyword phrases already integrated:

  • Arduino ultrasonic distance sensor tutorial

  • LED control with HC-SR04

  • Arduino project for beginners

  • How to measure distance using Arduino

  • Object detection with Arduino Uno

  • Arduino code for ultrasonic sensor and LED






ET

EmbedLab Team

Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.

Related projects

Arduino Ultrasonic Sensor
🔬 Sensor Tutorials

Arduino Ultrasonic Sensor

Arduino ultrasonic projects let you measure distance and respond to it in real time. In this guide, you’ll build a reliable Arduino ultrasonic distance sensor that activates an LED when an object come

Nov 13 10 min
Arduino UNO with HC-SR04 ultrasonic sensor and LCD
🔬 Sensor Tutorials

Arduino UNO Ultrasonic Project — HC-SR04 Distance Sensor with Free Arduino IDE Code

Build an Arduino UNO ultrasonic distance project with the HC-SR04 sensor and LCD display: free Arduino IDE code and Arduino library — easily ported to Arduino Nano, Arduino Mega and ESP32 Arduino.

Oct 27 8 min
Arduino UNO with LM35 sensor and I2C LCD showing temperature
🔬 Sensor Tutorials

Arduino UNO I2C Temperature Monitor — LCD, LM35 & Free Arduino IDE Code

Build an Arduino UNO I2C temperature monitoring project step by step: LM35 sensor, I2C LCD display, free Arduino IDE code and Arduino library — same wiring works for Arduino Nano, Arduino Mega and ESP32 Arduino.

Nov 5 6 min
Arduino UNO with LM35 temperature sensor and LCD display
🔬 Sensor Tutorials

Fix LM35 Wrong Values on Arduino UNO — Free Arduino IDE Code & LCD Tutorial

How to fix LM35 wrong values on Arduino UNO: free Arduino IDE code, Arduino library, LCD display wiring and analog reference tips — also works for Arduino Nano, Arduino Mega and ESP32 Arduino temperature projects.

Oct 27 8 min
Build Arduino Smart Parking System with LCD & Sensors
🔬 Sensor Tutorials

Build Arduino Smart Parking System with LCD & Sensors

Arduino Smart Parking System Global schematic Are you tired of circling blocks looking for parking? Imagine a smart system that shows you instantly whether a spot is free. This Arduino Smart Parking S

Nov 5 10 min
Browse all 50+ projects