You are on page 1of 61

www.ebook777.

com
Simple arduino based Automated Parking lot System

Seyedreza Fattahzadeh
© 2015 Seyedreza Fattahzadeh
srfattah@yahoo.com
info@plc-doc.com
No part of this book may be reproduced, in any form or by any means, without the
written permission from the author / publisher.

www.ebook777.com
NOTICE TO THE READER
All rights reserved. No part of this book may be reproduced, in any form or by any
means, without permission in writing from the publisher.
Every effort has been made to ensure our books are as accurate as possible. However,
there may be mistakes, both typo and in content. This content should be used as a general
guide and not as the solution. The author does not warrant or guarantee any of the
products described herein. Also, the author does not warrant or guarantee any of the
equipment or programs described herein or accept liability for any damages resulting from
their use. The reader is warned that both electricity and the construction of electrical
equipment can be dangerous. It is the responsibility of the reader to use common sense
and safe electrical and mechanical practices.
Arduino ® is a registered trademark of Arduino.
CodevisionAVR ® is a registered trademark of HP Info Teach
Proteus ® is a registered trademark of Lab center Electronics Company.
Altium Designer ® is a registered trademark of Altiume Company.
AVR and Atmega32 are trademarks Atmel Company.
Please be informed that:
This book contains many images. Since eReaders don’t always display images well,
upon request, I will provide you with the PDF file which contains the complete e-Book,
including the images, if you need an easier-to-read format. To receive a PDF version of
this e-book or any other software which is mentioned in the content of this text, you can
email me the proof of your purchase of the kindle version of the book from Amazon.com
to srfattah@yahoo.com. Upon receipt of that proof, a PDF version will be sent to your
email address.
Table of Contents
Preface
About the Author
How to use this book
Files developed for this project
Chapter 1 : AVR based parking system
Step 1: Project specification of the system
Generating schematic diagram of the project
Step 2: An overview of AVR microcontroller program
read_mux() function
sum_of_cars() function
read_gates function
LED_status() function
Car_times() function
calculate_price() function
show_lcd() function
write_outputs() function
Modify time
Step 3: Getting to manufacture PCBs of the prototype model lift system
Break-Beam sensor
Arm gate motor, related limit switches and motor driver PCB
Display counter
Sixteen toggle switches to indicate presence of cars parked
Main board
Step 4: Uploading the control program into the ATmega16 microcontroller (hex file)
Chapter 2 : Arduino base parking system
Step 1: The Arduino board
Step 2: An overview of Arduino board program
setup() function
timing() function
read_gates() function
led_statuse function

www.ebook777.com
Calculate() function
Show_lcd
set_key(), up_pb(), down-pb() functions
Step 3: Getting to manufacturing the PCBs of the prototype model parking systems
Step 4: Uploading the control program into the Arduino board
Conclusion
Bibliography
My e-book product list
Preface
The purpose of this manual is to design and implement a Microcontroller based
Parking system. In short, the Microcontroller based system will keep track of the number
of cars in the parking area. It will not allow more than 16 cars to park. It will show the
number of slots unoccupied, will automatically raise the Gate arm to allow cars into the
parking lot or leave it. It keeps track of the amount of time a car is parked, the time at
which driver leaves the parking lot, shows current and exit Time and Date and display
them on the a LCD. The microcontroller will finally calculate the cost of parking, for each
car parked in the Parking area. When the amount is paid and the parking operator
depresses a paid button, the exit arm gate is raised, and one unit is added to the total
number of vacant cars in the Parking lot.
The purpose behind developing a Project like this is an attempt to make learning
Microcontroller programming interesting and enjoyable! Making a connection with a
familiar control system such as a typical Microcontroller based automated parking system
is for showing how and why timers, limit switches, sensors and relays are used.

www.ebook777.com
About the Author
Seyedreza Fattahzadeh holds a Bachelor of Science in Electrical Engineering from
the University of Texas at Arlington. His main expertise is in digital systems, electronics,
and computer engineering.
Seyedreza has extensive experience in the field of programmable controllers. He
worked for a few industrial manufacturers as a programmable controller consulting firm.
His industrial experience includes designing and implementing mid - class automation
systems, based upon PLC.
Seyedreza has authored several other books about programmable controllers. You can
How to use this Book
This book is prepared for those who are familiar with the basic instructions of any
typical microcontroller and C language, and want to challenge their knowledge by writing
a more complex industrial program: in this case, preparing a control program for an AVR
microcontroller and Arduino board to control a car parking system.
In the text, the complete implementation of the project is presented to you. Read it
carefully and your job is to re-write your own version of the same program. Use your
simulator software (such as Proteus) or your own microcontroller hardware to develop a
similar program. When you have done it, simulate your solution and verify if it works
perfectly at the first try. If you did not succeed at the first try, then compare your solution
with the one presented in this text. When you find out what is wrong in your control
program, go ahead and fix it. Try again and again until you get it done right. When you
finally succeed making it, this means you are on the right track towards mastering your
microcontroller programming knowledge. And that is the purpose of this manual.
The prototype version of the project is actually designed and implemented in my lab.
The control software is presented to you after it is tested on the hardware, and I have made
sure it is functioning 100% according to the designed specification.
To watch the performance of the project hardware and software at action, click on the
these links:
http://www.youtube.com/watch?v=7aIp2iCC_mU
http://www.youtube.com/watch?v=CNt2DIlwrso
For the older version of this project with PLC (Programmable Logic Control) click on
the following link:
http://youtu.be/51oqLRxXcHk
The schematic diagrams of all hardware circuits used in this project are provided. If
you wish, you can even design and implement your own hardware system similar to mine
or even tailor it to the one with more features that you wish to have. Or, you can still learn
by simulating your own program and checking out its performance against mine. And if
you find yourself stuck up at any step, you can check out my solution against yours!

