3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino RFID Door Lock

Step 4Program!

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.

« Previous StepDownload PDFView All StepsNext Step »
28 comments
Nov 8, 2011. 5:01 PMweekendtech says:
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?
Feb 29, 2012. 6:02 AMloizosgroutas says:
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.
Oct 30, 2011. 3:27 PMJsmxbox says:
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!
Oct 30, 2011. 3:34 PMJsmxbox says:
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"
Jul 28, 2011. 8:04 AMnmstar56 says:
I'm getting "error: 'EEPROM' was not declared in this scope" for "EEPROM.write(i, 0);"

Please help.
Aug 14, 2011. 4:17 PMTrueHybridX says:
did you include the eeprom header files?
Jun 16, 2011. 2:32 AMnatsud1 says:
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
Jun 16, 2011. 3:32 AMnatsud1 says:
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
Mar 15, 2011. 10:51 AMjoshnosh says:
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
Jun 26, 2010. 8:14 AMJessecar96 says:
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?
Jun 26, 2010. 8:16 AMJessecar96 says:
I am using an Atmega 328 instead of a 128. That might be the problem? I so, what do i do?
Mar 4, 2011. 2:48 PMgregt says:
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.
Jun 26, 2010. 7:07 PMTechno Dancer says:
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!
Oct 8, 2010. 5:12 AMjoshnosh says:
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
Dec 5, 2010. 2:22 PMKadenP says:
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.
Jan 26, 2011. 8:23 AMjoshnosh says:
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!
Nov 3, 2010. 2:22 PMblister75 says:
the best place i have found to purchase a ftdi cable/board is Sparkfun.com.
Oct 9, 2010. 3:16 AMjoshnosh says:
if your not going to help why even comment?
there is a "be nice" policy remeber
Oct 26, 2010. 9:49 PMchieffancypants says:
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!
May 3, 2010. 11:21 AMpingywon says:
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?
Sep 5, 2010. 6:45 AMmayson says:
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
Aug 4, 2010. 7:33 PMacherman says:
Thanks for the updated code. I will play around with it tomorrow. Again, great work!!
Aug 4, 2010. 4:53 PMCCOTTER247 says:
I am having a hard time with the code. Could you please make a diagram showing where to put the master card number. -Thanks
May 6, 2010. 6:06 PMnate4alpha says:
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?
May 11, 2010. 4:04 AMmalvika says:
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;)
May 10, 2010. 12:09 PMtehjrow says:
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.
Mar 21, 2010. 8:39 AMbobbob says:
Would this code work for the Parallax RFID reader?
Mar 28, 2010. 3:43 PMbobbob says:
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
Mar 25, 2010. 5:14 PMM4industries says:
 It wont require much, the output for the reader is 2400 Baud serial.
Jan 25, 2010. 2:52 PMkjpace34 says:
How do you read in a card in order to hard code it? 
Jan 30, 2010. 8:26 PMhivoltage says:
 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!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
81
Followers
5
Author:pcmofo
I like to take things apart, sometimes they go back together sometimes they end up as something entirely different then where they started.