Arduino RFID Door Lock

 by pcmofo
Contest WinnerFeatured

Step 4: Program!

Time to program your arduino. This can be a bit tricky using a basic arduino, you may have to press the reset button multiple times before and during the first part of the upload. A very important thing to remember, you WILL get an upload error if you do no temporarily disconnect the ID-20 serial line to the arduino's Rx line. The ATMega168 only has 1 Rx input and it uses it to upload code to talk to the programmer. Disconnect the ID-20 while programming then plug it back in when your done. I used a FTDI programmer which allows you to program the arduino via USB with only 4 wires. The Controller schematic shows a pin header connection to allow you to plug one in directly. Sparkfun also sells this part but many may already have it.

You can easily upload my code to your arduino and never look back but whats the fun in that? Let me explain the basic idea of how it works.

First of all, I did not want any external buttons/switches/etc and I did not want to reprogram the arduino every time I wanted to add a new card. Therefore I wanted to use only RFID to control the operation of the circuit as well as control over the door lock.

The program turns on the Blue LED to indicate it is ready to read a new card. When the card is read it decides if it is a valid card or not by comparing what it read in to a list of valid cards. If the user is valid, the arduino turns OFF the Blue LED and turns on the Green LED for 5 seconds. It also turns on another output high for 5 seconds. This output is connected to the TIP31A transistor and allows the tiny arduino to control a much larger 12v 300mA door lock without being damaged. After 5 seconds the door lock re-locks and the LED turns back to blue to wait for another card to be read. If the card is invalid then the LED changes to RED for a few seconds and back to Blue to wait for another card. 

It is important that the door lock still work even if the arduino loses power overnight or is reset. Therefore all valid card ID's are stored in EEPROM memory. The ATMega168 has 512 Bytes of EEPROM memory. Each RFID card has a 5 Hex Byte serial number and a 1 Hex Byte Check sum that we can use to verify there were no errors in the transmission between the ID-20 and the arduino.

Valid cards are stored in the EEPROM by using the first Byte as a counter. For example, if there are 3 valid cards stored the first Byte in the EEPROM would be 3. EEPROM.read(0); = 3. Knowing this, and the fact that each ID is 5 Bytes long we know that 1-5 is card one, 6-10 is card 2 and 11-15 is card 3. We can make a loop that looks through the EEPROM 5 bytes at a time and tries to find the card that was read in by the reader.

But how can we add new cards to the EEPROM after the circuit is installed?? I have read in one of the RFID cards I have and hard coded it to be the Master RFID card. So even if the entire EEPROM is wiped the master card will still function. Whenever a card is read, it checks first to see if it is the Master card, if not, then it continues to see if it is a valid card or not. If the card is the master card we have the arduino go into a "programming mode" where it flashes RGB and waits for another valid tag to be read. The next tag that is read is added to the next free spot in the EEPROM and the counter is incremented 1 if the card does not already exist in the EEPROM memory. The reader then returns to normal mode and waits for a new card to be read.

Currently I have not programmed a way to delete a card as the reasons for deleting a card would most likely be it was lost or stolen. As this would most likely be used with 1-10 people the easiest thing to do would be to hard program a Master Erase card that will wipe all cards from the EEPROM then re add them all, which only takes a few seconds. I have added code to wipe the EEPROM but I have not implemented this feature yet. .

The code is attached in a text file along with a copy of the parts list.

 
Remove these adsRemove these ads by Signing Up
weekendtech says: Nov 8, 2011. 5:01 PM
How do i fix the "eeprom was not declared in this scope" error
I am doing this for a college project and have never used arduino before we are building it to interface it with computer help would really be appreciated is there part of the code i am missing?
loizosgroutas in reply to weekendtechFeb 29, 2012. 6:02 AM
I had the same problem. To fixed is easy. paste at the firt line
#include // Needed to write to EEPROM storage
this .
Now look like this.
#include // Needed to write to EEPROM storage.

Sorry for my english.
Jsmxbox says: Oct 30, 2011. 3:27 PM
Hey im getting a problem i have 4 RFID keyrings they all work and i can read them all fine but the problem is 2 of them start with 00 00 and will not work as master, delete or wipe but the other 2 start with 00 01 and work without problems any reason why this would happen?