www.ebook777.com
Files developed for this project:
The table below lists the files developed for this project explained in Chapter 2. These
files are not included in the text. To get these files emailed to your address, please contact
me as you are directed in the 'Please be informed' section of this text'.

Files related to Chapter 1

Simulation Proteus based file: parking.DSN

Codvision based generated file: parking.prj

Parking.PcbDoc (Main Controller board layout)

Files related to Chapter 2

parking.ino (Main controller file)

A_Parking.PcbDoc (Main controller board layout)


Chapter 1 : AVR based parking system

Step 1: Project specification of the system


At the first step, an outline of this project (Figure 1.1) is portrayed by the next details.
You may use the following link to see the performance of designed systems from
Youtube.com website before anything.
http://www.youtube.com/watch?v=7aIp2iCC_mU

Figure 1.1: The parking system with AVR microcontroller


1. We are dealing with a simple microcontroller based parking system that can control
16 cars at the maximum. Each time a car enters, the microcontroller automatically
deducts one from the total number of cars that can be parked in the garage, and
displays total number of cars that can be parked after last entrance (16-1 = 15). Each
car that comes out will automatically be added to number of slots available in the
garage. When 16 cars are parked, a signal (red LED indicator) will turn on and off,
indicting that the garage is full and notifying other drivers not to enter because there is
no space available. A two digit numerical Seven Segment LED Display (installed at
the parking entrance) shows total number of vacancies.

www.ebook777.com
Figure 1.2
2. The total number of available slots in the garage is updated when a car is either
entering or exiting the parking lot. There is a Break-Beam sensor at the entrance of
the garage which sends a signal to the microcontroller anytime light beam between
transmitter and receiver is broken (IR sensor shown in Figure 1.3). If there is any
vacant space for the incoming car to park, Entrance arm gate is lifted to let the car to
get in. Otherwise, the red LED light blinks with higher frequency to warn the driver
that no space is available to park.

Figure 1.3: IR sensor


3. At each parking slot, there is a toggle switch installed for each car. When a car enters
into the garage area, toggle switch is depressed (manually), a red LED indicator turns
on, which implies that the car is parked. On the other hand, anytime a car leaves the
parking slot, the corresponding green LED turns on (Figure 1.4).
Figure 1.4
4. When the parking lot is full (maximum of 16 cars are in the lot), the red light blinks
with frequency of 1 HZ. Blinking frequency is doubled when there is no space for
incoming car and Break-Beam Sensor is activated.
5. After a car is parked at any available slot, or the time it is exiting the parking
facilities, all data related to its entrance or exit and parking expense is determined and
displayed on a LCD in real time.
6. Prior to exit, a driver must stop at the exit kiosk to make a payment to cover the
parking cost. Upon depression of the Paid button by the parking operator, the out arm
gate is lifted up to let the driver out. There is a 10 second grace period from the time
of leaving the parking lot for derivers to exit the garage area.
7. If for any reason, the driver does not leave the parking lot, the output of count up
counter would not be changed.
8. Upon the exit of the car (plus the gate activation), the system automatically increases
the number of slots available in the garage by one unit.

www.ebook777.com
Generating schematic diagram of the project
After determining what the project scope is, the next step is to come up with a
schematic diagram of the project. After all, this is the hardware and software parts of the
project, which are supposed to function in a way to carry out the main task of the project.
Let's draw schematic diagram of the project in a way that we think it can carry out the
task of controlling all push buttons, 7-segments displays, LEDs etc.
We might be able to come up with a circuit diagram as shown in Figure 1.5 and try to
figure out if that is going to be able to control different electronics of our project nicely.
No problem to test the circuit and code at this stage. Our big brothers already have
developed two great software- CodevisionAVR ® and Proteus ® that can help us to
generate code and simulate the circuit respectively. So, at next step, we may go ahead and
start generating program code for ATmega16 microcontroller after putting all components
on the edit screen of the great Proteus software.

Figure 1.5: displays Proteus generate schematic diagram of the project.


Step 2: An overview of AVR microcontroller program
Now I need to generate the control code to simulate the circuit shown in Figure 1.5 to
make sure if the components shown in this figure are functioning properly. You can go
ahead and use CodevisionAVR software to generate the main code and save it in a Hex
format. In the next step, you will need to go back to Proteus and call up the Hex file which
you already have created and start simulating the schematic diagram which is shown in
Figure 1.5.
In this project, an ATmega16 AVR microcontroller is used. Its program is written in C
language by CodvisionAVR software whose full content is available in attendant CD.
Before explaining the written program, you may want to see a flowchart of the program in
Figure 1.6 to help understand the process better.

