RFID Based Toll Collection System

Published  June 25, 2015   6
Dilip Raja
Author
RFID Based Toll Plaza System using AVR Microcontroller

We know in offices, shopping malls and in many other places where only the person with authorization card is allowed to enter the room. These systems use RFID communication system. RFID is used in shopping malls to stop theft as the products are tagged with RFID chip and when a person leaves the building with the RFID chip an alarm is raised automatically. The RFID tag is designed as small as part of sand. The RFID authentication systems are easy to design and are cheap in cost. Some schools and colleges nowadays use RFID based attendance systems.

 

In this project we are going to design a RFID based toll collection system for security purposes. So this system open gates and allow people only with the authorized RFID tags. The authorized tags holder ID’s are programmed in to the ATMEGA Microcontroller and only those holders are allowed to leave or enter the premises.

 

Components Required

Hardware: ATmega32 microcontroller, power supply (5v), AVR-ISP Programmer, JHD_162ALCD (16x2 LCD module), 100uF capacitor (connected across power supply), button, 10KΩ resistor, 100nF capacitor, LED (two pieces), EM-18 (RFID reader module), L293D motor driver IC, 5V DC motor.

Software: Atmel studio 6.1, progisp or flash magic.

 

Circuit Diagram and Working Explanation

RFID Based Toll Plaza Circuit Diagram

In the RFID toll collection system circuit shown above, PORTA of ATMEGA32 is connected to data port of LCD. Here we should remember to disable the JTAG communication in PORTC to ATMEGA by changing the fuse bytes, if we wans to use the PORTC as a normal communication port. In 16x2 LCD, there are 16 pins over all if there is a back light, if there is no back light there will be 14 pins. We can power or leave the back light pins. Now in the 14 pins there are 8 data pins (7-14 or D0-D7), 2 power supply pins (1&2 or VSS & VDD or gnd & +5v), 3rd pin for contrast control (VEE-controls how thick the characters should be shown), 3 control pins (RS & RW & E).

 

In the circuit, you can observe that I have only took two control pins. This gives the flexibility of better understanding. The contrast bit and READ/WRITE are not often used so they can be shorted to ground. This puts LCD in highest contrast and read mode. We just need to control ENABLE and RS pins to send characters and data accordingly.

 

The connections which are made for LCD, are given below:

PIN1 or VSS to ground

PIN2 or VDD or VCC to +5v power

PIN3 or VEE to ground (gives maximum contrast best for a beginner)

PIN4 or RS (Register Selection) to PD6 of MCU

PIN5 or RW (Read/Write) to ground (puts LCD in read mode eases the communication for user)

PIN6 or E (Enable) to PD5 of Microcontroller

PIN7 or D0 to PA0

PIN8 or D1 to PA1

PIN9 or D2 to PA2

PIN10 or D3 to PA3

PIN11 or D4 to PA4

PIN12 or D5 to PA5

PIN13 or D6 to PA6

PIN14 or D7 to PA7

 

In the circuit, you can see we have used 8bit communication (D0-D7). However this is not a compulsory and we can use 4bit communication (D4-D7) but with 4 bit communication program becomes a bit complex, so I preferred 8 bit communication.

 

So from mere observation of above table we are connecting 10 pins of LCD to controller in which 8 pins are data pins and 2 pins for control.

 

Before moving ahead, we need to understand about the serial communication. The RFID module here sends data to the controller in serial. It has other mode of communication but for easy communication we are choosing RS232. The RS232 pin of module is connected to RXD pin of ATMEGA.

 

The data sent by the RFID module goes as:

RS232 Interface Format

 

Now for the RFID module interface, the following features are required:

1. The RXD pin (data receiving feature) of controller must be enabled.

2. Since the communication is serial we need to know whenever the data bye is received, so that we can stop the program until complete byte is received. This is done by enabling a data receive complete interrupt.

3. RFID sends data to controller in 8bit mode. So two characters will be sent to the controller at a time. This is shown in above figure.

4. From above figure, there are no parity bits, one stop bit in the data sent by the module.

 

The above features are set in the controller registers; we are going to discuss them briefly,

USART Control and Status Registers B

USART Control and Status Register C

RED (RXEN): This bit represents receive data feature. This bit must be set for the data from the module to be received by the controller. It also enables RXD pin of controller.

BROWN (RXCIE): This bit must be set for getting an interrupt after successful data reception. By enabling this bit we get to know, right after 8 bit data receive.

