Arduino Ultrasonic Sensor Tutorial – Control an LED with Distance Detection

Schematic2

list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!


 

🧠 Arduino Ultrasonic Distance Sensor Tutorial: Control an LED Based on Distance

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






list of products from Amazon (Not mine, i just link them!)


Please NOTE that ELEGOO is not Arduino but it is compatible with Arduino IDE, and mutch cheaper!


CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)

CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)

Post a Comment

0 Comments