www.ebook777.com
Figure 1.6: A flowchart of the AVR program
In the beginning of the program, needed libraries, suitable symbols for each
microcontroller pins and then needed variables are defined. Then the program goes into
the main function and does the following work for one time:
1. It gets the current time and date from RTC ( A Real Time Clock is connected to AVR )
2. The zero (0) number is stored as initial content of all variables related to the entrance
and exit time of each parking slot.
3. It computes the total number of vacancies in the parking lot
Then the program calls several functions, each one performing a specific task, such as
displaying data on the LCD, computing the number of available cars, controlling the LEDs
etc. The important functions are explained in following sections.

www.ebook777.com
read_mux() function
This function receives the state of each parking slot and stores them in car[1] to
car[16] variables. We use a 16×1 multiplexer between 16 signals from parking slots and
one pin of microcontroller. So for example, when the microcontroller want to update
car[16] variable, it sets the four control inputs of multiplexer(mux1 to mux4).
Accordingly, the multiplexer send its 16th input to its output and then microcontroller
reads the multiplexer output and stores it in car[16] variable.
mux1=1; mux2=1; mux3=1; mux4=1;
car[16]=!mux_out;
sum_of_cars() function
This function computes the number of vacant slots available in the parking lot
according to the number of cars parked (car[1] to car[16] variables). At first, this function
uses read_mux function to scan and store the state of each parking slot in the car array.
Then it changes the initial content of capacity_sam variable to zero and uses it to store
number of available car in parking lot. The number of parked cars is calculated by the
following loop
for(i=1;i<17;i++)
capacity_sam+=car[i];

Then capacity_sam variable is deducted from 16 get the available capacity instead of the
number of parked car in the parking lot.
capacity_sam=16-capacity_sam;

www.ebook777.com
read_gates function
In this function, if input_sensor value equals 1 (it means there is a car in the entrance
of the parking lot) and the capacity is greater than 0, then microcontroller issues a
command to move the arm gate up to let the car get in. The perm1==1 condition prevents
from repeating this instruction
if((input_sensor==1)&&(capacity>0)&&(perm1==1))
{
input_motor_up=1;
input_motor_down=0;
counter_perm=1
perm1=0;
}

In addition, the content of Cunter_perm variable is changed to 1 to have a permission to


deduct the parking capacity in future.
After the entrance arm gate is moved up completely (input_up_ms==1), the
input_down_time variable is set to active a timer to close the entrance gate after a
specific time (more explanation will be given in the later text). The entrance gate stays
open for about 5 seconds to give the driver enough time to go past underneath the arm
gate.
if(input_up_ms==1)
{
input_motor_up=0;
input_down_time=1;
}

Then entrance arm gate moves back to its original position and the related motor is turned
off when the related limit switch is activated.
if(input_down_ms==1)
{
input_motor_down=0;
}

Also, at the exit of the parking, if the paid key is depressed by parking operator, then
the program computes the parking capacity again and if there is a deference between the
older parking capacity and computed capacity, it means one or some cars have exit their
slots and are leaving the parking lot. Then at the entrance gate, the out arm gate is lifted up
to let the driver out. Then one unit is added to parking capacity, and all related variables of
the new vacant space are reset. The output_down_time variable is set and then until the
expiry of 10 seconds, the drivers can leave the parking lot and after that exit arm gate
moves back to its original position and the related motor is turned off when the related
limit switch is activated.
In the timer2 interrupt, a program generates a delay in the gates moving down. When
either input_down_time or output_down_time changes to 1, the related count variable
starts to get incremented. When its value exceeds the defined value, the system brings the
related gate down.

www.ebook777.com
LED_status() function
This function determines the state of two green, red LEDs. If there are some vacant
spaces in the parking lot, the green LED is turned on. Otherwise the red LED is turned on
(blinking with a slow speed) to inform the next driver that parking area is already full. In
this case, if IR sensor detects any obstacle again, the red LED will blink at a faster rate.
if(capacity>0)
{
g_led=1;
r_led=0;
blink=0;
}
if((capacity==0)&&(input_sensor==0))
{
blink_time=140; / and 70 for fast blink/
g_led=0;
blink=1;
}

