walid abdelazeem
Published © GPL3+

Car Parking system

Car Parking system used Ultrasonic Distance Sensor

AdvancedFull instructions provided1 hour1,296
Car Parking system

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long

Story

Read more

Schematics

Ultrasonic Distance Sensor

Car Parking system - Ultrasonic Distance Sensor

Code

Ultrasonic-Distance-Sensor---HC-SR04.ino

Arduino
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
 #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
 int led_green = 13;
 int led_yello = 12;
 int led_red = 11;
 // defines variables
 long duration; // variable for the duration of sound wave travel
 int distance; // variable for the distance measurement
 void setup() 
   {
     pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
     pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
     Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
     Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
     Serial.println("with Arduino UNO R3");
     pinMode(led_green, OUTPUT);
     pinMode(led_yello, OUTPUT);
     pinMode(led_red, OUTPUT);
 }
 
 void loop() 
 {
   // Clears the trigPin condition
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   // Reads the echoPin, returns the sound wave travel time in microseconds
   duration = pulseIn(echoPin, HIGH);
   // Calculating the distance
   distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
   // Displays the distance on the Serial Monitor
   Serial.print("Distance: ");
   Serial.print(distance);
    if(distance > 8)
    {
       digitalWrite(led_green, HIGH); // turn ON Arduino's LED
       digitalWrite(led_yello, LOW); // turn ON Arduino's LED
       digitalWrite(led_red, LOW); // turn ON Arduino's LED
    }
    else  if(distance < 8)
   {
       digitalWrite(led_green, LOW); // turn ON Arduino's LED
       digitalWrite(led_yello, HIGH); // turn ON Arduino's LED
       digitalWrite(led_red, LOW); // turn ON Arduino's LED
       if(distance < 4)
              {
                 digitalWrite(led_green, LOW); // turn ON Arduino's LED
                 digitalWrite(led_yello,LOW ); // turn ON Arduino's LED
                 digitalWrite(led_red, HIGH); // turn ON Arduino's LED
              }
  }

   Serial.println(" cm");
 }

Credits

walid abdelazeem
6 projects • 6 followers

Comments