many thanks

Jamie!
Jsmxbox in reply to JsmxboxOct 30, 2011. 3:34 PM
Also found a floor in it too being that if you scan a key that ain't stored in the EEprom more than 3 times it still unlocks the "door"
nmstar56 says: Jul 28, 2011. 8:04 AM
I'm getting "error: 'EEPROM' was not declared in this scope" for "EEPROM.write(i, 0);"

Please help.
TrueHybridX in reply to nmstar56Aug 14, 2011. 4:17 PM
did you include the eeprom header files?
natsud1 says: Jun 16, 2011. 2:32 AM
Hi i have an Arduino328 and a parallax rfid reader but i dont really know how to write code although im trying to learn my first problem is that when i load the code and run it the blue led is not on but the other 2 are so i looked at your "normalModeOn" part and it looks to me that it is exactly backward of what i thought it should be since you have the "powerpin" high and the rest low and the tricolor led has one + and three - electrodes this seemed backward .all i have hooked up is the reader and the tricolor led if i reverse the normalModeOn from high to low and the lows to high i get the blue led so i then swiped a card and nothing happensi cant seem to figure out what to do or what to change is it because of the different arduino and reader or am i just supposed to figure out where the code needs to be changed for theses Pleas Help i dont know much about code
natsud1 in reply to natsud1Jun 16, 2011. 3:32 AM
OK so its been another 3 hrs but i made some progress i had a different rfid code that would read the card and diaplay it in the serial monitor and i found this "if((val = Serial.read()) ==10)" was different than your code yours was: "if((val = Serial.read()) ==2)" so i changed all instances to the number 10 and i got it to make the led blink odd color purpleish .. like i asked before about the led out highs and lows being reversed in the void sections well i reversed all of them and now when i swipe my non master/non saved card it turns red and when i swipe my master it goes to the program blinky sequence and it grants access to the former card when i reswipe it so i think i got some of it working but im not sure about the door lock part i have an led stood in place of the lock but it doesnt light up yet ill let you know if i get anywhere else on it sorry about the long comments this is a great 'ible im sure all my problems stem from lack of knowledge of code and other stuff hope what i said made since and maybe helps somone else
joshnosh says: Mar 15, 2011. 10:51 AM
hey
im getting a
"eeprom was not declared in this scope" error
any way to fix this im stuck
it refrences the "EEPROM.write(i, 0);" line about a 5th off the way down
Jessecar96 says: Jun 26, 2010. 8:14 AM
The code comes up with an error for me: In function 'void setup()': error: redefinition of 'void setup()' In function 'void loop()': what do i do?
Jessecar96 in reply to Jessecar96Jun 26, 2010. 8:16 AM
I am using an Atmega 328 instead of a 128. That might be the problem? I so, what do i do?
gregt in reply to Jessecar96Mar 4, 2011. 2:48 PM
I tried using an Arduino Pro, which has a ATMega328 processor, and it would not read anything from the ID-20. I then built a homebrew Arduino using a ATMega168, and it worked perfectly. I thought the chip wouldn't make any difference either, but those were my results.
chrisgward in reply to Jessecar96Jun 26, 2010. 7:07 PM
The 328 is v. likely to not be the problem. In the Arduino environment, if there is more than one tab, under the compile etc. buttons, close all except the one that contains the door lock code. Otherwise, in that same piece of code, you may have defined the setup void twice. To fix this, all you have to do is just locate the second void setup() and rename or delete it, just be careful, as it may ruin your code, so also update any references to it in the code. Hope this helps!
joshnosh says: Oct 8, 2010. 5:12 AM
hey im going to build one for my collage project and it looks AWSUM
but i cant figger out how to program the chip
you said somthing about only needing 4 wires
where can i get an "ftdi programer"?
and wher do i connect it?
iv got plenty of skill with electronics but this is my first audrino project and im strugleing a bit
thanks guys
KadenP in reply to joshnoshDec 5, 2010. 2:22 PM
He WAS helping. If you learn to spell properly, Google will help you. Try searching for "ftdi programmer" and you'll be flooded with information. "ftdi programer"... eh.. not so much.
joshnosh in reply to KadenPJan 26, 2011. 8:23 AM
well im sorry for being dyslexic
i already searched for "ftdi programmer"
and i was flooded with information but not much off it was relevant
im going to buy a audrino board with removable chip and use the board to program it
thanks anyway!
blister75 in reply to joshnoshNov 3, 2010. 2:22 PM
the best place i have found to purchase a ftdi cable/board is Sparkfun.com.
pcmofo (author) in reply to joshnoshOct 8, 2010. 6:09 AM
Please tell me your taking "English" as one of tour college courses.
joshnosh in reply to pcmofoOct 9, 2010. 3:16 AM
if your not going to help why even comment?
there is a "be nice" policy remeber
chieffancypants says: Oct 26, 2010. 9:49 PM
Why is readCard / storedCard 6 bytes, when according to the datasheet, it only needs 5. 4 bytes used for the ID, and one byte for the checksum.

