Distance sensor with Arduino
DISTANCE SENSOR
In this Blog, we gonna see what is Distance sensor. How it works. Mainly Distance sensor we use for measuring the distance from the sensor to object /obstacles. Distance Sensor (Ultrasonic Sensor) released some ultrasonic waves when it bounces back after moving some path That is the Position of our obstacles. Now to getting distance, We have to use formula -
D = S*T
S = Speed Of Sound In The Medium
T = Time That Sound Travel
Speed of sound in the Air is 340m/s.
We Get time also (see that is in code)
After Getting Two value we can get Distance also.
Components:
- Arduino
- Distance sensor
- Jumper wire
- Breadboard
Code:
const int trigPin = 10;
const int echoPin = 13;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
}
Comments
Post a Comment