HiHiHiHiiHiiIiH
Published

How to Use the HC-SR04 Ultrasonic Sensor

In this tutorial we will learn how to use the HC-SR04.

BeginnerFull instructions provided980
How to Use the HC-SR04 Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SparkFun F-M Wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Look below.

Code

CODE

C/C++
The code For sensor
const int trigPin = 9;
const int echoPin = 10;

float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}

Credits

HiHiHiHiiHiiIiH
0 projects • 3 followers

Comments