Get Started with the HC-SR04 Ultrasonic Sensor using Arduino

13 May 2023 Balmiki Mandal 0 µC - µP

Getting Started With the HC-SR04 Ultrasonic Sensor Using Arduino

If you're looking to get started with robotics or robotics sensing, the HC-SR04 ultrasonic sensor is a great place to start! It's an easy-to-use and inexpensive sensor that can detect objects from 2 cm to 400 cm away. Perfect for robotic projects that need proximity sensing, like obstacle avoidance.

What Is the HC-SR04 Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor is a small device which emits an ultrasonic pulse and measures the time it takes to echo back. This time is used to measure the distance of objects from the sensor. The HC-SR04 consists of two transducers, one to emit ultrasonic pulses, and another to receive them. It works on the principle of the time of flight (TOF).

How Does the HC-SR04 Ultrasonic Sensor Work?

The HC-SR04 ultrasonic sensor consists of a transmitter and receiver. When triggered, the transmitter emits an ultrasonic pulse, which then reflects off of nearby objects and is received by the receiver. The time difference between when the signal was transmitted and when it was received is used to calculate the distance to the object.

The HC-SR04 is powered by 5V DC and communicates with the Arduino board through digital pins. These pins are used for Trigger and Echo, where trigger sends out an 8-cycle burst of ultrasonic sound, and echo receives the return pulse. The output from the Echo pin can be used to measure the distance from the ultrasonic sensor.

How to Use the HC-SR04 Ultrasonic Sensor With Arduino?

Using the HC-SR04 ultrasonic sensor with an Arduino is quite simple. First, you'll need to connect the VCC and GND pins of the sensor to the 5V and GND pins of the Arduino. Then, connect the Trig and Echo pins of the sensor to the digital pins of the Arduino, respectively. Make sure your connections are correct before continuing.

Once the hardware setup is complete, you'll need to program the Arduino to read the distance data from the HC-SR04. To do this, you'll need to write a function that will send a trigger signal to the sensor's Trig pin, wait for the Echo pin to respond, then calculate the distance based on the duration of the pulse. You can also use the ready-made libraries available online for simplifying the programming process.

With the HC-SR04 ultrasonic sensor and the Arduino, you can easily and cheaply build a robot that can detect and avoid obstacles. So go ahead and get started with your robotics project today!

Here is a sample code that can be used to read data from an HC-SR04 ultrasonic sensor using Arduino:

const int triggerPin = 7;  // Define trigger pin
const int echoPin = 6;     // Define echo pin

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

  // Initialize trigger and echo pins
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Send 10us pulse to trigger pin
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Read echo pin and calculate distance
  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;

  // Print distance to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Wait for 500 milliseconds
  delay(500);
}

In this code, we are using the HC-SR04 ultrasonic sensor to measure distance. The triggerPin variable is set to the digital pin 7, which is used to send a 10us pulse to the sensor. The echoPin variable is set to the digital pin 6, which is used to read the duration of the returned pulse.

In the setup() function, we initialize the serial communication using the Serial.begin() function. We also initialize the trigger and echo pins using the pinMode() function.

In the loop() function, we send a 10us pulse to the trigger pin using the digitalWrite() and delayMicroseconds() functions. We then read the duration of the returned pulse using the pulseIn() function and calculate the distance using the formula distance = duration * 0.034 / 2, where 0.034 is the speed of sound in centimeters per microsecond and we divide the result by 2 to account for the round-trip time of the ultrasonic wave.

We then print the distance to the serial monitor using the Serial.print() and Serial.println() functions.

Finally, we wait for 500 milliseconds using the delay() function before starting the loop again. This delay is added to prevent the Arduino from reading the sensor too frequently and to ensure that the serial monitor has enough time to display the sensor readings.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.