Arduino Mega Smart Parking System with RFID, IR Sensors & Servo Gate

Build an Arduino Mega smart parking system with RC522 RFID entry, four IR slot sensors, a servo barrier and a 16×2 I2C LCD. Wiring table, diagram and full code.

  • DifficultyAdvanced
  • Build time5–8 hours
  • Board / MCUArduino Mega 2560
  • Parts9 items
  • CodeArduino C++

Need it ready-made? Buy Ready Project

This Arduino Mega smart parking system runs a small car park on its own. Four IR sensors watch the parking bays, a 16×2 I2C LCD shows how many slots are free, and an SG90 servo lifts a barrier only when a registered RFID card is scanned at the entry. Cars leaving are let out automatically, and when every bay is taken the red LED lights and the display says PARKING FULL.

It is a big, combination project: RFID over SPI, an I2C display, a servo, and six IR sensors all work together in one sketch. The Arduino Mega 2560 (ATmega2560) is a good fit because it has plenty of spare pins, hardware SPI on pins 50–53 and I2C on pins 20–21, so nothing has to share a pin. It makes a great model for a school exhibition, an apartment gate demo or a college mini-project.

How the Smart Parking System Works

  • Slot sensing: each bay has an IR obstacle sensor. When a car (or a toy car on a model) is above it, the module pulls its OUT pin LOW. The Mega reads pins D24–D27 and counts the free bays.
  • Display: line 1 of the LCD shows Free slots: 3/4. Line 2 shows each bay as S1O (free) or S1X (occupied). The green LED is on while space is available and the red LED is on when the car park is full.
  • Entry: when the entry IR sensor on D22 sees a car, the LCD asks for a card. The RC522 reader checks the card’s UID against a list in the code. A known card makes the servo on D9 raise the barrier; an unknown card gives a long beep and Access denied.
  • Full car park: if no bays are free, the gate stays shut, the buzzer beeps twice and the LCD shows PARKING FULL, even if the card is valid.
  • Exit: the exit IR sensor on D23 opens the gate with no card, so nobody gets trapped inside.
  • Closing the gate: the barrier stays up until the car has passed the sensor, then waits 1.5 seconds and closes. A 15-second time-out closes it if the car never moves.

Components Required

  • Arduino Mega 2560 (or a Mega 2560 compatible board) – 1
  • MFRC522 (RC522) 13.56 MHz RFID reader with cards or key tags – 1 set
  • 16×2 LCD with I2C backpack (PCF8574, address 0x27 or 0x3F) – 1
  • SG90 micro servo (or MG90S for a heavier barrier) – 1
  • IR obstacle sensor modules (FC-51 type) – 6 (2 for the gate, 4 for the bays)
  • Green LED and red LED – 1 each, with 220 Ω resistors – 2
  • Active 5 V buzzer – 1
  • External 5 V, 2 A power supply for the servo (a USB charger with a cut cable or a buck converter works)
  • USB cable for the Mega, breadboard, jumper wires, and a cardboard or foam-board base for the parking model

Wiring Table (Pin-to-Pin)

ModuleModule PinArduino Mega PinNotes
RC522 RFIDSDA (SS)D53Hardware SPI chip select
RC522 RFIDSCKD52
RC522 RFIDMOSID51
RC522 RFIDMISOD50
RC522 RFIDRSTD49
RC522 RFID3.3V / GND3.3V / GNDNever connect the RC522 to 5 V
RC522 RFIDIRQNot connected
I2C LCDSDA / SCLD20 / D21Mega I2C pins (also on the SDA/SCL header)
I2C LCDVCC / GND5V / GND
SG90 servoOrange (signal)D9
SG90 servoRed (+)External 5 V +Do not power the servo from the Mega’s 5V pin
SG90 servoBrown (GND)External 5 V − and Mega GNDGrounds must be common
Entry IR sensorOUTD22LOW when a car is in front
Exit IR sensorOUTD23LOW when a car is in front
Slot 1–4 IR sensorsOUTD24, D25, D26, D27One sensor per bay
All IR sensorsVCC / GND5V / GNDAdjust the blue pot for 3–8 cm range
Green LEDAnode via 220 ΩD30Cathode to GND
Red LEDAnode via 220 ΩD31Cathode to GND
Buzzer+ / −D32 / GNDActive buzzer

Connection Diagram

