DEV Community

Cover image for Arduino Nano ESP32 - Ultrasonic Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Nano ESP32 - Ultrasonic Sensor

The HC-SR04 ultrasonic sensor measures distance by sending out a sound pulse and timing how long it takes to bounce back, the same trick bats and sonar use. Paired with an Arduino Nano ESP32, it's a cheap way to add distance sensing to a project, whether that's a parking sensor, an obstacle detector, or a trigger for something else in your code.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano ESP32 - Ultrasonic Sensor

What you need

Wiring

Arduino Nano ESP32 - Ultrasonic Sensor wiring diagram

The HC-SR04 has four pins. VCC connects to the board's 5V, and GND connects to GND. TRIG is an input on the sensor that receives a control pulse from the ESP32, and ECHO is an output that sends a pulse back whose length corresponds to the measured distance. You wire TRIG and ECHO to two digital pins on the Nano ESP32, for example D2 and D3. The wiring differs slightly depending on whether you power the board over USB or through the Vin pin.

How it works

You tell the sensor to "ping" by pulsing the TRIG pin high for 10 microseconds. The sensor then sends out an ultrasonic burst and raises its ECHO pin high the moment the sound leaves, holding it high until the echo comes back. Reading how long ECHO stays high tells you how long the sound traveled, and since sound speed is constant, you can convert that time directly into a distance in centimeters.

Code

The sketch sets TRIG as an output and ECHO as an input with pinMode(). To take a reading, it pulses TRIG high for 10 microseconds using digitalWrite() and delayMicroseconds(), then calls pulseIn() on the ECHO pin to capture how long the return pulse lasts, in microseconds. That duration gets converted into a distance in centimeters and printed with Serial.println(), so you can watch the numbers change in the Serial Monitor as you move your hand toward and away from the sensor.

arduino-nano-esp32-ultrasonic-sensor.ino

/*
 * 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
 */

#define TRIG_PIN D2 // The Arduino Nano ESP32 pin connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN D3 // The Arduino Nano ESP32 pin connected to Ultrasonic Sensor's ECHO pin

float duration_us, distance_cm;

void setup() {
  // begin serial port
  Serial.begin (9600);

  // Configure the trigger pin to output mode
  pinMode(TRIG_PIN, OUTPUT);
  // Configure the echo pin to input mode
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Produce a 10-microsecond pulse to the TRIG pin.
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the pulse duration from the ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-ultrasonic-sensor

Top comments (0)