Skip to main content

Ultrasonic sensor using Arduino
























Code
/* 
   CAR PARKING SENSOR
   https://electronicassit.blogspot.in
   ARJUN ARANGIL
   9444868542
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 5;
int const echoPin = 6;
int const buzzPin = 12;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 40 meter and more than 30 
if (distance <= 20 && distance >= 15) 
{
// Buzz
digitalWrite(buzzPin, HIGH);
delay(100);
digitalWrite(buzzPin, LOW);
delay(300);
}
// if distance less than 15 meter and more than 10 
if (distance <= 15 && distance >= 10) 
{
// Buzz
digitalWrite(buzzPin, HIGH);
delay(100);
digitalWrite(buzzPin, LOW);
delay(200);
}
// if distance less than 10 meter and more than 5
if (distance <= 10 && distance >= 5) 
{
// Buzz
digitalWrite(buzzPin, HIGH);
delay(100);
digitalWrite(buzzPin, LOW);
delay(100);
// if distance less than 5 meter and more than 0
if (distance <= 5 && distance >= 0) 
{
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms
delay(10);
}

Comments

Popular posts from this blog

PIR Senesor / Motion Sensor using Arduino  Code /* PIR SENSOR / MOTION SENSOR https://electronicassist.blogspot.in/ ARJUN ARANGIL 9444868542 */ int ledPin   = 13;                 // choose the pin for the LED int inputPin = 2;                  // choose the input pin (for PIR sensor) int leda     = 12;                 // choose the pin for the LED int ledb     = 11;                 // choose the pin for the LED int pirState = LOW;                // we start, assuming no motion detected int val      = 0;                  // variable for reading the pin status void setup()  {   pinMode(ledPin, OUTPUT);        ...
#LESSON 02-LED CHASER   CODE /* LED CHASER https://electronicassit.blogspot.in/ ARJUN ARANGIL 9444868542 */      int i,j; void setup() {   i=25; // blinking time   j=60; //blinking time   pinMode(2, OUTPUT);   pinMode(3, OUTPUT);   pinMode(4, OUTPUT);   pinMode(5, OUTPUT);   pinMode(6, OUTPUT);   pinMode(7, OUTPUT);   pinMode(8, OUTPUT);   pinMode(9, OUTPUT);   pinMode(10, OUTPUT);   pinMode(11, OUTPUT);   pinMode(12, OUTPUT);   pinMode(13, OUTPUT);  } void loop(){      digitalWrite(4, HIGH);   delay(i);   digitalWrite(4, LOW);   delay(j);   digitalWrite(4, HIGH);   delay(i);   digitalWrite(4, LOW);   delay(j);   digitalWrite(8, HIGH);   delay(i);   digitalWrite(8, LOW);   delay(j);   digitalWrite(8, HIGH);   delay(i);...