ARDUINO WITH ULTRASONIC SENSOR


In this tutorial you will get to know that how you can create an obstacle detection system .


Material requirements are given below :-
  • ULTRASONIC SENSOR
  • ARDUINO
  • GREEN LED
  • RED LED
  • BRAEDBOARD
  • JUMPER WIRES
Working
Whenever any object comes in front of the sensor red LED start blinking making you alert.
Otherwise green LED glows indicating no object in front.
In this project the sensor range I am showing is 5 cm whereas it can be change by editing the code.


CONNECTIONS
ARDUINO

GND          =     -ve of green , red LED and
                          GND of ultrasonic sensor
5V              =     VCC of ultrasonic sensor
2                =      +ve of red LED
3                =      +ve of green LED 
9                =      trig pin of ultrasonic sensor
10              =      echo pin of ultrasonic sensor


CODE OF THE PROJECT :-

const int trig = 9;
const int echo = 10;
const int green = 3;
const int red = 2;
// defines variables
long duration;
int distance;

void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
}

void loop() {
// Clears the trigPin
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);


duration = pulseIn(echo, HIGH);


distance= duration*0.034/2;

if(distance < 5){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(100);
  digitalWrite(green,LOW);
  digitalWrite(red,LOW);
  delay(100);
}
else{
   digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
}
}






Popular posts from this blog

ARDUINO WITH PCA9685

TELEGRAM WITH RASPBERRY PI