IoT Based Car Parking System Using Arduino

In this project we are going to construct a smart vehicle parking system that connects to internet to help a car driver or any vehicle owner to check if there is a vacant parking spot exist in a parking lot even before the driver reach the intended parking lot destination.

We will see:

  • What is IoT based parking system?
  • Block diagram of IoT parking system.
  • Circuit diagram for IoT parking system.
  • Program code for Arduino and ESP8266.
  • Setting-up Thingspeak account.
  • Prototype images.
  • How to operate the machine?
  • Output on Thingspeak.

What is an IoT Based Parking System?

An IoT based parking system is a vehicle parking management system to ease the search for a vacant parking spot in a parking lot through a smartphone. The system utilizes various sensors and microcontrollers with internet capability for detecting parked vehicles and to update the data in real-time on internet.

The proposed design:

As mentioned above, the proposed smart parking lot circuit will be equipped with several sensors, inexpensive microcontrollers and Wi-Fi module using which a car / any vehicle owner can check if there is a vacant space in a parking lot using his / her phone or tablet or even on computer.

The number of vacant spaces in the smart parking lot can be viewed from anywhere in the world using a URL link or the user can scan a QR code. The scanned / shared URL can be browsed on any web browser to know how many empty parking spot exist in real time.

IoT smart parking system for electric vehicle (EV) charging stations:

The proposed smart parking system is very useful in electric vehicle charging stations and this technology is going to a boon for those who are passing beside the charging stations equipped with this system. Now the motorists can see number of vacant chargers on their smartphone and plan their journey accordingly.

In conclusion, the main purpose of a smart vehicle parking system is to save time and reduce hassle for motorists to find a parking lot with a vacant parking spot; otherwise a driver may need to spend their time to find if there are any vacant parking spot left or should they move on to an another parking lot and this situation may put many motorists to mental stress especially those who are in an urgent circumstances.

Block Diagram:

block diagram of IoT based car parking system
block diagram of IoT based car parking system

The circuit we are going to build will be based on the above architecture. An inexpensive Arduino board is going to be the brain of the project.

 A 16 x 2 LCD is utilized for displaying the number of vacant spots locally (without internet). An I2C module is utilized for driving the LCD with just four wires so that GPIO pins can be saved for interfacing the sensors and other modules.

There are three ultrasonic sensors for detecting 3 cars / vehicles on the parking spot, we are using ultrasonic sensors instead of IR based sensors because if the parking lot is situated outdoors, infrared light from sunlight may interfere with IR sensors and may give incorrect detection of the vehicle, whereas ultrasonic sensor acts like a mini radar and environmental factors affecting its functionality is minimal.

Please note that we are constructing a scale down version of the real project; hence we are just using three sensors.   

An ESP8266 Wi-Fi module is used for internet connectivity which sends the parking lot’s data to a cloud server where general public can view the data in real time. A power supply module is utilized which provides 5V and 3.3V for Arduino, ultrasonic sensors and ESP8266 Wi-Fi Module.

The internet cloud service we are going to use is called “Thingspeak” where the parking lot’s data to be sent, stored and displayed in real time. This concludes the block diagram.

Circuit diagram for IoT based car park monitoring system:

IoT based car parking system
IoT based car parking system

The above illustrated schematic consists of commonly available and easy to find modules. The brain of the project is an Arduino board and you can use any Arduino board with ATmega328p microcontroller.

Power supply unit:

Power supply 5V/3.3V
Power supply 5V/3.3V

A power supply module takes 9V to 12V DC from a wall adapter and converts in to 5V and 3.3V outputs, the 5V output from power supply module is directly connected to 5V pin of Arduino and GND of power supply is connected to GND of Arduino.

Similarly 3.3V from the power supply unit is connected to 3.3V Vcc of ESP8266 (it operates strictly on 3.3V and 5V will kill the module), the ground of power supply is connected to ground of ESP8266.

Make sure you that have inserted the voltage select jumpers correctly.

Generic ESP8266 Wi-Fi module:

Generic ESP8266
Generic ESP8266

This project utilizes a generic ESP8266 Wi-Fi module for internet connectivity. The ESP8266 is actually a miniature microcontroller board and just like Arduino the ESP8266 need a program code to perform its intended function.

It uses UART protocol to communicate with Arduino board; the baud rate we are going to set for UART is 115200 bits per second.

We can witness its pin diagram on back side of the ESP8266 module:

Pin Diagram of ESP8266
Pin Diagram of ESP8266

Since this ESP8266 module does not support or have any USB port, to program this module we need a programmer kit using which we can upload the code to this module seamlessly, we will explore more about this in later part of this post.

Ultrasonic Sensor HC – SR04:

Ultrasonic sensor
Ultrasonic sensor

The sensor we are going to use for detecting a parked vehicle on its parking spot is called HC – SR04 which is an ultrasonic sensor module.

The ultrasonic sensor module generates ultrasonic sound at around 40 KHz, these sound waves are inaudible to human beings and propagate through air and if the ultrasonic sound wave hits an obstacle, it reflects back to sensor just like radars.

