To detect when a car is in a parking lot, there are some sensors available; in this case, we will use a presence sensor like this:
In some parking lots, there is a gate that goes up when the driver arrives in front of it and they press the button and receive their ticket; in others, they have an identity card. Here is a common parking lot:
This is a prototype of the entrance to the parking lot:
Now we will make a sketch that we can use with Arduino; it detects when the car gets close to the sensor. We use the digitalread() instruction to detect a digital signal; this sensor activates with a signal in low, as shown in the following code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
//variables
int cont = 0;
int reading;
void setup()
{
Serial.begin(9600);
lcd.init();
pinMode(13,OUTPUT);
pinMode(2,INPUT_PULLUP);
// Print a message to the LCD.
lcd.backlight();
lcd.print("Counting Cars");
lcd.setCursor(0,1);
lcd.print("Counting = 0 ");
}
void loop()
{
lectura = digitalRead(2);
if (reading == LOW){
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
cont = cont + 1;
lcd.setCursor(0,1);
lcd.print("Counting = ");
lcd.print(cont);
Serial.print("Counting = ");
Serial.println(cont);
// this condition counts until 21 cars after that the count returns to zero
if (cont == 21){
cont = 0;
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Counting = ");
lcd.print(cont);
Serial.print("Counting = ");
Serial.println(cont);
}
}
}
The final connections look like this:
This information of the total number can be displayed on a big screen or we implement storing the number of cars or spaces in a database; we will discuss this in the following sections.