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:

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 kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

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

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

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-ultrasonic-sensor-led */ #define TRIG_PIN D5 // The Arduino Nano ESP32 pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN D4 // The Arduino Nano ESP32 pin connected to Ultrasonic Sensor's ECHO pin #define LED_PIN D11 // The Arduino Nano ESP32 pin connected to LED's pin #define 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 ESP32 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32 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

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!

Video Tutorial