If a car or any vehicle is parked, the ultrasonic sound waves hit the parked vehicle and the sensor module detects the reflection and thus existence of a vehicle on a parking spot is detected.     

The ultrasonic sensor module has four pins, Vcc, GND, trigger and echo. The Vcc is connected to 5V supply and GND is connected to GND of the supply.  When we apply “HIGH” signal to trigger pin for 10 microseconds, the module generates ultrasonic sound from one of the transducers, when the sound wave hit back the other transducer, the echo pin gets “HIGH” and this signal is detected by Arduino.

The time taken between generating and detecting the sound wave is calculated and thus a parked vehicle is detected.

LCD display module 16 x 2:

I2C and LCD
I2C and LCD

In this project we are using a 16 x 2 LCD display for displaying parking lot’s data locally without the need for internet. The LCD is driven by an I2C adapter module to reduce the number of wires to four; otherwise you need to connect up to 16 wires to Arduino just to drive the display. If the LCD occupies most of the I/O pins, then there won’t be any pins left for the sensors.

The I2C module has 16 pins at the output and just four at the input: Vcc, GND, SDA and SCL. The SDA and SCL are I2C bus pins which are connected to A4 and A5 pins of Arduino respectively and it operates on 5V.

You can control the contrast of the display by adjusting the trim pot on the I2C adapter module. This concludes about the circuit diagram.

How to setup your Thingspeak account?

We are using a (free) cloud service called Thingspeak where we will send parking lot’s data to share it with public.

  • First you need to sign up for Thingspeak: Click here
  • Enter the credentials it asks for and create a new channel and do the following to your new channel:
  • Go to channel settings and enter the things as shown above and take note of your channel ID which we need to enter it in the program code.
  • Scroll down and click save to save the changes.
  • Now click on API keys tab and you will see your keys as illustrated below. API keys are responsible for writing and reading the data to your Thingspeak account.
  • Go to channel settings and enter the things as shown above and take note of your channel ID which we need to enter it in the program code.
  • Scroll down and click save to save the changes.

Now click on API keys tab and you will see your keys as illustrated below. API keys are responsible for writing and reading the data to your Thingspeak account.

  • Take note of your “write API key” which needs to be entered in the program code and read API key is not used in this project.
  • Now go to sharing tab and click on “share channel view with everyone”, this makes your channel visible to those who have the URL of “public view” page.
  • Click on public view tab and you will see an empty graph field and the URL of this page can be shared to the public.
  • Now on the public view tab we are going to setup a number widget where public can view the number of vacant spots on the parking lot, click on “add widget”.
  • A window will pop-up as illustrated below, choose numeric display and click next.
  • Now fill the fields as shown below and click on create.
  • Now you will see a new widget where number will be displayed once we send data. Close the “field chart” by clicking on ‘X’, the public just need to know the number of vacant parking spots and not the parking history.
  • Click on private view tab, you see an empty field chart; this is where you can view the history car parking history and this is not visible to public. This concludes on Thingspeak account setup.

Program code for Arduino:

Download LCD Library: Click here

//---------- Electronics-Project-Hub.com-----------------//
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int trig_1 = 2;
const int echo_1 = 3;
const int trig_2 = 4;
const int echo_2 = 5;
const int trig_3 = 6;
const int echo_3 = 7;
float distanceCM_1 = 0, resultCM_1 = 0;
float distanceCM_2 = 0, resultCM_2 = 0;
float distanceCM_3 = 0, resultCM_3 = 0;
long Time_1, Time_2, Time_3;
float car_1, car_2, car_3;
float Dist_1 = 8.0, Dist_2 = 8.0, Dist_3 = 8.0;
int total = 0, timer_cnt = 0;
void setup()
{
  mySerial.begin(115200);
  pinMode(trig_1, OUTPUT);
  pinMode(trig_2, OUTPUT);
  pinMode(trig_3, OUTPUT);
  pinMode(echo_1, INPUT);
  pinMode(echo_2, INPUT);
  pinMode(echo_3, INPUT);
  digitalWrite(trig_1, LOW);
  digitalWrite(trig_2, LOW);
  digitalWrite(trig_3, LOW);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("  IoT CAR PARK");
  lcd.setCursor(0, 1);
  lcd.print(" MONITOR SYSTEM");
  delay(2000);
  lcd.clear();
}

void loop()
{
  total = 0;
  car_1 = sensor_1();
  car_2 = sensor_2();
  car_3 = sensor_3();
  lcd.setCursor(0, 0);
  lcd.print("CAR1:");
  if (car_1 <= Dist_1)
  {
    lcd.print("OK ");
  }
  else
  {
    total += 1;
  }
  if (car_1 > Dist_1)   lcd.print("NO ");
  lcd.print("CAR2:");
  if (car_2 <= Dist_2)
  {
    lcd.print("OK ");
  }
  else
  {
    total += 1;
  }
  if (car_2 > Dist_2)   lcd.print("NO ");
  lcd.setCursor(0, 1);
  lcd.print("CAR3:");
  if (car_3 <= Dist_3)
  {
    lcd.print("OK ");
  }
  else
  {
    total += 1;
  }
  if (car_3 > Dist_3)   lcd.print("NO ");
  lcd.print("FREE:");
  lcd.print(total);
  if (timer_cnt >= 50)
  {
    mySerial.print('*');
    mySerial.print(total);
    mySerial  .println('#');
    timer_cnt = 0;
  }
  timer_cnt += 1;
  delay(200);
}