In above program code, the speed of red LED blinking can be changed by blink_time
variable. This variable is used in timer0 interrupt. In its interrupt function, the count
variable is incremented continuously, and when it reaches to blink_time value, if the
blink variable value equals 1, the state of red LED is inverted to show up a blinking LED
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
count++;
if(count>blink_time)
{
count=0;
if(blink==1)
r_led=~r_led;
}
}
Car_times() function
This function stores cars’ entry and exit time in the relevant variables. At the first time,
the whole parking is scanned by read_mux function. Then in a loop, each slot is checked.
read_mux();
for(x=1;x<17;x++)
{↓

If there is a parking slot then its key is activated and if it wasn’t checked previously, then
the entrance time and date are stored in related variables. And if counter_perm (go back
to read_gates function) is 1, the parking capacity is decreased by one unit.
if((car[x]==1)&&(car_perm[x]==0))
{
car_day_in[x]=day;
car_month_in[x]=month;
car_hour_in[x]=hour;
car_min_in[x]=minute;
car_sec_in[x]=sec;
car_perm[x]=1;
sum_of_cars();
if((capacity_sam<capacity)&&(counter_perm==1))
{
capacity—;
counter_perm=0;
}
}

The exit time for each parking slot is recorded when the state of a slot (for example
car[5] variable for 5th slot) is zero and its entrance time is recorded before and the exit
time isn’t recorded still. Because it means the parked car in 5th slot exit from 5th slot and is
leaving the parking, so the exit time should be recorded in related variable to compute its
parking cost. The total cost is calculated by calculate_price function. This function
receives the number of the slot, and calculates its total cost.
if((car[x]==0)&&(car_perm[x]==1))
{
car_day_out[x]=day;
car_month_out[x]=month;
car_hour_out[x]=hour;
car_min_out[x]=minute;
car_sec_out[x]=sec;
total_price[x]=calculate_price(x);
car_perm[x]=0;

www.ebook777.com
time is equal to exit time or exit time is greater than entrance time by just one second, then
the program disregards the detected entrance or exit signal and resets all the related
variables.
if((car_sec_in[x]==car_sec_out[x])||(car_sec_in[x]==(car_sec_out[x]+1)))
{
car_day_in[x]=0;
car_month_in[x]=0;
car_hour_in[x]=0;
car_min_in[x]=0;
car_sec_in[x]=0;
car_day_out[x]=0;
car_month_out[x]=0;
car_hour_out[x]=0;
car_min_out[x]=0;
car_sec_out[x]=0;
total_price[x]=0;
}

When a car has exit from a slot (for example 2nd slot) and it is going out, then the
slect_screen variable is changed to the slot number (here number 2). Then show_lcd
function displays the entrance and exit time and its total cost instead of the normal screen
that shows time, date and parking capacity.
calculate_price() function
In this function the entering time of a car is subtracted from its exiting time to
calculate its total parked time. Some conditions are considered in computing parking cost.
For example if the entering time is greater than exit time, it means the car exited in a
different day from entrance day. The parking cost is computed according to the following
table:

From 1 hour until From 3h More than 24


Less than one hour
3 hours until 24h hours

40 ¢ for entrance cost and one ¢


One dollar 3 dollar 6 dollar
for each minute

www.ebook777.com
show_lcd() function
There are 17 screens to display needed information on one LCD. One is the main
screen for displaying parking capacity, time and date and the rest are for displaying
entrance and exit time and the total parking cost. So the LCD displays different data on the
same screen, according to the content of select_screen variable (1 to 17). For example, if
select_screen equals 17, then the main screen data is displayed
write_outputs() function
The all microcontroller outputs are sent serially by two shift registers. The following
items are considered for microcontroller outputs.
1. Turn a motor to lift the exit arm gate down.
2. Turn a motor to lift the exit arm gate up.
3. Turn a motor to lift the entrance arm gate down
4. Turn a motor to lift the entrance arm gate up
5. Green LED
6. Red LED
7. First 7segment
8. Second 7segment.

www.ebook777.com
Modify time
Three push buttons (setup, up and down) are used to modify the time or select one of
the 17 screens to be displayed. These keys are connected to external interupt2, 0 and 1
respectively.
By depressing setup push button, the content of set variable is increased one unit and
if we are in the main screen, each time that this key is depressed the following parameters
can be modified.
Hour, minute, second, day of week, day, month, year.
Each of the above parameters can be increased or decreased by up and down keys.
If the up or down pushbuttons are depressed without setup pushbutton, then the LCD
displays the state of each parking slot (1 to 16) or the main screen (17).
Step 3: Getting to manufacture PCBs of the prototype model lift system
It was very encouraging to see that simulation was carried out successfully and now
we wish to get the PCBs built and test the parking system. This might be also related to a
project that we are working on it as a college final year project and our instructors have
advised us the course grade will not be give in full unless we show them the whole
hardware of the system working properly!
It seems that the following items are the main sections in building of a simulator
parking system.
1. Break-Beam sensor circuit to detect cars.
2. Arm gate motors, related limit switches, and motor driver PCB at entrance and exit
parking lot
3. A two digits numeric to display parking capacity.
4. 16 toggle switches to indicate presence of cars parked in 16 slots of the parking lot.
5. A main control board that installed in parking operator room and it consist of
microcontroller, A LCD to display information, and the pushbuttons needed for the
operator.

www.ebook777.com
Break-Beam sensor
Figure 1.7 displays the schematic circuitry of the IR sensor (Break-Beam Sensor).
Notice that when circuit in Figure 1.7 is energized, the NO terminal of the circuit is raised
to 5 VDC. And when the IR ray is broken by presence of a car in front of the parking
entrance arm gate, relay is activated (NC = 1 or 5 VDC) and that is the time when the
microcontroller is notified that a car at the entrance wants to get in. Electronic components
are soldered to a printed circuit board to create the IR sensor PCB shown in Figure 1.8.

Figure 1.7

Figure 1.8
Arm gate motor, related limit switches and motor driver PCB
Figure 1.9 displays the schematic circuitry of two DC driver motors, which is
responsible to lift up or down any arm gate at either entrance or exit of the parking garage.
When EN UP = 1 and EN Down = 0, signals are set accordingly, the motor rotates in
counterclockwise direction to lift the arm gate at the entrance of the garage. The gate
returns to its initial position when EN Down = 1 (EN UP = 0) which causes the motor to
rotate in a clockwise direction. The same is true for the EX DC motor.
Figure 1.10 illustrates the pictorial diagram of the circuit related to the two arm gates
used in this project. When limit switches 1 and 2 in Figure 1.10 are activated, then the
microcontroller is informed that the entrance arm gate is either closed (its normal position)
or open. Also when limit switches with names 3 and 4, it means that the exit arm gate is
either in its normal position (closed) or open position.

Figure 1.9

Figure 1.10

www.ebook777.com
Display counter
Figure 1.11 displays schematic diagram of a 2 digit numeric display counter. At any
given time, this display will inform the drivers if there is any vacant slot to park. If green
LED indicator is on, as many slots as the number shown on the display are vacant to park,
but when the red LED is on, no space is available. Figure 1.11 displays schematic diagram
of a 2 digit numerical display circuitry.

Figure 1.11
Sixteen toggle switches to indicate presence of cars parked
Figure 1.12 displays the schematic circuitry of an input device related to one of 16
sensors to be activated when a car is parked in the parking space. When any of these
toggle switched is depressed, the green LED turns off and the red one turns on, to inform
the microcontroller that a car is parked on the corresponding slot.

Figure 1.12

www.ebook777.com
Main board
In the main board, a ATmega16 AVR microcontroller is used to control the program. A
16x1 multiplexer is used for the16 signals that come from parking slots. So just five pins
of microcontroller (four pins are to control multiplexer and one is for multiplexer output)
are used, instead of 16 pins. The microcontroller selects a signal among the 16 signals by
four multiplexer control pins and then reads it from the multiplexer output. Also, two shift
registers are used to have a lot of serially output for microcontroller just using three pins
of microcontroller. The needed pushbutton and a LCD to display useful information are
considered in this board. You can see the PCB layout of this board (Figure 1.13) with
Altium Designer ®, which is a registered trademark of Altiume Company. In addition,
you can see its source file in attendant CD.

Figure 1.13

Figure 1.13 Notes

1 ATmega16

2 DM74150N Multiplexers

3-4 74HC595N shift registers

5 DS1307 is a Serial Real-Time Clock


6 Optocoupler 4n35

Female pin to have easy connect between board and its inputs and
7
outputs

8 Female pin to have easy connect between board and LCD

9 Lithium Battery

10 0.1 uf (104) Ceramic Disk Capacitor

11 CRISTAL QUARTZO 32,768KHZ

12 pins to supply power (5 or 17 v)

13-14 Five small push buttons

15 10 kΩ potentiometer

16
11x10kΩ
to19

20 1x10kΩ and 1x5.6kΩ

www.ebook777.com
Step 4: Uploading the control program into the ATmega16
microcontroller (hex file)
Now that all needed PCBs are manufactured according to Step3, last step before
powering up the system is to 1- assemble the PCBs 2- burn the code developed at step 1
into AVR ATmega16 controller user memory.
I used my little mega16 programmer to upload the hex file developed in step 2 and
complete project final step.

Figure 1.14: The programmer is used to program Atmega16 microcontroller


Chapter 2 : Arduino base parking system

Step 1: The Arduino board


The Arduino board has a nearly limitless array of innovative applications for
everything from robotics and lighting to games etc. It is a fun way to automate everything,
enabling you to control simple devices or manage complex other projects.
We also implemented the above project with Arduino board, by a little different
mechanism (Figure 2.1). When a driver want to drive his car into the parking lot, and if
the parking lot has a vacant space, the input gate is raised and the operator puts a card on a
card reader to record the time and date on it, and give it to the driver. When the driver is
going out, he gives the card back to operator. The operator puts it on the card reader again.
Then, the system computes the elapsed time and its parking cost, and displays it on the
LCD. At this time, the operator can get the pay from the driver and depresses the Paid
pushbutton to open the exit gate. Such as previous project, green and red LEDs display the
state of parking lots. Notice that in this one, the systems doesn’t use16 signals to scan the
state of each parking slot because now the system uses 16 RFID cards to record entrance
time for each car and it does not matter where the cars are parked.
To view this project using an Arduino board in action, click on the following link:
http://www.youtube.com/watch?v=CNt2DIlwrso
As can be seen in the video, two main deference between this and previous projects
are:
1. The usage of Arduino board instead of AVR microcontroller
2. The usage of RFID cards instead of wiring for 16 slots in the parking lot.

Figure 2.1: Parking system with Arduino mega2560 board

www.ebook777.com
RFID cards are:
1. Unlike previous project, by using RFID cards, we do not need wiring from 16 slots to
main board controller. Therefore, the using of RFID cards makes a cheaper system.
2. High security against faking because of using RFID card with encryption technology.
( only the operator can read or write time on the cards)
3. This parking system prevents the stealing of cars, because each car needs a card to
exit.
4. The card reader can be restarted, or exchanged, because the primary data (time and
date of entrance) are recorded on cards.
5. There is an internal clock to compute the time and because of using backup battery, if
the system power turns off for a time, it does not have any effect on the system
performance.
Now you might be able to come up with a circuit diagram such as Figure 1.5 in the
previous chapter. There is no problem to test the circuit and code at this stage, because of
the use of two great software- CodevisionAVR ® and Proteus ® that can help us to
generate code and simulate the circuit respectively. Therefore, at next step, we may go
ahead and start generating program code for Arduino board after putting all components
on the edit screen of the great Proteus software.
Step 2: An overview of Arduino board program
The Arduino software is used to write a program for the Arduino mega2560. Now
we’re going to explain the written program. Any of you can see the program source file in
attendant CD.
As you are now in in the first part of the program, the needed libraries should be
inserted. MFRC522.h library is inserted to the program to use RFID_RC522 card. Then
this card should be configured by the following instruction.
MFRC522 mfrc522(SS_PIN, RST_PIN);

In addition, the LiquidCrystal.h library is inserted to use the LCD. The next program
code configures it in the program.
LiquidCrystal lcd(42, 43, 44, 45, 46, 47);

The DS1307 IC generates exact time in the circuit. It has been connected to Arduino
board with I2C protocol. In our program to setup and computing time, two Wire.h and
DS1307rtc.h libraries are inserted to the program. The following instruction is a typical
command to generate time and date that will be used during the program.
tmElements_t mytm;

To have a better program, some symbols are defined for input and output pins of the
Arduino board.

Inputs Symbols

Paid Paid button

Setpb The Key to modify time

input_sensor The sensor that it detects car

input_up_ms Gate input upper limit limit switch

input_down_ms Gate input lower limit limit switch

output_up_ms The limit switch in lower limit of Gate output

output_down_ms The limit switch in upper limit of gate output

www.ebook777.com
Output symbols

The motor which is responsible to lift up the arm gate at


input_motor_up
entrance of the parking

The motor which is responsible to lift down the arm gate at


input_motor_down
entrance of the parking

The motor which is responsible to lift up the arm gate at exit of


output_motor_up
the parking

The motor which is responsible to lift down the arm gate at exit
output_motor_down
of the parking

SS_PIN Slave select pin to have SPI connection with RFID

RST_PIN Reset pin to have SPI connection with RFID

r_led To turn the red LED on

g_led To turn the green LED on

disp1 - disp8 for controlling two7segments

Buzzer Make sound for valid or invalid card

alarm_r Red LED for invalid card

alarm_g Green LED for valid card


setup() function
As you know, this function is run once time in the program. The following activities
are performed by this function:
1. It configures input and output pins of Arduino board by pinMode instruction.
2. It reads the capacity of parking lot from EEPROM.
3. Reads the time from RTC IC
After setup function, the program will go into loop function. The program will stay in
the loop function and recall some functions, as explained in the following sections.

www.ebook777.com
timing() function
In the Arduino programming to make different times, millis() instruction can be used.
This instruction holds the elapsed time from Arduino starts and give back time in
millisecond format. In the timing function by this instruction, the following times are set:
1. Blinking red LED.
2. The time duration that entrance arm gate stays open.
3. The time duration time that exit arm gate stays open.
4. The time for one beep to notify that the card is valid.
5. The time for three beeps to notify that the card is invalid.
read_gates() function
This function lifts up and down either the entrance or exit arm gates. There are three
conditions to determine the state of each gate. For the entrance gate, the three condition
are :
1. If there is a car in front of input gate and there is a vacant slot in the parking lot, the
entrance arm gate should be lifted up.
2. The entrance arm gate is raised until its related up limit switch is activated. Then the
related motor is turned off. After a specific time (timing function), the motor is
turned on to bring the entrance arm gate down.
3. The motor is being moved down until the related limit switch is activated. Then the
motor is turned off.
There is a similar process for exit arm gate, but this process is started when the
operator depresses the paid key.

www.ebook777.com
led_statuse function
This function determines the state of green and red LEDs. If there is any vacant slot in
the parking lot, the green LED is turned on. Otherwise, the red LED is turned on. The red
LED blinks at a slow speed under this condition, and if a car tries to enter the parking lot,
the red LED starts to blink at a higher speed.
Card_reader() function
This function is to control, read and write data on RC522 RFID card. At first, it store
FF code in key variable because usually the default password for these cards is FF.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++)
key.keyByte[i] = 0xFF;

Then, it checks if there is a card on card reader device or not. If there is no card, exit from
this function
if ( ! mfrc522.PICC_IsNewCardPresent() )
return;

But, if there are some cards on the card reader a card is selected.
if ( ! mfrc522.PICC_ReadCardSerial() )
return;

The following variable defined in the card memeory.


byte sector = 1;
byte valueBlockA = 4;
byte valueBlockB = 5;
byte valueBlockC = 6;
byte trailerBlock = 7;

Then a connection between the card and card reader should be made. It gives the password
(keyA) to the card. If the password is incorrect or the card isn’t valid, it logs out from the
card and then the red LED is turned on and three beeps are sounded and at the end, it exits
the card reader.
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
buzzer_error=1;
previousMillis_buzzer=currentMillis;
digitalWrite(alarm_r,HIGH);
return;
}