PINK (URSEL): This bit must be set before enable other bits in UCSRC. After setting, other needed bits in UCSRC, URSEL must be disabled or put to zero.

YELLOW (UCSZ0,UCSZ1,UCSZ2):These three bits are used for selecting the number of data bits we are receiving or sending in a single go.

UCSZ Bits Settings

Since the data sent by RFID module is 8bit data type, we have to set UCSZ0, UCSZ1 to one and UCSZ2 to zero.

ORANGE (UMSEL): This bit is set based on whether the system is communicating asynchronously (both use different clock) or synchronously (both use same clock).

UMSEL Bit Settings

Since module and controller use different clock, this bit must be set to zero or left alone as they are all set to zero by default.

GREEN (UPM1, UPM0): These two bits are adjusted based on bit parity we are using in communication.

UPM Bits Settings

Since RFID module sends data with no parity, we have set both UPM1, UPM0 to zero or they can be left alone as all bits in any registers are set to zero by default.

BLUE (USBS): This bit is used for choosing the number of stop bits we are using during communication.

USBS Bit Settings

Since RFID module sends data with one stop bit, we just have to leave USBS bit alone.

Now at last we need to set the baud rate, from figure above it is clear that the RFID module sends data to controller with a baud rate of 9600bps (bits per second).

The baud rate is set in controller by choosing the appropriate UBRRH.

USART Baud Rate Registers

The UBRRH value is chosen by cross referring baud rate and CPU crystal frequency, So by cross reference UBRR value is seen as ‘6’, and so the baud rate is set.

UBRR Settings

 

Now as shown in figure, two pins from controller goes to L293D, which is a H-BRIDGE used to control speed and direction of rotation for low power DC motors.

L293D Motor Driver

L293D is an H-BRIDGE IC designed for driving low power DC motors and is shown in figure, this IC consists two h-bridges and so it can drive two DC motors. So this IC can be used to drive robot motors from the signals of microcontroller.

 

Now as discussed before this IC has ability to change the direction of rotation of DC motor. This is achieved by controlling the voltage levels at INPUT1 and INPUT2.

Enable Pin

Input Pin 1

Input Pin 2

Motor Direction

High

Low

High

Turn Right

High

High

Low

Turn Left

High

Low

Low

Stop

High

High

High

Stop

 

 

So as shown in above table, for clock wise rotation 2A should be high and 1A should be low. Similarly for anti clockwise 1A should be high and 2A should be low. 

 

Whenever an authorized card is brought near the module, the motor is programmed to move clockwise for a second, to show the toll gate is opened after a second it comes back, telling the toll gate is closed. The working of toll plaza is best explained in step by step of C code give below.

 

Programming Explanation

Below is the line to line explanation for the code of RFID Toll Collection System. You can understand the concept and working of this project by reading the code below. To download or copy, you can find the complete code at the bottom of page.

 

 #include <avr/io.h> // header to enable data flow control over pins

#define F_CPU 1000000  // telling controller crystal frequency attached

#include <util/delay.h> // header to enable delay function in program

#define    E   5 // giving name “enable”  to 5th pin of PORTD, since it is connected to LCD enable pin

#define RS  6 // giving name “registerselection” to 6th pin of PORTD, since it is connected to LCD RS pin

void send_a_command(unsigned char command);

void send_a_character(unsigned char character);

void send_a_string(char *string_of_characters);

int main(void)

{

DDRA = 0xFF;   // putting porta as output pins

DDRD = 0b11111110;

 _delay_ms(50);//giving delay of 50ms

DDRB = 0b11110000; // Taking some portB pins  as input.

UCSRB |=(1<<RXEN)|(1<<RXCIE); //enabling data receive complete interrupt,enabling data receive pin

UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); //changing other bits by first setting URSEL, setting for 8 bit communication

  UCSRC &=~(1<<UMSEL); //using asynchronous communication

 UBRRH &=~(1<<URSEL);

UBRRL=6;//setting the baud rate  // The following contains ID of tags, these must be changed for different tags, These must be updated for the project to work