The illustration below is also the featured image and shows the overall layout of the build. Follow the pin-accurate drawing under it, which matches the wiring table, when you connect the wires.

Wiring diagram of an Arduino Mega 2560 smart parking system with RC522 RFID reader, I2C LCD, SG90 servo gate, entry and exit IR sensors, four slot IR sensors, LEDs and buzzer
Smart parking system overview: RFID reader, I2C LCD, servo barrier, gate and slot IR sensors, LEDs and buzzer.
Arduino Mega 2560 Smart Parking System – Wiring Arduino Mega 2560 D53 SS, D52 SCK D51 MOSI, D50 MISO D49 RST 3.3V, 5V, GND D20 SDA, D21 SCL D9 servo D22 entry IR D23 exit IR D30 green, D31 red D32 buzzer D24–D27 slot IR 1–4 RC522 RFID (3.3 V only) SDA → D53, SCK → D52 MOSI → D51, MISO → D50 RST → D49, IRQ → not used 3.3V → 3.3V, GND → GND 16×2 LCD + I2C backpack SDA → D20, SCL → D21 VCC → 5V, GND → GND SG90 servo (barrier) Orange signal → D9 Red → external 5 V (1–2 A) Brown → GND (common with Mega) Gate IR sensors Entry OUT → D22, Exit OUT → D23 VCC → 5V, GND → GND Indicators D30 → 220 Ω → green LED → GND D31 → 220 Ω → red LED → GND D32 → active buzzer (+), (−) → GND Parking slot IR sensors (one per bay, facing up or across the bay) Slot 1 OUT → D24 Slot 2 OUT → D25 Slot 3 OUT → D26 Slot 4 OUT → D27 All sensor VCC pins go to 5V and all grounds are common. Purple = SPI, green = I2C, orange = servo, violet/yellow = IR signals, red = indicators.
Pin-accurate connection diagram for the Arduino Mega 2560 smart parking system.

Libraries to Install

  • MFRC522 by GithubCommunity (the miguelbalboa library) – Arduino IDE, Tools, Manage Libraries, search MFRC522.
  • LiquidCrystal I2C by Frank de Brabander.
  • Servo, SPI and Wire come with the Arduino IDE.

Arduino Mega Smart Parking Code

Select Tools, Board, Arduino Mega or Mega 2560 and the right COM port, then upload. The sketch compiles to about 10.8 KB, so there is lots of room left on the Mega for extra features.

/*
  Arduino Mega 2560 Smart Parking System
  RFID-controlled entry gate + free exit + 4 slot sensors + 16x2 I2C LCD

  Libraries (Library Manager):
    - MFRC522 by GithubCommunity (miguelbalboa)
    - LiquidCrystal I2C by Frank de Brabander
    - Servo (built in)

  Wiring summary
    RC522  : SDA(SS)->53, SCK->52, MOSI->51, MISO->50, RST->49, 3.3V, GND
    LCD I2C: SDA->20, SCL->21, VCC->5V, GND
    Servo  : signal->9, power from separate 5 V supply (common GND)
    IR gate: entry->22, exit->23
    IR slot: slot1..slot4 -> 24..27
    LEDs   : green->30, red->31 (each through 220 ohm)   Buzzer -> 32
*/

#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

// ---------- pins ----------
#define RFID_SS   53
#define RFID_RST  49
#define SERVO_PIN 9
#define IR_ENTRY  22
#define IR_EXIT   23
#define LED_GREEN 30
#define LED_RED   31
#define BUZZER    32
const uint8_t SLOT_PINS[4] = {24, 25, 26, 27};
const uint8_t NUM_SLOTS = 4;

// ---------- settings ----------
#define GATE_CLOSED_ANGLE 0     // barrier down
#define GATE_OPEN_ANGLE   90    // barrier up
#define IR_ACTIVE LOW           // most IR obstacle modules pull OUT low when they see an object
#define GATE_TIMEOUT_MS 15000UL // close the gate anyway after 15 s
#define CLEAR_DELAY_MS   1500UL // wait after the car passes before closing

// Authorised card UIDs. Scan your cards, read the UID on Serial Monitor, then put it here.
const byte AUTH_UIDS[][4] = {
  {0xDE, 0xAD, 0xBE, 0xEF},   // example card 1 (replace)
  {0x12, 0x34, 0x56, 0x78}    // example card 2 (replace)
};
const uint8_t NUM_UIDS = sizeof(AUTH_UIDS) / sizeof(AUTH_UIDS[0]);

