Security system using Arduino and Ultrasonic sensor
Arduino Sketch for the same project is given below:
//this code is created by Yogesh Bawane for my channel ImpulseTech.
//you will find arduino tutorial on this channel https://www.youtube.com/channel/UCHCzBGgDMjT3fOWCv1ruIdA
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;
int buzzer=5;// Echo Pin of Ultrasonic Sensor
void setup() {
Serial.begin(9600); // Starting Serial Terminal
pinMode(buzzer,OUTPUT);
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
if(cm<10)
{
digitalWrite(buzzer,HIGH);
}
else
{
digitalWrite(buzzer,LOW);
}
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
Comments
Post a Comment
If you any query please comment