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

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Ultrasonic Sensor
1×LED Kit with resistor
1×LED (red)
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

The wiring diagram between Arduino UNO R4 Ultrasonic Sensor LED

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

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-ultrasonic-sensor-led */ #define TRIG_PIN 6 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 7 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin #define LED_PIN 3 // The Arduino UNO R4 pin connected to LED's pin #define DISTANCE_THRESHOLD 50 // centimeters 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); }

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.
Arduino IDE Upload Code
  • 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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!