Otherwise, read the first block (block A) from card memory.


status = mfrc522.MIFARE_Read(valueBlockA, buffer, &size);

If the fifth byte is zero, it means that the card was not on card reader device before and we
should write on it entrance time and date. After writing hour, minute, day, month and year
on it, set the fifth byte to indicate that the entrance time is written on card memory. Then

www.ebook777.com
Then it decreases the parking capacity by one unit.
if(buffer[5]==0)
{
value1Block[0]=mytm.Minute;
value1Block[1]=mytm.Hour;
value1Block[2]=mytm.Day;
value1Block[3]=mytm.Month;
value1Block[4]=mytm.Year-96;
value1Block[5]=1;
status = mfrc522.MIFARE_Write(valueBlockA, value1Block, 16);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
buzzer_ok=1;
previousMillis_buzzer=currentMillis;
digitalWrite(alarm_g,HIGH);
digitalWrite(buzzer,HIGH);
capacity—;
if(capacity < 0)
capacity=0;
EEPROM.write(0,capacity);
}

But if the fifth byte is one, it means that this card already been used on the card reader
device at the parking entrance, and now it should compute elapsed time and its parking
cost. So, it reads and stores the card variables on the Arduino memory and then resets all
used bytes in card memory. Then, logs out of the card and sounds a beep to indicate that
the process is completed correctly. Here, it recalls Calculate function to compute the total
time and cost.
if(buffer[5]==1)
{
for(byte i=0;i<16;i++)
value1Block[i]=0;
status = mfrc522.MIFARE_Write(valueBlockA, value1Block, 16);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
buzzer_ok=1;
previousMillis_buzzer=currentMillis;
digitalWrite(alarm_g,HIGH);
digitalWrite(buzzer,HIGH);
calculate();
}

