Arduino + Ultrasonic Sensor HC-SR04 Tutorial

 Yo fam! πŸ‘‹ Today we’re diving into the HC-SR04 ultrasonic sensor and hooking it up with an Arduino UNO. This sensor is like echolocation for bats it measures distance using sound waves. Let’s make it happen! 

Stuff You’ll Need

  • Arduino UNO

  • HC-SR04 Ultrasonic Sensor

  • Breadboard (optional, but neat)

  • Jumper wires (male-to-male)

  • USB cable to plug into your laptop

Wiring Guide

Check the wiring in the diagram πŸ‘†

  • VCC → 5V (red wire)

  • GND → GND (black wire)

  • Trig → Pin 8 (green wire)

  • Echo → Pin 9 (green wire)

Super simple: just 4 wires and you’re good πŸ’―

this ur code guys :

const int TRIGPIN = 8;

const int ECHOPIN = 9;

long timer;

int distance;


void setup() {

  Serial.begin(9600);        // open serial monitor

  pinMode(ECHOPIN, INPUT);   // echo pin as input

  pinMode(TRIGPIN, OUTPUT);  // trig pin as output

}


void loop() {

  // send trigger signal

  digitalWrite(TRIGPIN, LOW);

  delayMicroseconds(2);

  digitalWrite(TRIGPIN, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIGPIN, LOW);


  // read the echo

  timer = pulseIn(ECHOPIN, HIGH);


  // convert time into distance (cm)

  distance = timer / 58;


  // show on serial monitor

  Serial.print("Distance = ");

  Serial.print(distance);

  Serial.println(" cm");


  delay(1000); // wait 1s before next measurement

}


What’s Happening

  • Trig pin sends a tiny sound pulse 🎢

  • Echo pin waits for the bounce back

  • Arduino calculates how long it took → converts into distance in cm πŸ“

  • Results show up on the Serial Monitor

So yeah, it’s basically sonar but mini version 😎

Wrap-Up

And that’s it, squad! You just built a distance measuring tool using Arduino + ultrasonic sensor. You can use this project for:

  • A parking sensor for your car πŸš—

  • A smart trash bin that opens when you get close πŸ—‘️

  • Or even a robot that avoids obstacles πŸ€–

Play around, tweak the delay, or even add LEDs/buzzer to make it more interactive πŸ”₯

Komentar

Postingan populer dari blog ini

How to Make Arduino LCD Show Lyrics in wokwi

Arduino Buzzer Music Tutorial (Play Do-Re-Mi with Arduino)