ultrasonic sensor arduino png

Ultrasonic Sensor Arduino PNG Guide

By Rachel Garcia
AD

Ever since the advent of Maker Culture, the Arduino platform has empowered innovators worldwide to bring their ideas to life. A popular component used in Arduino projects is the ultrasonic sensor, a key tool for measuring distance and detecting objects. In this article, we'll explore the fascinating world of ultrasonic sensors, their implementation with Arduino, and delve into a comprehensive example using a popular Arduino project board and sensor: the Arduino Uno and HC-SR04.

Let's kickstart our ultrasonic sensor journey by understanding what makes these sensors so versatile and why they're an excellent pick for Arduino projects.

The Basics of Ultrasonic Sensors

Ultrasonic sensors operate on the principle of emitting sound waves and measuring the time it takes for the echo to return. This time is then converted into distance using the speed of sound. Unlike infrared sensors, ultrasonic sensors can measure greater distances and are less affected by ambient light conditions.

AD
an electronic device with two speakers attached to it
an electronic device with two speakers attached to it
Discover Easy Ultrasonic Sensing with Arduino UNO R4 🚀
Discover Easy Ultrasonic Sensing with Arduino UNO R4 🚀
Ultrasonic Sensor HC-SR04 and Arduino - Complete Guide
Ultrasonic Sensor HC-SR04 and Arduino - Complete Guide

Ultrasonic sensors are broadly categorized into two types: Ping (single-transducer) and Echo (two-transducer) sensors. The HC-SR04, for instance, is an Echo sensor, which we'll be focusing on in this guide.

Ultrasonic Sensoruo Pin Configuration

Typical ultrasonic sensors like the HC-SR04 consist of four pins: VCC, GND, Trigger, and Echo. Here's a breakdown:

Ultrasonic Sensor Arduino Interfacing
Ultrasonic Sensor Arduino Interfacing
Ultrasonic Sensor Work, Used, Limitations and FAQs - Renke
Ultrasonic Sensor Work, Used, Limitations and FAQs - Renke
How to Use an Ultrasonic Sensor
How to Use an Ultrasonic Sensor
  • VCC: Provides power to the sensor (5V for HC-SR04).
  • GND: Ground connection for the sensor.
  • Trigger: Sends a 10µs trigger pulse to generate an ultrasonic burst.
  • Echo: Receives the reflected signal. The pulse width on this pin indicates the distance in centimeters or inches.

Arduino Libraries and Modules

To work with ultrasonic sensors on Arduino, we'll be utilizing the NewPing library. This library simplifies the complex calculations and allows us to focus on our project's functionality.

First, include the NewPing library in your Arduino IDE's Library Manager. Then, you'll need to download the NewPing library from the Arduino Playground and install it.

AD

Setting Up the HC-SR04 Ultrasonic Sensor with Arduino Uno

Now that we understand the basics let's connect an HC-SR04 ultrasonic sensor to an Arduino Uno and create a simple distance measurement sketch.

Ultrasonic Sensor HC-SR04
Ultrasonic Sensor HC-SR04
an electronic device with two speakers attached to it
an electronic device with two speakers attached to it
the electronic components are connected to each other
the electronic components are connected to each other
Interfacing Ultrasonic Sensor HC-SR04 with Arduino
Interfacing Ultrasonic Sensor HC-SR04 with Arduino
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Arduino Distance Sensor using the HC-SR04
Arduino Distance Sensor using the HC-SR04

The Arduino Uno's pin layout is as follows: GND – Pin 9, 5V – Pin 7, and digital pins range from 2 to 13. Here's how to wire the HC-SR04:

HC-SR04 Pin Arduino Uno Pin
GND 9
VCC 7
Trigger 12
Echo 11

Arduino Code: Basic Distance Measurement

Let's write a simple Arduino sketch to measure and display the distance data on the Serial Monitor:

AD
#include <NewPing.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200 // measurement maximum in centimeters

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
  Serial.println("Starting ultrasonic sensor test!");
}

void loop() {
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int distance = sonar.ping_cm(); // Send ping, get distance in centimeters
  if (distance > 0) {
    Serial.print("Distance (cm): ");
    Serial.println(distance);
  }
}

Monitoring Distance Data

Upload the code to your Arduino Uno. Open the Serial Monitor (Ctrl+Shift+M or Cmd+Shift+M for Mac users), set the baud rate to 9600, and observe the distance measurements as the sensor detects objects in its path.

Module de capteur ultrasonique HC-SR04, plage de Distance 2cm-400cm pour Robot Arduino, voiture
Module de capteur ultrasonique HC-SR04, plage de Distance 2cm-400cm pour Robot Arduino, voiture
Arduino - Ultrasonic Sensor | Arduino Tutorial
Arduino - Ultrasonic Sensor | Arduino Tutorial
HC-SR04 Ultrasonic Module Distance Sensor 2-400cm for Arduino Smart Car Robot Obstacle Avoidance
HC-SR04 Ultrasonic Module Distance Sensor 2-400cm for Arduino Smart Car Robot Obstacle Avoidance
Ultrasonic Range Sensor w/ Cable
Ultrasonic Range Sensor w/ Cable
Arduino Sensor Module gyroscope touch soil ultrasonic IR microcontroller sensor interface plc industrial component isometric Isolated vector illustration cartoon
Arduino Sensor Module gyroscope touch soil ultrasonic IR microcontroller sensor interface plc industrial component isometric Isolated vector illustration cartoon
an electronic device connected to a breadboard
an electronic device connected to a breadboard
UltraSonic Sensor With Arduino
UltraSonic Sensor With Arduino
how to make obstacle avoiding robot Arduino
how to make obstacle avoiding robot Arduino
How HC-SR04 Ultrasonic Sensor Works & How to Interface It With Arduino
How HC-SR04 Ultrasonic Sensor Works & How to Interface It With Arduino
Como conectar o Sensor UltrassĂ´nico HC-SR04 ao Arduino
Como conectar o Sensor UltrassĂ´nico HC-SR04 ao Arduino
Parallax PING Ultrasonic Sensor
Parallax PING Ultrasonic Sensor

That's it! You've successfully integrated an ultrasonic sensor with your Arduino board and written a simple yet powerful distance measurement sketch. From fear-avoiding robots to automated parking systems, ultrasonic sensors form the backbone of numerous innovative projects. Now that you've mastered the basics, the sky's the limit for your next big idea!

AD