/* After dumping the program in the controller one must take the cards which must be authorized and get the tags ID. These are obtained by placing the tag near RFID module and the ID will be shown on the screen. After getting the IDs, the program must be updated by replacing the below ID numbers with new ID numbers.

char ADMIT [5][4]={{(0x97),(0xa1),(0x90),(0x92)},{(0x97),(0xa1),(0x90),(0x93)},{(0x97),(0xa1),(0x90),(0x94)},{(0x97),(0xa1),(0x90),(0x95)},{(0x97),(0xa1),(0x90),(0x96)}};

Now in above we are authorizing only five cards, these can be changed to any number.

For example consider the default program is dumped in the controller, get the cards which should be authorized. Place one after other near module, you will get the ID for each one as xxxxxxxx (907a4F87),

If there are 7 tags we have, then we will have 7 eight bit ID. */

//now for seven cards it goes as

//char ADMIT[7][4]={{(0x90),(0x7a),(0x4F),(0x87)},[},{},{},{},{},{}}

//Now once all the IDs are written, those ID tags are considered as authorized

Int16_t COUNTA = 0; //allotting memory for storing ID send by module

char SHOWA [4]; // allotting memory for showing the ID send by module

int i=0;

int vote =0;

int k =0;

send_a_command(0x01); // Clear Screen 0x01 = 00000001

_delay_ms(50);

send_a_command(0x38); //telling lcd we are using 8bit command /data mode

_delay_ms(50);

send_a_command(0b00001111); // LCD SCREEN ON and courser blinking

char MEM[4]; // allotting memory to store complete ID of tag

send_a_string ("RFID NUMBER"); // sending string

send_a_command(0x80 + 0x40 + 0); // moving courser to second line

while(1)

{

while(!(UCSRA&(1<<RXC))); // wait till first eight bit data is received

{

}

COUNTA=UDR; // UDR stores the eight bit data received and is taken into an integer.

MEM[0]=COUNTA; // first two characters are updated to memory

itoa(COUNTA,SHOWA,16); //command for putting variable number in LCD(variable number, in which character to replace, which base is variable(ten here as we are counting number in base10))

send_a_string(SHOWA); // telling the display to show character(replaced by variable number) of second person after positioning the courser on LCD

while(!(UCSRA&(1<<RXC)));//wait till second eight bit data is received

{

}

COUNTA=UDR;

itoa(COUNTA,SHOWA,16);

send_a_string(SHOWA);

MEM[1]=COUNTA; // third and fourth characters are updated to memory

while(!(UCSRA&(1<<RXC))); // wait till third eight bit data is received

{

}

COUNTA=UDR;

itoa(COUNTA,SHOWA,16);

send_a_string(SHOWA);

MEM[2]=COUNTA;// fifth and sixth  characters are updated to memory

while(!(UCSRA&(1<<RXC))); //wait till fourth eight bit data is received

{

}

COUNTA=UDR;

itoa(COUNTA,SHOWA,16);

send_a_string(SHOWA);

MEM[3]=COUNTA;// seventh and eight characters are updated to memory

send_a_string("        ");

send_a_command(0x80 + 0x40 + 0);

UCSRB &=~(1<<RXEN);//disabling the receive data until the tag ID is checked for  authorization

for (i=0;i<5;i++)

{

if ((MEM[0]==ADMIT[i][0])&(MEM[1]==ADMIT[i][1])&(MEM[2]==ADMIT[i][2])&(MEM[3]==ADMIT[i][3]))

{   // checking for authorization buy comparing two characters at a time with the characters in the memory

PORTB|=(1<<PINB3); // if authorized turn the LED ON

PORTB&=~(1<<PINB0); // motor moves clockwise

 _delay_ms(220);//delay

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

PORTB|=(1<<PINB0);//motor clockwise movement stop

PORTB&=~(1<<PINB1);//motor anti clockwise movement start

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

_delay_ms(220);

PORTB&=~(1<<PINB3); //motor anti clockwise rotation stop

PORTB|=(1<<PINB1); //authorization LED off

}

}

UCSRB |=(1<<RXEN); // enabling receive data from module

}

}

void send_a_command(unsigned char command)

{

PORTA = command;

PORTD &= ~ (1<<RS); // putting 0 in RS to tell lcd we are sending command

PORTD |= 1<<E; // telling lcd to receive command /data at the port

_delay_ms(50);

PORTD &= ~1<<E; // telling lcd we completed sending data

PORTA= 0;

}

void send_a_character(unsigned char character)

{

PORTA= character;

PORTD |= 1<<RS; // telling LCD we are sending data not commands

PORTD |= 1<<E;//telling LCD to start receiving command/data

_delay_ms(50);

PORTD &= ~1<<E;//telling lcd we completed sending data/command

PORTA = 0;

}