Thanks! Great post!
pingywon says: May 3, 2010. 11:21 AM
Great system for getting in the door, but how do you get out? Do you really unlock the knob and slam the door behind you? Seems a bit analog to me :)

Have you thought about adding a "release" button from the inside that would allow anyone to exit?
mayson in reply to pingywonSep 5, 2010. 6:45 AM
You could easily add a switch with a jumper from the Vdd over the transistor to the electric door. This would unlock the door when you hold down the button (momentary) normally open
acherman says: Aug 4, 2010. 7:33 PM
Thanks for the updated code. I will play around with it tomorrow. Again, great work!!
CCOTTER247 says: Aug 4, 2010. 4:53 PM
I am having a hard time with the code. Could you please make a diagram showing where to put the master card number. -Thanks
nate4alpha says: May 6, 2010. 6:06 PM
I noticed that an <EEPROM.h> library is being included in the provide code, but where is the .h file located. Is the .h file available or is that code I have to develop myself?
malvika in reply to nate4alphaMay 11, 2010. 4:04 AM
e'ven i m getting the same warning. i m also getting one error which tells that " ; " missing before programmode in line 54 ( boolean programmode = false;)
tehjrow says: May 10, 2010. 12:09 PM
If the RFID starts with 00 (one of mine does) it will not compare it correctly because of this: 

if ( a[0] != NULL  )
match = true;

Since 00 would be seen as NULL, match never gets set to true.  Took me and the Arduino forum guys a few hours to figure it out.
bobbob says: Mar 21, 2010. 8:39 AM
Would this code work for the Parallax RFID reader?
pcmofo (author) in reply to bobbobMar 22, 2010. 8:21 AM
 It should. It may require some modification as the parallax rfid reader may not transmit data in the same format. The same basic code will work though. At the least, my code will be a good place to start. 
bobbob in reply to pcmofoMar 28, 2010. 3:43 PM
Is it possible for you to make the changes and post the code? I'm new to arduino and I have NO clue what I have to change...I've been messing with it for a while and still nothing...

thanks,
Elijah
pcmofo (author) in reply to bobbobApr 2, 2010. 7:46 AM
 Half the fun of an electronics project is learning and doing yourself. I struggled with this project at first as well. Your best bet is to take things one step at a time. Get some Parallax example code or find a tutorial on where to start. Once you can read input from the RFID then your half way home. Use my code as a reference to write your own. It should have enough comments so that you can understand what each part does.
M4industries in reply to pcmofoMar 25, 2010. 5:14 PM
 It wont require much, the output for the reader is 2400 Baud serial.
kjpace34 says: Jan 25, 2010. 2:52 PM
How do you read in a card in order to hard code it? 
hivoltage in reply to kjpace34Jan 30, 2010. 8:26 PM
 program the arduino with the above code, connect the rfid reader to it, then plug it into the computer and start the serial monitor in the arduino programmer. swipe a card near the reader and its ID number will appear in the serial monitor window. then just copy the number back into the "ismaster" function, recompile, and upload it to your board and the card is now a master card!
pcmofo (author) in reply to hivoltageJan 30, 2010. 11:04 PM
 Looks like I have been missing some comments. Thanks Hivoltage for answering them for me, and accurately too!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!