Programming the Ultrasonic Sensor with Arduino

What is an Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor is a popular distance sensor commonly used with Arduino and other microcontroller platforms. It’s used to measure distances between the sensor and an object by emitting ultrasonic waves and calculating the time it takes for the waves to bounce back.

Here’s a step-by-step guide on how to use an HC-SR04 sensor with Arduino:

Components you’ll need:

  1. Arduino.
  2. HC-SR04 ultrasonic sensor.
  3. Jumper wires.
  4. Breadboard.

Connections:

  1. Connect the VCC pin of the HC-SR04 to the 5V output on your Arduino.
  2. Connect the GND (ground) pin of the HC-SR04 to the GND (ground) on your Arduino.
  3. Connect the TRIG (trigger) pin of the HC-SR04 to a digital pin on your Arduino (e.g., D12).
  4. Connect the ECHO pin of the HC-SR04 to another digital pin on your Arduino (e.g., D11).
HC-SR04 Ultrasonic Distance Sensor circuit diagram

The Code:

Here’s an example Arduino sketch to read distance measurements using the HC-SR04 sensor:

// Define the pins for the trigger and echo
const int trigPin = 12;
const int echoPin = 11;

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

  // Define the trigger pin as an OUTPUT
  pinMode(trigPin, OUTPUT);
  // Define the echo pin as an INPUT
  pinMode(echoPin, INPUT);
}

void loop() {
  // Trigger a pulse by setting the trigger pin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse on the echo pin
  long duration = pulseIn(echoPin, HIGH);

  // Convert the duration into distance in centimeters
  // (speed of sound is approximately 343 m/s)
  float distanceCM = (duration / 2.0) * 0.0343;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distanceCM);
  Serial.println(" cm");

  // Wait a moment before taking the next measurement
  delay(1000);
}

In this code:

  • We define the trigger and echo pins and set them as OUTPUT and INPUT, respectively.
  • In the loop() function, we trigger the sensor by setting the trigger pin HIGH for 10 microseconds and then LOW.
  • We measure the duration of the pulse on the echo pin using pulseIn().
  • We convert the duration into distance in centimeters using the speed of sound (approximately 343 meters per second).
  • Finally, we print the distance to the Serial Monitor and wait for a second before taking the next measurement.

Upload this sketch to your Arduino, open the Serial Monitor, and you should see distance measurements displayed in centimeters.

Conclusion

The HC-SR04 ultrasonic sensor is a versatile and cost-effective tool for measuring distances with precision when interfaced with an Arduino. The combination of the HC-SR04 and Arduino opens up a world of possibilities for innovation and automation in the realm of electronics and robotics.

Happy Tinkering!

Recommendations:

The Elegoo Super Starter Kit

If you don’t already own any Arduino hardware, we highly recommend this kit as it has everything you need to start programming with Arduino. You can find out more about this kit, including a list of its components here: Elegoo Super Starter Kit

You can find this kit on Amazon here: Elegoo Super Starter Kit

The 0.96-inch Mini-OLED Display

We highly recommend this mini-OLED bundle of five 0.96-inch OLED displays. We have bought these before and they all worked perfectly. You can read more about the mini-OLED here: Mini-OLED

You can find this bundle on Amazon here: OLED Displays

Elegoo Nano (Arduino Compatible)

We have bought these Nano boards many times and can highly recommend them. There are three Nano boards in this pack making them a total bargain for everyone.

You can find this pack on Amazon here: Arduino Nano

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights