Monday 9 January 2017

Car Parking using Ultrasonic sensor and Arduino



Hello everyone!
Recently we made a project 'Car Parking' using Arduino Uno. Check out the video we made of the project.




Components required :
Arduino Uno
Ultrasonic sensor HC-SR04
Buzzer FC-49
LED- Red, Yellow, Green/Blue
Male to male connectors
Breadboard
10k ohm Potentiometer
LCD Display 16x2

First of all you should download and install Arduino IDE.To code the program you should have basic knowledge of C language.

Here is the code we have used for our project:


#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int trig=12;
int echo=13;
int buzzer=11;
int ledb=8;
int ledr=9;
int ledy=10;
void setup() {
 // put your setup code here, to run once:
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(buzzer,OUTPUT);
digitalWrite(ledb,HIGH);
digitalWrite(ledr,LOW);
digitalWrite(ledy,LOW);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
}

void loop()
{
long duration,distance;
  // put your main code here, to run repeatedly:
digitalWrite(trig,LOW);
delayMicroseconds(10);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
distance=(duration/58);

if(distance<10)
{
  Serial.print("DANGER ");
  Serial.print(distance);
  Serial.println(" cm");
  digitalWrite(ledr,HIGH);
  digitalWrite(ledb,LOW);
  digitalWrite(ledy,LOW);
  digitalWrite(buzzer,HIGH);
  delay(33);
  digitalWrite(buzzer,LOW);
  delay(33);
  lcd.clear();
  lcd.print("DANGER ");
  lcd.print(distance);
  lcd.print(" cm ");
  delay(110);
}
else if((distance>=10)&&(distance<=20))
{
  Serial.print("SLOW DOWN ");
  Serial.print(distance);
  Serial.println(" cm");
  digitalWrite(ledy,HIGH);
  digitalWrite(ledb,LOW);
  digitalWrite(ledr,LOW);
  digitalWrite(buzzer,HIGH);
  delay(70);
  digitalWrite(buzzer,LOW);
  delay(70);
  lcd.clear();
  lcd.print("GO SLOW ");
  lcd.print(distance);
  lcd.print("cm ");
  delay(110);
}


else{
  Serial.print("OK ");
  Serial.print(distance);
  Serial.println(" cm");
  digitalWrite(ledb,HIGH);
  digitalWrite(ledr,LOW);
  digitalWrite(buzzer,LOW);
  digitalWrite(ledy,LOW);
  lcd.clear();
  lcd.print("OK ");
  lcd.print(distance);
  lcd.print(" cm ");
  delay(110);
}

 
}

First you have to include libraries of components you gonna use in your project. Here for LCD display we have used <LiquidCrystal.h> and then set the data type same as you do in C language. In void setup() you have tell the arduino, which pins are for O/P and which are for I/P by using command pinMode. digitalWrite are used as digital commands for LED to turn ON/OFF. Serial.begin is used to print required O/P on Serial Monitor. lcd.clear() is used to blank the LCD and lcd.begin(16,2) is used to start the LCD.
This code will be executed only once. The code you will put in void loop() will be executed continuously. To know the function of each command please visit Arduino website.
   Ultrasonic sensor Transmits a pulse and it is reflected back from the obstacle. The reflected pulse is received by receiver of the sensor and the time duration required by the pulse to come back to sensor is given to Arduino.
basically the formula to calculate distance is
Distance= (Velocity x Time)/2
(Velocity = 340 m/s)
So to display the distance in cm, we have formula
Distance=(duration)/58

Here we have used if-else loop. When the distance is less the 10 cm then red LED will glow and buzzer will start beeping at high rate and message will be displayed on LCD as Danger and corresponding distance. When the distance is between 10 to 20 cm then yellow LED will glow , buzzer will start beeping at low rate and on LCD message will be displayed as Go Slow and corresponding distance and when the distance is greater than 20 cm then blue LED will glow and buzzer will not beep; on LCD message will be displayed as OK and corresponding distance.

Now, lets build the circuit on breadboard.
Place the components as shown in the picture.(I have used two breadboards to show because of limited space on first breadboard. You will only need one breadboard.)



Lets start with LCD.

LCD Display 16x2 (Front)
LCD Display 16x2 (Back)




Connect PIN4 (RS) to digital PIN7 of Arduino
PIN6 (Enable) to digital PIN6
PIN11 (D4) to digital PIN5
PIN12 (D5) to digital PIN4
PIN13(D6)  to digital PIN3
PIN14(D7)  to digital PIN2
PIN2(VCC)  to   3.3V PIN
PIN1(VSS) to ground (GND)
PIN3(VEE) to Wiper PIN of Potentiometer

For Ultrasonic sensor



Ultrasonic HC-SR04



connect VCC(+5V) and GND
Conncet Trigger PIN to Digital PIN12 of Arduino
Echo PIN to Digital PIN13 of Arduino.

For Buzzer


Buzzer FC-49


Connect VCC(+5V) and GND
I/O PIN to digital PIN11 of Arduino

Connect LED,s anode to digital PINs of Arduino
Red LED - PIN9
Blue LED - PIN8
Yellow LED - PIN10

Connect Potentiometer



Connect VCC (+5V) and GND PINs
Connect Wiper PIN to LCD PIN3

This project is made my Sanul RaskarAniruddha TongeSaurabh Damle & Sanket Jadhav.










No comments:

Post a Comment