MFRC522 rfid(RFID_SS, RFID_RST);
LiquidCrystal_I2C lcd(0x27, 16, 2);   // try 0x3F if the screen stays blank
Servo gate;

int lastFree = -1;
bool prompted = false;    // "scan your card" already on screen

// ---------- helpers ----------
bool irSees(uint8_t pin) { return digitalRead(pin) == IR_ACTIVE; }

int countFreeSlots() {
  int freeSlots = 0;
  for (uint8_t i = 0; i < NUM_SLOTS; i++) {
    if (!irSees(SLOT_PINS[i])) freeSlots++;    // no car above the sensor = free
  }
  return freeSlots;
}

void beep(int ms) {
  digitalWrite(BUZZER, HIGH);
  delay(ms);
  digitalWrite(BUZZER, LOW);
}

// Line 1: free count, line 2: which slots are taken (X) or free (O)
void showStatus(int freeSlots) {
  lcd.clear();
  lcd.setCursor(0, 0);
  if (freeSlots == 0) {
    lcd.print("  PARKING FULL  ");
  } else {
    lcd.print("Free slots: ");
    lcd.print(freeSlots);
    lcd.print("/");
    lcd.print(NUM_SLOTS);
  }
  lcd.setCursor(0, 1);
  for (uint8_t i = 0; i < NUM_SLOTS; i++) {
    lcd.print("S");
    lcd.print(i + 1);
    lcd.print(irSees(SLOT_PINS[i]) ? "X " : "O ");
  }
  digitalWrite(LED_GREEN, freeSlots > 0 ? HIGH : LOW);
  digitalWrite(LED_RED,   freeSlots > 0 ? LOW  : HIGH);
}

void showMessage(const char *l1, const char *l2) {
  lcd.clear();
  lcd.setCursor(0, 0); lcd.print(l1);
  lcd.setCursor(0, 1); lcd.print(l2);
}

// Open the barrier, wait for the car to pass the given sensor, then close it
void passCar(uint8_t sensorPin) {
  gate.write(GATE_OPEN_ANGLE);
  unsigned long start = millis();
  // wait until the car reaches/passes the sensor (or time out)
  while (irSees(sensorPin) && millis() - start < GATE_TIMEOUT_MS) delay(20);
  delay(CLEAR_DELAY_MS);
  gate.write(GATE_CLOSED_ANGLE);
  lastFree = -1;            // force a display refresh
}

// Returns true if the card UID is in the allowed list
bool cardAllowed(const MFRC522::Uid &uid) {
  if (uid.size < 4) return false;
  for (uint8_t c = 0; c < NUM_UIDS; c++) {
    bool match = true;
    for (uint8_t b = 0; b < 4; b++) {
      if (uid.uidByte[b] != AUTH_UIDS[c][b]) { match = false; break; }
    }
    if (match) return true;
  }
  return false;
}

void printUid(const MFRC522::Uid &uid) {
  Serial.print("Card UID:");
  for (byte i = 0; i < uid.size; i++) {
    Serial.print(uid.uidByte[i] < 0x10 ? " 0x0" : " 0x");
    Serial.print(uid.uidByte[i], HEX);
  }
  Serial.println();
}

