Arduino UNO R4 - Ultrasonic Sensor - LED
In this tutorial, we'll learn how to use an ultrasonic sensor to control an LED based on object distance. When an object is close to the sensor, the LED will turn on. When the object moves away, the LED will turn off. This project is perfect for beginners and helps understand ultrasonic distance measurement and basic LED control with Arduino.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of LED and Ultrasonic Sensor
Learn about LED and ultrasonic sensors (pinout, operation, programming) in these tutorials:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
Arduino UNO R4 Code
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy and paste the code into the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the Arduino UN First R4.

- Slide your hand over the sensor
- Check out the LED's reaction
Code Explanation
Look at the explanations given in the comments within the source code!
※ NOTE THAT:
This code is for learning. The ultrasonic sensor is sensitive to noise. To use the ultrasonic sensor properly, you should remove noise. Learn how to remove noise from the ultrasonic sensor here.