arduino ultrasonic sensor example

Arduino Ultrasonic Sensor Projects: Top Examples & Tutorials

By Michael Clark
AD

Empowering various applications from robotics to consumer electronics, Arduino's versatility lies in its ability to integrate with a wide array of sensors. Among these, the ultrasonic sensor plays a pivotal role in enhancing interaction with the physical environment, offering distance measurement at a higher level than other sensors. Let's delve into the world of Arduino and discover an insightful example involving the HC-SR04 ultrasonic sensor.

Before we embark on this journey, let's ensure you have a basic understanding of the Arduino platform, specifically the Arduino Uno, which we'll use for this project. Arduino Uno is a microcontroller board designed for beginners and hobbyists, packed with 14 digital I/O pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, and an open-source IDE for coding.

Understanding the HC-SR04 Ultrasonic Sensor

The HC-SR04 is an inexpensive, compact, and highly efficient ultrasonic sensor that operates using the concept of sonar. It measures distance by sending out ultrasonic pulses and recording the time it takes for the echo to bounce back to its receiver.

AD
HC-SR04 Ultrasonic Sensor with ESP32 - Measure Distance
HC-SR04 Ultrasonic Sensor with ESP32 - Measure Distance
Arduino Projects: Ultrasonic Distance Sensor - Tutorial45
Arduino Projects: Ultrasonic Distance Sensor - Tutorial45
Ultrasonic Sensor Arduino Interfacing
Ultrasonic Sensor Arduino Interfacing

To get started with the HC-SR04, you'll need to familiarize yourself with its four pins: VCC, Trig, Echo, and GND. VCC and GND provide the power supply, while the Trig pin sends out ultrasonic pulses, and the Echo pin records the reflected signals.

Setting Up the Hardware

The first step involves connecting your HC-SR04 ultrasonic sensor to your Arduino Uno board. Connect the VCC and GND pins of the sensor to 5V and GND on the Arduino board, respectively. The Trig pin should be connected to digital pin 12, and the Echo pin to digital pin 13 on the Arduino.

an image of the cover of easy way to code ultrasonics for arduino robots
an image of the cover of easy way to code ultrasonics for arduino robots
Cool Arduino Ultrasonic Transducer HC-SR04 Distance Measurement With Tinkercad
Cool Arduino Ultrasonic Transducer HC-SR04 Distance Measurement With Tinkercad
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials

Once the hardware setup is complete, we can proceed to programming the Arduino to read distance data from the HC-SR04.

Calculating Distance with the HC-SR04

The key to calculating the distance lies in determining the time it takes for the echo to return. Utilizing the speed of sound (around 0.0343 cm/µs) and the duration of the echo, we can compute the distance to target objects.

AD

In your Arduino code, incorporate functions to trigger the sensor and measure the pulse width of the echo. Multiply the pulse width by the speed of sound and divide the result by 2 (to account for the distance covered by the outbound and inbound pulses). The resulting value will give you the distance in centimeters.

Ultrasonic Sensor HC-SR04 and Arduino - Complete Guide
Ultrasonic Sensor HC-SR04 and Arduino - Complete Guide
Waterproof Ultrasonic Distance Sensors with Arduino
Waterproof Ultrasonic Distance Sensors with Arduino
Arduino - Ultrasonic Sensor | Arduino Tutorial
Arduino - Ultrasonic Sensor | Arduino Tutorial
Ultrasonic Sensor Without Arduino using 555 timer » Hackatronic
Ultrasonic Sensor Without Arduino using 555 timer » Hackatronic
the electronic components are connected to each other
the electronic components are connected to each other
HC-SR04 Ultrasonic Sensor Pinout
HC-SR04 Ultrasonic Sensor Pinout

Building a Distance Monitoring Circuit

Now that we've covered the basics of the HC-SR04, let's construct a simple Arduino circuit that monitors the distance to a target object and displays the data on a computer screen using the Serial Monitor function in the Arduino IDE.

Again, ensure the HC-SR04 is correctly connected to the Arduino Uno. For this project, we won't be using any additional components. The Arduino board itself is capable of handling communication with the HC-SR04 and displaying the output data.

AD

Programming the Distance Monitoring Circuit

In the Arduino IDE, begin by defining the Trig and Echo pins and initializing the serial communication. Create a function named 'ultrasonic_distance()' to measure the distance to a target object and return the value in centimeters. In the main loop function, call this function and print the distance value to the Serial Monitor.

HC-SR04 Ultrasonic Distance Sensor with Arduino | DroneBot Workshop
HC-SR04 Ultrasonic Distance Sensor with Arduino | DroneBot Workshop
THIRD EYE FOR THE BLIND USING ARDUINO NANO AND ULTRASONIC SENSORS
THIRD EYE FOR THE BLIND USING ARDUINO NANO AND ULTRASONIC SENSORS
5 Arduino Ultrasonic Sensor Projects to Build
5 Arduino Ultrasonic Sensor Projects to Build
16×2 LCD, Servo Motor with Arduino and Ultrasonic Sensor
16×2 LCD, Servo Motor with Arduino and Ultrasonic Sensor
Ultrasonic HC-SR04 Sensor Interfacing Arduino - Distance Measurement
Ultrasonic HC-SR04 Sensor Interfacing Arduino - Distance Measurement
Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino Tutorial
Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino Tutorial
Simple Project With the Ultrasonic Sensor (HC-SR04) +LED -Arduino Tutoriel-
Simple Project With the Ultrasonic Sensor (HC-SR04) +LED -Arduino Tutoriel-
an electronic device with two speakers attached to it and wires running through the back ground
an electronic device with two speakers attached to it and wires running through the back ground
an instruction manual for using the simple ultrasonic radar with color display and lcd
an instruction manual for using the simple ultrasonic radar with color display and lcd
Distance Measurement Using Arduino and Ultrasonic Sensor
Distance Measurement Using Arduino and Ultrasonic Sensor
Arduino - Ultrasonic Sensor - LED | Arduino Tutorial
Arduino - Ultrasonic Sensor - LED | Arduino Tutorial

To gain more insight, consider adding some conditional statements to your code. For example, you could make your Arduino display an alert on the Serial Monitor when the distance to the target object falls below a specific threshold.

After mastering the basics of the Arduino HC-SR04 ultrasonic sensor setup and programming, you can explore more complex applications, such as obstacle avoidance systems, autonomous vehicles, or even advanced robotics.

AD
AD