// ---------- setup ----------
void setup() {
  Serial.begin(9600);
  pinMode(IR_ENTRY, INPUT);
  pinMode(IR_EXIT, INPUT);
  for (uint8_t i = 0; i < NUM_SLOTS; i++) pinMode(SLOT_PINS[i], INPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(BUZZER, OUTPUT);

  gate.attach(SERVO_PIN);
  gate.write(GATE_CLOSED_ANGLE);

  SPI.begin();
  rfid.PCD_Init();

  lcd.init();
  lcd.backlight();
  showMessage(" Smart Parking ", "  Starting...  ");
  delay(1500);
}

// ---------- main loop ----------
void loop() {
  int freeSlots = countFreeSlots();
  if (freeSlots != lastFree) {        // redraw only when something changes
    showStatus(freeSlots);
    lastFree = freeSlots;
  }

  // 1) Car leaving: exit sensor opens the gate with no card needed
  if (irSees(IR_EXIT)) {
    showMessage("   Thank you!   ", "  Drive safely  ");
    passCar(IR_EXIT);
    return;
  }

  // 2) Car arriving: wait for a card scan while the car is at the entry sensor
  if (irSees(IR_ENTRY)) {
    if (freeSlots == 0) {
      showMessage("  PARKING FULL  ", " Sorry, no space");
      beep(150); delay(150); beep(150);
      delay(1500);
      lastFree = -1;
      return;
    }
    if (!prompted) {                  // draw the prompt once, not every loop
      showMessage("Welcome! Please ", "scan your card  ");
      prompted = true;
    }
    if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
      printUid(rfid.uid);
      if (cardAllowed(rfid.uid)) {
        showMessage(" Access granted ", " Gate opening.. ");
        beep(100);
        passCar(IR_ENTRY);
        prompted = false;
      } else {
        showMessage(" Access denied  ", " Unknown card   ");
        beep(800);
        delay(1200);
        prompted = false;             // show the prompt again
        lastFree = -1;
      }
      rfid.PICC_HaltA();
      rfid.PCD_StopCrypto1();
    }
    delay(100);
    return;
  }

  // car drove away from the entry without a card: go back to the status screen
  if (prompted) { prompted = false; lastFree = -1; return; }

  // 3) Idle: still print UIDs of any scanned card, handy for enrolling new cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    printUid(rfid.uid);
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }
  delay(50);
}

Adding Your Own RFID Cards

  1. Upload the sketch and open the Serial Monitor at 9600 baud.
  2. With no car at the entry sensor, tap each card on the reader. The monitor prints a line like Card UID: 0xA3 0x5F 0x12 0x9C.
  3. Copy the first four bytes into the AUTH_UIDS list, for example {0xA3, 0x5F, 0x12, 0x9C}. Add one line per card.
  4. Upload again. Those cards now open the gate.

Testing and Calibration

  1. Turn each IR module’s blue potentiometer until its second LED lights only when your hand or a toy car is 3–8 cm away.
  2. Check the LCD. With all bays empty it should show Free slots: 4/4 and S1O S2O S3O S4O.
  3. Cover the slot sensors one by one and watch the count drop. With all four covered, the red LED turns on and the LCD shows PARKING FULL.
  4. Uncover a bay, block the entry sensor and scan an enrolled card. The barrier should rise, then fall 1.5 seconds after you remove your hand.
  5. Block the exit sensor. The gate should open without any card.

Troubleshooting

ProblemFix
LCD backlight is on but no textTurn the contrast pot on the I2C backpack. If it is still blank, change the address in the code from 0x27 to 0x3F.
RFID cards are never readCheck that SDA goes to D53 (not D10 as on the Uno), that the RC522 is on 3.3 V, and that the jumper wires are short.
Board resets when the servo movesPower the servo from a separate 5 V supply and join its ground to the Mega’s GND.
A bay always shows occupiedTurn that sensor’s pot to reduce its range, or shield it from sunlight, which can trigger IR modules.
Logic is inverted (free shows X)Your IR modules output HIGH on detection. Change IR_ACTIVE from LOW to HIGH.
Gate moves the wrong waySwap the values of GATE_OPEN_ANGLE and GATE_CLOSED_ANGLE.

Ideas to Extend This Project

  • Add an ESP8266 or ESP32 on Serial1 (pins 18/19) to show free slots on a phone web page.
  • Log entry and exit times with a DS3231 RTC and a microSD card module for a simple parking ticket system.
  • Scale it up to more bays; the Mega still has more than 20 free digital pins.
  • Replace bay IR sensors with HC-SR04 ultrasonic sensors for real cars in an outdoor setup.

FAQ

Can I use an Arduino Uno instead of the Mega? Only with fewer parts. The Uno does not have enough free pins for six IR sensors, SPI, I2C, a servo, two LEDs and a buzzer, which is why this build uses the Mega 2560.

Will any RFID card work? The RC522 reads 13.56 MHz MIFARE cards and tags, such as the blue key fob and white card that come with it. It cannot read 125 kHz EM4100 cards.

Is this suitable for a real car park? It is a working model. For a real gate you would use a proper motorised barrier, weatherproof sensors and a relay or motor driver instead of the SG90.

Related Projects

Browse more Arduino Projects on Arduino Project Hub.

2 thoughts on “Arduino Mega Smart Parking System with RFID, IR Sensors & Servo Gate”

Leave a Comment