float sensor_1(void)
{
  digitalWrite(trig_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_1, LOW);
  Time_1 = pulseIn(echo_1, HIGH);
  distanceCM_1 = Time_1 * 0.034;
  return resultCM_1 = distanceCM_1 / 2;
}

float sensor_2(void)
{
  digitalWrite(trig_2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_2, LOW);
  Time_2 = pulseIn(echo_2, HIGH);
  distanceCM_2 = Time_2 * 0.034;
  return resultCM_2 = distanceCM_2 / 2;
}

float sensor_3(void)
{
  digitalWrite(trig_3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_3, LOW);
  Time_3 = pulseIn(echo_3, HIGH);
  distanceCM_3 = Time_3 * 0.034;
  return resultCM_3 = distanceCM_3 / 2;
}
//---------- Electronics-Project-Hub.com-----------------//

Program code for ESP8266:

Download Thingspeak library: Click here

// ----------(c) Electronics-project-hub-------- //
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "SSID"; //SSID here
char pass[] = "PASSWORD"; // Password here
//--------------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID =123456; // Your Channel ID
const char * myWriteAPIKey = "ACBDE12345"; //Your write API key
//-------------------------------------------//

const int Field_Number_1 = 1;
String value = "";
int value_1 = 0;
WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  internet();
}

void loop()
{
  internet();
  if (Serial.available() > 0)
  {
    delay(100);
    while (Serial.available() > 0)
    {
      value = Serial.readString();
      if (value[0] == '*')
      {
        if (value[2] == '#')
        {
          value_1 = value[1] - 0x30;
        }
      }
    }
  }
  upload();
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
}

void upload()
{
  ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey);
  delay(15000);
  value = "";
}
// ----------(c) Electronics-project-hub-------- //

Please enter your Wi-Fi credentials here:

//------- WI-FI details ----------//
char ssid[] = "SSID"; //SSID here
char pass[] = "PASSWORD"; // Password here
//--------------------------------//

Please enter your channel credentials here:

//----------- Channel details ----------------//
unsigned long Channel_ID =123456; // Your Channel ID
const char * myWriteAPIKey = "ACBDE12345"; //Your write API key
//-------------------------------------------//

How to upload code to ESP8266?

To upload the given code to ESP8266 we need 2 things:

  1. ESP8266 board package (Software).
  2. An ESP8266 programmer (Hardware).

1) How to Download ESP8266 board package?

  • Paste the URL on the box and click “OK”.
  • Now go to Tools > Board > Boards Manager.
  • A window will popup:
  • Type “ESP8266” on the box as shown and you will get installation option, select the latest version and click install.
  • Now the IDE will download the necessary packages and this could take more than 5 minute to complete.
  • Now go to Tools > Board > ESP8266 boards > select “Generic ESP8266”.
  • Now, copy the given ESP8266 program code and paste it on to Arduino IDE software.
  • Now press compile button (Green tick button). The compilation of code may take more than couple of minutes and be patient. If the compilation failed please check whether have you selected the “Generic ESP8266 Module” in the board option or not.
  • After successful compilation of code, now it’s time to upload the code to ESP8266.

2) How to upload code to ESP8266:

To program ESP8266 we need a programmer module as illustrated below:

ESP8266 Programmer
ESP8266 Programmer

Insert the ESP8266 on the programmer module as shown below and insert it to your PC’s USB port and press upload.

ESP8266 Programmer
ESP8266 Programmer

Once the code is successfully uploaded you will see the following info, now you may insert the ESP8266 to the main circuit:

Prototype images of IoT based vehicle monitoring system:

IoT based car parking system
IoT based car parking system
IoT based car parking system
IoT based car parking system

How to operate the machine?

  • After completing the circuit setup and Thingspeak account setup, power the circuit ON.
  • You will see the below illustrated screen:
  • Here we have three parking spots and each parking spot’s status is displayed along with number of free parking spots.
  • When you bring an obstacle like a junk box near to the ultrasonic sensor, the machine will count it as an occupied parking spot and this information will be updated to Thingspeak as well as on the display.
  • “OK” status signifies that a car / vehicle is occupied and “NO” signifies the parking spot is empty.

Thingspeak Private View:

Thingspeak public view:

Thingspeak public view on smart phone:

The URL of the public view page is converted in to a QR and this can be done using any online URL to QR code converter tool. This QR code can be placed at the parking spot or anywhere else, so that the users can scan and bookmark the URL and when they are visiting the parking spot, users can open the link and see how many vacant spaces exist and if this parking lot is full they can move to another.

If you have any questions regarding this project, feel free to ask us in the comment you will get a guaranteed replay from us.  

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.