MisterBotBreak
Published

How to Use an Ultrasonic Sensor

I will show you how to use an ultrasonic sensor.

BeginnerProtip1 hour12,463
How to Use an Ultrasonic Sensor

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

HC-SR04

Arduino
The distance is viewable on the serial monitor.
int trig = 12; 
int echo = 11; 
long lecture_echo; 
long cm;
void setup() 
{ 
  pinMode(trig, OUTPUT); 
  digitalWrite(trig, LOW); 
  pinMode(echo, INPUT); 
  Serial.begin(9600); 
}
void loop() 
{ 
  digitalWrite(trig, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trig, LOW); 
  lecture_echo = pulseIn(echo, HIGH); 
  cm = lecture_echo / 58; 
  Serial.print("Distance in cm : "); 
  Serial.println(cm); 
  delay(1000); 
}

Credits

MisterBotBreak

MisterBotBreak

40 projects • 81 followers
I love electronic and I make explanatory project on how to control sensors.

Comments