Arduino Nano ESP32 - Ultrasonic Sensor - LED
This tutorial instructs you how to use Arduino Nano ESP32 and Ultrasonic sensor to control the LED. In detail:
- The Arduino Nano ESP32 automatically turns LED on when an object is close to the ultrasonic sensor.
- The Arduino Nano ESP32 automatically turns LED off when an object is far from the ultrasonic sensor.
Hardware Preparation
1 | × | Arduino Nano ESP32 | |
1 | × | USB Cable Type-C | |
1 | × | Ultrasonic Sensor | |
1 | × | LED | |
1 | × | 220 ohm resistor | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) DC Power Jack | |
1 | × | (Recommended) Screw Terminal Adapter for Arduino Nano |
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
We have specific tutorials about LED and ultrasonic sensor. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn more about them at the following links:
Wiring Diagram
The wiring diagram with power supply from USB cable
![The wiring diagram between Arduino Nano ESP32 and Ultrasonic Sensor LED](https://newbiely.com/images/tutorial/arduino-nano-esp32-ultrasonic-sensor-led-wiring-diagram.jpg)
This image is created using Fritzing. Click to enlarge image
The wiring diagram with power supply from 5v adapter
![The wiring diagram between Arduino Nano ESP32 and Ultrasonic Sensor LED 5v power source](https://newbiely.com/images/tutorial/arduino-nano-esp32-ultrasonic-sensor-led-wiring-diagram-5v-power-adapter.jpg)
This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Move your hand in front of sensor
- See the change of LED's state
Line-by-line Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!