www.ebook777.com
Calculate() function
This function, based on the current time and the stored time from card, computes the
elapsed time and then the parking charge, according the following table.

From 1 hour From 3h More than


Less than one hour
until 3 hour until 24h 24 hour

40 ¢ for entering cost and one ¢


One dollar 3 dollar 6 dollar
for each minute

It shows the elapsed time and the cost on the LCD, and then it adds one unit to the
capacity of parking lot and stores the new parking capacity on EEPROM.
Show_lcd
This function write the time and date on LCD as shown in the following figure:

Figure 2.2

www.ebook777.com
set_key(), up_pb(), down-pb() functions
These three functions are for tuning time and date. With the set_key, we can choose
any appropriate value of time or date. Up_pb and down_pb are considered external
interrupts and when they are active, we can increase the value of time or date that is
selected by set_key. You can see the related programs in attendant CD.
Step 3: Getting to manufacturing the PCBs of the prototype model
parking systems
It was very encouraging to see that simulation was carried out successfully and now
we wish to get the PCBs built and test the lift system functionality manually in the first
place. This might be also related to a project that we are working on it as a collage final
year project and our instructors have advised us the course grade will not be give in full
unless we show them the whole hardware of the system working properly!
No sweat again. Some other big brothers already have developed great software that
their application allows us to design and generate the PCB layout of our circuits. Altium
Designer ® which is a registered trademark of Altiume Company is a sample of that
software that can be used to create PCBs of any complexity.
It seems that the all components for this project have been designed in the previous
chapter and now we just need to design a main controller board with Arduino board
(Figure 2.3) instead of AVR microcontroller. In addition, here, we do not need the 16
signals from 16 slots in parking lot, and we use RFID cards and RFID_RC522 (Figure
2.4) module on main controller board instead.