void send_a_string(char *string_of_characters)

{

while(*string_of_characters > 0)

{

send_a_character(*string_of_characters++);

}

}

Code

#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>
#include <stdlib.h>

#define enable            5
#define registerselection 6

void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);

int main(void)
{
    DDRA = 0xFF;
    DDRB = 0b11111111;
    PORTB|=(1<<PINB0)|(1<<PINB1);
    DDRD = 0b11111110;
    _delay_ms(50);
    
    UCSRB |=(1<<RXEN)|(1<<RXCIE);
    UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
    UCSRC &=~(1<<UMSEL);
    UBRRH &=~(1<<URSEL);
    UBRRL=6;
    
    char ADMIT [5][4]={{(0x97),(0xa1),(0x90),(0x92)},{(0x97),(0xa1),(0x90),(0x93)},{(0x97),(0xa1),(0x90),(0x94)},{(0x97),(0xa1),(0x90),(0x95)},{(0x97),(0xa1),(0x90),(0x96)}};
    int16_t COUNTA = 0;
    char SHOWA [4];
    int i=0;

    send_a_command(0x01); //Clear Screen 0x01 = 00000001
    _delay_ms(50);
    send_a_command(0x38);
    _delay_ms(50);
    send_a_command(0b00001111);
    _delay_ms(50);
    char MEM[4];
    send_a_string ("RFID NUMBER");
    send_a_command(0x80 + 0x40 + 0);
    while(1)
    {
        while(!(UCSRA&(1<<RXC)));
        {
            
        }
        COUNTA=UDR;
        MEM[0]=COUNTA;
        itoa(COUNTA,SHOWA,16);
        send_a_string(SHOWA);
        
        while(!(UCSRA&(1<<RXC)));
        {
            
        }
        COUNTA=UDR;
        itoa(COUNTA,SHOWA,16);
        send_a_string(SHOWA);
        MEM[1]=COUNTA;
        
        while(!(UCSRA&(1<<RXC)));
        {
            
        }
        COUNTA=UDR;
        itoa(COUNTA,SHOWA,16);
        send_a_string(SHOWA);
        MEM[2]=COUNTA;
        while(!(UCSRA&(1<<RXC)));
        {
            
        }
        COUNTA=UDR;
        itoa(COUNTA,SHOWA,16);
        send_a_string(SHOWA);
        MEM[3]=COUNTA;
        
        send_a_string("        ");

        
        send_a_command(0x80 + 0x40 + 0);
        
        UCSRB &=~(1<<RXEN);
        for (i=0;i<5;i++)
        {
            
                if ((MEM[0]==ADMIT[i][0])&(MEM[1]==ADMIT[i][1])&(MEM[2]==ADMIT[i][2])&(MEM[3]==ADMIT[i][3]))
                {
                    PORTB|=(1<<PINB3);
                    PORTB&=~(1<<PINB0);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    PORTB|=(1<<PINB0);
                    PORTB&=~(1<<PINB1);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    _delay_ms(220);
                    PORTB&=~(1<<PINB3);
                    PORTB|=(1<<PINB1);
                }
        }
        UCSRB |=(1<<RXEN);
    }
}

void send_a_command(unsigned char command)
{
    PORTA = command;
    PORTD &= ~ (1<<registerselection);
    PORTD |= 1<<enable;
    _delay_ms(20);
    PORTD &= ~1<<enable;
    PORTA = 0;
}

void send_a_character(unsigned char character)
{
    PORTA = character;
    PORTD |= 1<<registerselection;
    PORTD |= 1<<enable;
    _delay_ms(20);
    PORTD &= ~1<<enable;
    PORTA = 0;
}
void send_a_string(char *string_of_characters)
{
    while(*string_of_characters > 0)
    {
        send_a_character(*string_of_characters++);
    }
}

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by Shailendra kumar on Tue, 02/21/2017 - 00:00

Permalink

The description is well explained.this is very useful project mostly to use on toll tax collection booth.
Regard,
Shailendra kumar

Submitted by Aj on Sat, 01/13/2018 - 02:46

Permalink

Can u give code for 8051 and also steper motor

Thanx

Submitted by Naveen Kumar on Sun, 02/04/2018 - 19:32

Permalink

Do we need some external memory to store the details about the balance or any information about the tag details.