Ultrasonic Sensor - LED | Arduino Tutorial
Maybe your like
- TUTORIALS
- HARDWARE & TOOLS
- REFERENCES
- FAQs
- ABOUT US
SENSORS/ACTUATORS
- Arduino - Software Installization
- Arduino - Hardware Preparation
- Arduino - Hello World
- Arduino - Code Structure
- Arduino - Serial Monitor
- Arduino - Serial Plotter
- Arduino - LED - Blink
- Arduino - LED - Blink Without Delay
- Arduino - Blink multiple LED
- Arduino - LED - Fade
- Arduino - RGB LED
- Arduino - Traffic Light
- Arduino - Button
- Arduino - Button - Debounce
- Arduino - Button - Long Press Short Press
- Arduino multiple Button
- Arduino - Switch
- Arduino - Limit Switch
- Arduino - DIP Switch
- Arduino - Button - LED
- Arduino - Button - Relay
- Arduino - Button Toggle LED
- Arduino - Button Toggle Relay
- Arduino - Button - Piezo Buzzer
- Arduino - Button - Servo Motor
- Arduino - Potentiometer
- Arduino - Potentiometer fade LED
- Arduino - Potentiometer Triggers LED
- Arduino - Potentiometer Triggers Relay
- Arduino - Potentiometer Triggers Piezo Buzzer
- Arduino - Potentiometer Triggers Servo Motor
- Arduino - Rotary Encoder
- Arduino - Rotary Encoder LED
- Arduino - Rotary Encoder - Servo Motor
- Arduino - Piezo Buzzer
- Arduino - Buzzer
- Arduino - Motor
- Arduino - DC Motor
- Arduino - DC Motor - Limit Switch
- Arduino - Servo Motor
- Arduino - MG996R
- Arduino - Servo Motor controlled by Potentiometer
- Arduino - Light Sensor
- Arduino - LDR Module
- Arduino - Light Sensor Triggers LED
- Arduino - Light Sensor Triggers Relay
- Arduino - Light Sensor Triggers Servo Motor
- Arduino - Ultrasonic Sensor
- Arduino - Ultrasonic Sensor - LED
- Arduino - Ultrasonic Sensor - Relay
- Arduino - Ultrasonic Sensor - Piezo Buzzer
- Arduino - Ultrasonic Sensor - Servo Motor
- Arduino - Ultrasonic Sensor - LCD
- Arduino - Ultrasonic Sensor - OLED
- Arduino - Motion Sensor
- Arduino - Motion Sensor - LED
- Arduino - Motion Sensor - Relay
- Arduino - Motion Sensor - Piezo Buzzer
- Arduino - Motion Sensor - Servo Motor
- Arduino - Motion Sensor - MP3 Player
- Arduino - Motion Sensor - LED Strip
- Arduino - Relay
- Arduino - Relay Shield
- Arduino - 2-Channel Relay Module
- Arduino - 4-Channel Relay Module
- Arduino - Controls Fan
- Arduino - Controls Heating Element
- Arduino - Car
- Arduino - Actuator
- Arduino - Actuator with Feedback
- Arduino - Joystick
- Arduino - Joystick - Servo Motor
- Arduino - Soil Moisture Sensor
- Arduino - Soil Moisture Sensor Pump
- Arduino - Automatic Irrigation System
- Arduino - LCD
- Arduino - LCD I2C
- Arduino - LCD 20x4
- Arduino - LCD Keypad Shield
- Arduino - OLED
- Arduino - TFT LCD Display
- Arduino - TFT LCD Touch Screen Display
- Arduino - Round Circular TFT LCD Display
- Arduino - Button Count - OLED
- Arduino - Button Count - LCD
- Arduino - 74HC595 4-Digit 7-Segment Display
- Arduino - TM1637 4-Digit 7-Segment Display
- Arduino - LED Matrix
- Arduino - Bluetooth LED Matrix
- Arduino - LED Matrix via Web
- Arduino - Keypad
- Arduino - Keypad 1x4
- Arduino - Keypad - LCD
- Arduino - Keypad - Beep
- Arduino - Keypad - Buzzer
- Arduino - Keypad - Relay
- Arduino - Keypad - Servo Motor
- Arduino - Keypad - Solenoid Lock
- Arduino - Keypad - Electromagnetic Lock
- Arduino - Temperature Sensor
- Arduino - Temperature Sensor - LCD
- Arduino - Temperature Sensor - OLED
- Arduino - Temperature Sensor - LED Matrix
- Arduino - Temperature via Web
- Arduino - Temperature Sensor - Servo Motor
- Arduino - Temperature Sensor - Relay
- Arduino - DHT11
- Arduino - DHT22
- Arduino - Temperature Humidity Sensor
- Arduino - Temperature Humidity Sensor - LCD
- Arduino - Temperature Humidity Sensor - OLED Display
- Arduino - LM35 Temperature Sensor
- Arduino - TMP36 Temperature Sensor
- Arduino - Display Temperature from LM35 Sensor on OLED
- Arduino - Display Temperature from LM35 Sensor on LCD
- Arduino - Control Temperature
- Arduino - Cooling System using DHT Sensor
- Arduino - Cooling System using DS18B20 Temperature Sensor
- Arduino - Heating System
- Arduino - DHT11 - Relay
- Arduino - DHT11 - OLED
- Arduino - DHT11 - LCD
- Arduino - DHT22 - Relay
- Arduino - DHT22 - OLED
- Arduino - DHT22 - LCD
- Arduino - Touch Sensor
- Arduino - Touch Sensor - LED
- Arduino - Touch Sensor - Relay
- Arduino - Touch Sensor Toggle LED
- Arduino - Touch Sensor Toggle Relay
- Arduino - Touch Sensor - Piezo Buzzer
- Arduino - Touch Sensor - Servo Motor
- Arduino - Touch Sensor - Solenoid Lock
- Arduino - Door Sensor
- Arduino - Door Sensor - LED
- Arduino - Door Sensor - Relay
- Arduino - Door Sensor Toggle LED
- Arduino - Door Sensor Toggle Relay
- Arduino - Door Sensor - Piezo Buzzer
- Arduino - Door Sensor - Servo Motor
- Arduino - Solenoid Lock
- Arduino - Electromagnetic Lock
- Arduino - Button Controls Electromagnetic Lock
- Arduino - Keypad Door Lock
- Arduino - Door Lock System using Password
- Arduino - Controls Door Lock via Web
- Arduino - RFID/NFC
- Arduino - RFID/NFC - Relay
- Arduino - RFID/NFC - Servo Motor
- Arduino - RFID - Solenoid Lock
- Arduino - RFID - Electromagnetic Lock
- Arduino - RFID/NFC Door Lock System
- Arduino - RTC
- Arduino - DS1307 RTC Module
- Arduino - OLED Clock
- Arduino - LCD Clock
- Arduino - 7-segment Clock
- Arduino - Water Sensor
- Arduino - Water Leak Detector
- Arduino - Water Sensor Relay
- Arduino - Water Sensor Pump
- Arduino - Water/Liquid Valve
- Arduino - Controls Pump
- Arduino - Button - Pump
- Arduino - Rain Sensor
- Arduino - Rain Sensor - LED
- Arduino - Rain Sensor - Relay
- Arduino - Rain Sensor - Buzzer
- Arduino - Rain Sensor - Servo Motor
- Arduino - Measure Voltage
- Arduino - Sound Sensor
- Arduino - Sound Sensor - LED
- Arduino - Sound Sensor - Relay
- Arduino - Sound Sensor - Servo Motor
- Arduino - Force Sensor
- Arduino - Flame Sensor
- Arduino Fire Alarm
- Arduino - Gas Sensor
- Arduino - Gas Sensor - Relay
- Arduino - Infrared Obstacle Avoidance Sensor
- Arduino - Stepper Motor
- Arduino - Controls 28BYJ-48 Stepper Motor using ULN2003 Driver
- Arduino - Controls Stepper Motor using L298N Driver
- Arduino - DRV8825 Stepper Motor Driver
- Arduino - Stepper Motor and Limit Switch
- Arduino - GPS
- Arduino - LED Strip
- Arduino - NeoPixel LED Strip
- Arduino - WS2812B LED Strip
- Arduino - Dotstar Led Strip
- Arduino - Micro SD Card
- Arduino - Write Variable to SD Card
- Arduino - Log Data with Timestamp to SD Card
- Arduino - Read Config from SD Card
- Arduino - Audio
- Arduino - MP3 Player
- Arduino - RFID MP3 Player
- Arduino - Siren
- Arduino - SoftwareSerial
- Arduino RS232
- Arduino RS485
- Arduino RS422
- Arduino Serial to WiFi Converter
- Arduino RS232 to WiFi
- Arduino RS485 to WiFi
- Arduino RS422 to WiFi
- Arduino - IR Remote Control
- Arduino - Bluetooth
- Arduino - BLE
- Arduino controls Servo Motor via Bluetooth
- Arduino controls LED via Bluetooth
- How to get started with Arduino Uno R4
- Arduino Uno R4 WiFi controls LED via Web
- Arduino Uno R4 WiFi controls Relay via Web
- Arduino Uno R4 LED Matrix displays Number Character
INTERNET OF THING (IoT)
- Arduino - Ethernet
- Arduino - Ethernet Module
- Arduino - Ethernet Shield 2
- Arduino - PHPoC Shield
- Arduino - WiFi
- Arduino - TCP Client
- Arduino - Web Server
- Arduino - Web Server Multiple Pages
- Arduino - Web Server Password
- Arduino - WebSocket
- Arduino - Web Plotter
- Arduino - Controls Servo Motor via Web
- Arduino - Controls Car via Web
- Arduino - IoT
- Arduino - MQTT
- Arduino Shield
- Arduino - HTTP Request
- Arduino - HTTPS Request
- Arduino - MySQL
- Arduino - Modbus
- Communication between two Arduino
- Arduino to Arduino MQTT Communication
- Arduino - IFTTT
- Arduino - Make Voice Phone Call
- Arduino - Send SMS Message
- Arduino - Gmail
- Arduino - Door Open Email Notification
- Arduino - Motion Sensor Email Notification
- Arduino - Water Leak Email Notification
- Arduino - Secure Your Belongings: Detect Theft and Send Email Alerts
- Arduino - Send Email
- Arduino - Door Open - Send Email Notification
- Arduino - Temperature - Send Email Notification
LIBRARIES
- Arduino - Button Library
- Example - 01.Single Button
- Example - 02.Single Button Events
- Example - 03.Single Button Debounce
- Example - 04.Single Button All
- Example - 05.Multiple Button All
- Example - 06.Button Count
- Example - 07.Button Array
- Arduino - LED Library
- Example - LED Blink
- Example - LED Blink In Period
- Example - LED Blink Number Of Times
- Example - LED Fade In Fade Out
- Example - LED On Off
- Example - LED Toggle
- Example - Multiple LED
- Example - LED Array
- Arduino - Output Library
- Example - 01.On Off
- Example - 02.Toggle
- Example - 03.Single Blink Without Delay
- Example - 04.Single Blink Change Frequency
- Example - 05.Multiple Blink Without Delay
- Example - 06.Multiple Blink With Offset
- Example - 07.Blink In Period
- Example - 08.Pulse
- Arduino - Buzzer Library
- Example - Beep
- Example - Melody
- Example - Melody Repeat
- Arduino - Analog Keypad Library
- Example - Analog Keypad
- Example - Analog Button Array
We are going to learn how to:
- If the object is close to ultrasonic sensor, turn LED on
- If the object is far from ultrasonic sensor, turn LED off
Hardware Required
| 1 | × | Official Arduino Uno |
| 1 | × | Alternatively, DIYables STEM V3, Fully Compatible with Arduino Uno R3 |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) |
| 1 | × | Ultrasonic Sensor |
| 1 | × | LED Kit |
| 1 | × | LED (red) |
| 1 | × | LED Module |
| 1 | × | 220Ω Resistor |
| 1 | × | Breadboard |
| 1 | × | Jumper Wires |
| 1 | × | Recommended: Screw Terminal Block Shield for Arduino Uno |
| 1 | × | Recommended: Breadboard Shield for Arduino Uno |
| 1 | × | Recommended: Enclosure for Arduino Uno |
| 1 | × | Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Buy Note: To simplify the wiring process, we recommend using the LED Module, which comes with a built-in resistor.
About LED and Ultrasonic Sensor
If you do not know about LED and ultrasonic sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:
- Arduino - LED tutorial
- Arduino - Ultrasonic Sensor tutorial
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Code
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-led */ // constants won't change const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin const int LED_PIN = 3; // Arduino pin connected to LED's pin const int DISTANCE_THRESHOLD = 50; // centimeters // variables will change: float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if(distance_cm < DISTANCE_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }Quick Steps
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Move your hand in front of sensor
- See the change of LED's state
Code Explanation
Read the line-by-line explanation in comment lines of source code!
※ NOTE THAT:
The above code is for the learning purpose. The ultrasonic sensor is very sensitive to noise. If you want to use the ultrasonic sensor in practice, you should filter noise for the ultrasonic sensor. See how to filter noise for ultrasonic sensor
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
Ultrasonic Sensor and LED on Commercial Products
- TP-Link AC1750 Smart WiFi Router - Dual Band Gigabit Wireless Internet Routers
- Gaming Keyboard and Mouse
The Best Arduino Starter Kit
- See the best Arduino kit for beginner
See Also
- Arduino - Ultrasonic Sensor
- Arduino - Ultrasonic Sensor - Relay
- Arduino - Ultrasonic Sensor - Piezo Buzzer
- Arduino - Ultrasonic Sensor - Servo Motor
- Arduino - Ultrasonic Sensor - LCD
- Arduino - Ultrasonic Sensor - OLED
※ OUR MESSAGES
- We are AVAILABLE for HIRE. See how to hire us to build your project
- If this tutorial is useful for you, please give us motivation to make more tutorials.
- You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
TABLE OF CONTENTS
- Hardware Required
- About LED and Ultrasonic Sensor
- Wiring Diagram
- Arduino Code
- Video
- Ultrasonic Sensor and LED on Commercial Products
- The Best Arduino Starter Kit
- See Also
Tag » Arduino Hc-sr04 Led
-
Simple Project With The Ultrasonic Sensor (HC-SR04) +LED
-
Arduino HC-SR04 + LED : 5 Steps (with Pictures) - Instructables
-
Controlling LED's Using Ultrasonic Distance Sensor - Arduino Cloud
-
Find Distance With LEDs And Ultrasonic Sensor - Arduino Project Hub
-
Arduino Ultrasonic Sensor Led Projects | Hc-sr04 ... - YouTube
-
Ultrasonic Sensor With Arduino - Coderdojo Athlone
-
Controlling LED's Using Ultrasonic Distance Sensor
-
Using-ultrasonic-sensor-with-arduino/hc-sr04-o At Master
-
[GUIDE] Arduino Utiliser Un Capteur De Distance HC-SR04
-
Simple Project With The Ultrasonic Sensor (HC-SR04) +LED - Pinterest
-
Ultrasonic Sensor HC-SR04 LED If Else By Churchjw - Codebender
-
Tuto-Arduino Utiliser Un Module Ultrason HC-SR04 - Memorandum
-
Complete Guide For Ultrasonic Sensor HC-SR04 With Arduino
-
Distance Sensor Hc-Sr04 और Led Project - Mechanic37