Figure 2.3: Arduino mega2560 board

Figure 2.4: RFID_RC522


Such as previous chapter, to have a better connection with Arduino board, it’s better to
design a PCB board that consists of needed push buttons, buzzer, LCD, RC522 to read
cards, etc. and assemble them on the Arduino board. In Figure 2.5 you can see our
designed PCB board with the Altium designer software. Also you can see the source file
in attendant CD, if you want to build a similar one for yourself.

www.ebook777.com
Figure 2.5

Figure 2.5 Notes

1 RC522-RFID

2 LCD 2x16

male pin header connectors to help to wire controller I/O signals to the
3
Arduinomega2560 board

4 Optocoupler 4n35

5 DS1307 is a Real-Time Clock

6 Lithium Battery

7 Buzzer for making beep sound


8 NPN-transistor(KSP-222A0)

9 CRISTAL QUARTZO 32,768KHZ

10 0.1 uf (104) Ceramic Disk Capacitor

11-
Five small push buttons
12-13

14 Female pin for power supply

15 10 K ohm pot

16 1x330Ω - 4x10kΩ - 7x10kΩ and 1x5.6kΩ -


to21 3x3.3k Ω, 3x1.6k Ω,2x10k Ω and 1x330k Ω - 1x1k Ω – 2x1k Ω resistors

