yaeseonggledel
Published

Ultra Sonic sensor (SR04)

I learned about Ultra Sonic sensor in fab lab academy

BeginnerFull instructions provided980
Ultra Sonic sensor (SR04)

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

measure distance by ultrasonic sensor

measure distance by ultrasonic sensor

Code

distance of object

Arduino
It allows me to measure the distance.
#include <LiquidCrystal.h>
 
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
 
void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
 
void loop() {
  ultra();
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.print("UltraSonic");
 
  lcd.setCursor(0,1);
  lcd.print("distance : ");
  lcd.print(distance);
  lcd.print("cm");
  delay(200);
 
   ultra();
  if (distance <= 10)  {
    digitalWrite(7, HIGH);
    delay(1000);
     digitalWrite(7, LOW);
    delay(1000);   }
  digitalWrite(7, LOW);
}
 
void ultra(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
 
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
 
// Calculating the distance
distance= duration*0.034/2;
}

Credits

yaeseong

yaeseong

7 projects • 7 followers
Korean High School Student who wants to expand his knowledge of the World
gledel

gledel

100 projects • 84 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"

Comments