Arduino Nano - Ultrasonic Sensor - LED
This tutorial instructs you how to use Arduino Nano and ultrasonic sensor to control LED. In detail:
- Arduino Nano turns on the LED if the object is near the ultrasonic sensor.
- Arduino Nano turns off the LED if the object is distant from the ultrasonic sensor.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Overview of LED and Ultrasonic Sensor
If you are unfamiliar with LED and ultrasonic sensor, including pinout, how they work, and how to program them, the following tutorials can help:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Nano Code
Detailed Instructions
- Plug the USB cable into the Arduino Nano and PC.
- Launch the Arduino IDE and select the appropriate board and port.
- Copy the code and open it in the Arduino IDE.
- Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
- Move your hand in the vicinity of the sensor,
- Check out the alteration in the LED's state.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
※ NOTE THAT:
The code above is for educational purposes. The ultrasonic sensor is very sensitive to noise. If you want to use it in a practical setting, you should filter out the noise for the ultrasonic sensor. For more information on how to do this, please refer to how to filter noise for ultrasonic sensor.