Arduino Ultrasonic Sensor Project for Beginners: Measure Distance with HC-SR04

Arduino Ultrasonic Sensor Project for Beginners: Measure Distance with HC-SR04

Project Overview

This beginner-friendly project demonstrates how to use an HC-SR04 ultrasonic sensor with Arduino to measure distance accurately. The HC-SR04 is a popular sensor for distance measurement, making it useful in obstacle detection and range-finding projects.

Project Goals for Arduino Ultrasonic Sensor

  • Learn how to connect the HC-SR04 ultrasonic sensor to Arduino for distance measurement.
  • Measure and display the distance in centimeters using the Arduino Serial Monitor.
  • Understand how to use the NewPing library for improved accuracy and performance.

Required Components for Arduino Ultrasonic Sensor Project

Here’s a list of components needed to build this project:

ComponentDescriptionLink
Arduino UnoMain microcontroller boardBuy on Amazon
HC-SR04 Ultrasonic SensorMeasures distance using sound wavesCheck Availability
Jumper WiresConnects components to ArduinoBuy on Amazon
BreadboardFor prototyping connectionsBuy on Amazon

What is the HC-SR04 Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor is a popular distance sensor that uses ultrasonic waves to measure the distance between the sensor and an object. It emits sound waves from the trigger pin and listens for their reflection using the echo pin. The time taken for the sound to return is used to calculate the distance.

HC-SR04 Ultrasonic Sensor Pinout Overview

PinDescription
VCCPower supply (5V)
GNDGround connection
TriggerTrigger pin, connected to Arduino digital pin
EchoEcho pin, connected to Arduino digital pin

Circuit Connection for HC-SR04 with Arduino

Follow these connections to set up the HC-SR04 ultrasonic sensor with Arduino:

ComponentArduino PinDetails
Sensor VCC5VConnect to 5V power supply
Sensor GNDGNDGround connection
Sensor TriggerD3Trigger pin for sending sound waves
Sensor EchoD2Echo pin for receiving sound waves

How the Circuit Works

The HC-SR04 ultrasonic sensor sends a pulse from the trigger pin and waits for it to reflect back to the echo pin. The time taken for the pulse to return is used to calculate the distance in centimeters. The NewPing library is used to improve the accuracy and efficiency of distance measurement.

Arduino Code for Ultrasonic Sensor

This code measures distance using the HC-SR04 ultrasonic sensor and displays the result in the Serial Monitor. It uses the NewPing library for better performance. Copy and upload this code to your Arduino Uno.


#include 

#define TRIGGER_PIN  3 
#define ECHO_PIN     2  
#define MAX_DISTANCE 400 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

float tempval1;
float tempval2;
int finalval;

void setup() {
  Serial.begin(57600); 
}

void loop() {
  delay(20);                     
  Serial.print("Ping: ");
  
  int iterations = 10;
  tempval1 = ((sonar.ping_median(iterations) / 2) * 0.0343);
  
  if(tempval1 - tempval2 > 60 || tempval1 - tempval2 < -60) {
    tempval2 = (tempval1 * 0.02) + (tempval2 * 0.98);
  } else {
    tempval2 = (tempval1 * 0.4) + (tempval2 * 0.6);
  }
  
  finalval = tempval2;
  
  Serial.print(finalval);
  Serial.println(" cm");
}

Steps to Upload Arduino Code

  • Connect your Arduino to the computer using a USB cable.
  • Open the Arduino IDE and paste the code into a new sketch.
  • Install the NewPing library from the Library Manager.
  • Select the correct board (e.g., Arduino Uno) and port from the "Tools" menu.
  • Click the "Upload" button to transfer the code to the Arduino.

Check Output for HC-SR04 Ultrasonic Sensor

  • Open the Serial Monitor (Ctrl + Shift + M) after uploading the code.
  • The measured distance will be displayed in the Serial Monitor in centimeters.
  • Place an object in front of the sensor to observe changes in the distance readings.

Troubleshooting Tips for HC-SR04 Sensor

  • No Readings in Serial Monitor: Check the wiring, especially the connections for the trigger and echo pins.
  • Incorrect Distance Values: Ensure that the sensor is properly aligned with the object being measured.
  • Inconsistent Readings: Verify the power supply and avoid electrical noise in the circuit.

Suggestions for Beginners

Start by testing the HC-SR04 ultrasonic sensor to understand its behavior with different distances. Once you’re comfortable with the readings, try integrating it into more advanced obstacle detection and range-finding projects.

Recommended Book for Learning Arduino

Arduino Programming for Absolute Beginners - This book provides easy, step-by-step instructions, making it ideal for beginners who want to learn Arduino programming.

For more Arduino tutorials, visit MechatronicsLab.net, where you’ll find resources on Arduino, ESP8266, ESP32, and Raspberry Pi.

No comments

Theme images by Dizzo. Powered by Blogger.