22 Two green, Red LEDs

23 Two 1N4007 diodes

www.ebook777.com
Figure 2.6
Step 4: Uploading the control program into the Arduino board
One great thing I like about Arduino board is that you do not need to have a
programmer to upload your developed code. If this is the first time you are using an
Arduino board for building your project, there is a lot of information on the internet to
teach you how to do it, or you might try the following link for example:
http://www.ladyada.net/learn/arduino/lesson1.html

www.ebook777.com
Conclusion

Now you have the information you need to become a successful Microcontroller
programmer. This is a moment for you to take the information you’ve been given and not
be afraid to put it to use.
The primary thing that keeps people from becoming successful is fear. If you can
overcome this fear then you will succeed and prosper.
All you need to do at this point is to stop putting things off and just do it. Start
working and you will find it becomes easier as you go along.
Once you accomplish any new skill or task, you will realize just how easy it is. That is
when you want to kick yourself for not starting sooner.
So don’t hesitate. Start right now. Start today. Break that cycle of doubt and enjoy your
newfound skills and succeed in exploiting them.
To your success and happiness,
Seyedreza
Bibliography

Some more information on a Break-Beam Sensor:


http://jumperone.com/2011/11/break-beam-sensor/
http://www.zen22142.zen.co.uk/Circuits/Power/power.htm
http://www.elprocus.com/rfid-based-paid-car-parking-system/
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=50367&p=393053

www.ebook777.com
My e-book product list

www.plc-doc.com
Hardware, software and more
We would like to introduce our Teach Yourself Consumer PLC books as an excellent
resource for readers who want to learn more about PLCs. So far, we have launched this
groundbreaking new series with four exciting titles:
1. Programming with SIMATIC S7 300/400 Programming Controllers
2. Programming with SIMATIC S7-200 PLCs using STEP 7-Micro/win software
3. Introduction to programming a SIEMENS LOGO! PLC
3. Basics of Programmable Logic Controllers (PLC)
4. Programming Allen Bradley series of PLCs using RSLogix 500 software compiler
5. Introduction to programming Allen-Bradley series of PLCs (Second Edition)
Design and Implementation of typical end year college projects using a PLC with
youtube.com site links to watch the projects in action:
1. Programming a 4 floor elevator with SIMATIC STEP 7 and LOGO! SoftComfort
http://youtu.be/dqkeHD5WNcc
2. Programming SIEMENS LOGO! PLC as a dyeing machine temperature controller
http://youtu.be/mJfT4z1oCeo
3. Programming a Traffic Light Controller system using SIMATIC S7-300 & HMI
display
http://youtu.be/0ADfFPOzIUE
4. Programming a PLC based car Parking System with SIMATIC S7-300 PLC
http://youtu.be/51oqLRxXcHk
5. Implementing a PLC-based temperature controller with PID algorithm
http://youtu.be/yhSyKObMlgA
6. Design and implementation of an 8 Floor Elevator Control System with a PLC
http://youtu.be/M3qgxkEIDw8
Design and Implementation of typical end year college projects using AVR
Microcontroller\the Arduino board based controllers with youtube.com site links to
watch the projects in action:
1. Simple Arduino based 4 Floor Elevator Project
http://youtu.be/WU2arv-yghw
1(A). Microcontroller based 4 Floor Elevator System
http://youtu.be/UQbGcntcVP4
2. Implementing a Traffic Light Controller System using Arduino
http://youtu.be/f-ZbP8juB20
2(A). Programming a TRAFFIC LIGHT CONTROL system using an AVR
microcontroller
http://youtu.be/q0eWsX2kMeU
3. Simple Arduino based Automated Parking lot System
http://youtu.be/CNt2DIlwrso
3(A). Design and implementation of a Microcontroller-Based Car Parking System
http://youtu.be/7aIp2iCC_mU
4. Implementing an Arduino based temperature controller with PID algorithm
http://youtu.be/i2nxqLs9wBg
4(A). Temperature controller and monitoring with a microcontroller
http://youtu.be/mIyixludYaE
5. Analog LED clock with chime, alarm, calendar and temperature display (not
published yet)
http://youtu.be/Mi5zAdctp4g
Free microcontroller \Arduino based project Downloads from our www.plc-doc.com
or www.plcgoods.net websites:
1. How to control a Stepper motor with an ATmega8 microcontroller
http://youtu.be/XBWCsl512Lc
2. MATLAB/PC and Microcontroller based advanced Line following Robot
http://youtu.be/2Y_qSzKL_2Y
3. 2 Zones microcontroller based weekly digital timer
http://youtu.be/xbIEKI3qqNM

www.ebook777.com
www.ebook777.com

You might also like