Step 5: Understanding the Code

Thanks to an unknown code writer for most of this code!
#include <Servo.h>  // Includes libraries (computer files) that include lines of code for the servo
Servo myservo; // Gives the servo a name

int RFIDResetPin = 0;

//Register your RFID tags here
char tag1[13] = "------------";  //First tag ID, I replaced the letters and numbers with a corresponding amount of dashes (i'd rather you              
                                                  //not hack my system...)

char tag2[13] = "------------";  //Second tag ID

void setup(){                        // everything within the void setup runs the code once on start-up 
  Serial.begin(9600);    //Sets up a connection to the computer when prototyping, incredibly useful to figure out whats happening in                      
                                        //the code because it can print lines of code onto a computer screen 
  myservo.attach(8);     //Attaches the servo to a specific pin on the arduino, this is like the lane of a highway for information 
  myservo.write(178);  // Sets the limited rotation (0-180 degrees) servo to 178, the idle position
  pinMode(RFIDResetPin, OUTPUT);  //Clears the slate for sensing a tag
  digitalWrite(RFIDResetPin, HIGH);    //puts it on "high alert"


void loop(){              //everything within the void loop is run through in a linear fashion over and over again 
  char tagString[0];                    //the next couple of lines work with the incoming data to turn it into numbers and letters which form
  int index = 0;                            //the ID tag 
  boolean reading = false;

  while(Serial.available()){               //while there is a serial connection (while the reader is communicating to the board...)

    int readByte = Serial.read(); //read next available byte          //code that begins decoding (haha) the binary 

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //reset the RFID reader

void checkTag(char tag[]){                   //Check the read tag against known tags

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){                 // if matched tag1, do this
    servoGo();                                            //Activate the servo!
    Serial.println("card 1 read");             //print "card 1 read" to the computer, helps see if the code reaches this point when and how it
  else if(compareTag(tag, tag2)){           //if matched tag2, do this
   servoGo();                                                //Activate the servo!
   Serial.println("card 2 read");
/* else{
    Serial.println(tag); //read out any unknown tag
    Serial.println("unknown card");


void servoGo(){                                     
  Serial.println("Servo Activated");         //prints a line to the computer that tells me that its reaches this point 
    myservo.write(20);                               //writes the servo to a position that will pull the cord that pops the trunk
    myservo.write(178);                            //resets the position for the next activation 
  Serial.println("Servo Action Complete");    //prints a line to the computer confirming the completion of this method (external code of

/////////////////////////////////// The rest is code used to control the system that reads the code

void resetReader(){

//Reset the RFID reader to read again.

  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);

void clearTag(char one[]){

//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise

  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;

boolean compareTag(char one[], char two[]){

//compare two value to see if same,
//strcmp not working 100% so we do this

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;

  return true; //no mismatches

//picture: http://www.thinkgeek.com/images/products/additional/large/wrapping_paper-binary.jpg
<p>Hey guys, check this out!</p><p>A no of RFID based projects.</p><p>http://www.nrdcentre.com</p>
<p>Hey guys, check this out!</p><p>A no of GSM based projects.</p><p>http://www.nrdcentre.com</p>
<p>Interesting </p>
Maybe add a reed valve to turn on the power only when the card with a magnet and RFID chip is presented.
I would like to purchase an rfid setup. Basically powered from 12 to fire a spst relay. <br>If you can supply something like that please email me direct. Greenzero13 at gmail<br><br>Thanks
I have a pro micro and I never solder my FTDI headers into it. Instead, I stick the long end of a 6 pin straight header into the USB - FTDI. Then stick the short end into the 6 holes on the pro micro while programming. When finished, all you have to do is remove the header + FTDI. If you are doing small projects, this can save a lot of space.
Why do you have a computer hard disk, an old tape cassette reader, a graphics card, and either a dial-up modem or an Ethernet board pictured at the top right of the image?
It is just a title picture. I could have set up all of my supplies and tools for a photoshoot but I choose to progress my electronic projects instead :) <br>Sorry if it was confusing.
This is the first time I've been truly disappointed in a feature of my car. I know my trunk latch mechanism inside &amp; out from when I had everything dismantled to install a rear-view camera...and there is no way I can manage this :'( <br>
I am not an electronics geek, but I am a Braille Geek and very curious about why you included the Braille....I love to see Braille where ever it is!
With my car I could just run a power wire from the car battery to a relay triggered by the Arduino, since with my car the interior trunk release is electric... <br>
Great place to start. I can see where it could be integrated into the car a little more. One could use the car's own battery, make sure your Arduino can take 14 volts or use a regulator. Most cars now have an electric trunk release so you could hook into that using a relay instead of the servo. <br>Heat sinks, good idea, I rarely use one though. I would recommend anyone new to soldering use one. I find if you move quickly you get the best joints. If your flux is smoking get the joint completed before it quits and don't be afraid to use lots.
Thanks, I am looking into integrating parts of it into my car battery but am also trying to be careful in order to not find my car with a dead battery one day. <br>Unfortunately my car does not have an electric trunk release. That would have made things a lot easier for me. <br>As for the soldering heat sink, it turns out I didn't need it, my board is not broken. I had not ruled that out as a possibility because the power system was not working, but since it now is, I have no reason to blame the board.
The Arduino's lowest &gt;required&lt; voltage is 6V, but recommended is 7V. The &quot;pulsing&quot; you are experiencing may or may not be too much current draw. You can test this by using a multimeter on the 5V + GRND pins. If the 5V drops to 0 during the &quot;pulse off&quot; phase, then it is likely that the Arduino is seeing more current than it can handle and the overcurrent protection is kicking in. I've had this happen with certain loads. <br> <br>After looking at your &quot;flow diagram,&quot; here is my recommendation: Use a separate supply for the servo. That size of servo is very likely to pull more amperage than the Arduino can handle regardless of what supply you have hooked up to the Arduino. I think the best option is to use the 12V from your car, regulated down to the required 6V, to power the servo and buy a different battery with a higher voltage to power the Arudino. The servo obviously doesn't use power unless you're actually actuating it so it won't be a drain on the battery and it can draw much more current without bothering the Arduino.
Thanks, those are some good suggestions. If the issue is that the current is more than the arduino can handle, why would the system work when plugged in to the computer? I will definitely try the higher voltage battery though, I could see that being the problem. Thanks for the help!
The USB connection is current-limited. The battery is not. You can pump as much current as the battery will allow through the Arduino (and eventually hit the overcurrent protection) but the USB connection will only allow a certain amount of current to be drawn thanks to current-limiting resistors in your motherboard.
I agree with jwhitley, use the car's battery as the power source(s) and let the arduino become a &quot;relay-like&quot; trigger, not a power supplier. Also, USB is MB limited to 500mw max and yes I'd think the arduino would switch to it's USB power mgmnt upon USB connection. It looks like you have a crown vic so running a voltage line is easy enough by removing the trim along the bottom of the doors and you can punch through the thick rubber boot that allows the car's wiring harness through the firewall. Just remember to fuse at the battery (as close as you can get) to limit any potential damage from shorts to GND. You can also find switched voltage under the dash that is only &quot;HOT&quot; while the key is in the &quot;RUN&quot; position or only &quot;HOT&quot; while the key is in the &quot;STARTING-ONLY&quot; position should you choose to explore other options! I have kitted a few Crown V's up for police use (as well as other cars/truck/suvs) and this has way more potential than just a novelty! Great job! See now I need to dig out my barely used UNO kit and get an RIFD kit too! Peace n God bless! &lt;&gt;&lt;
When plugged into the PC, doesn't the Arduino automatically switch it's power to run off of the USB? That would have a much higher amperage capacity, to run the servo.
wow, this gave me an idea to how to make an anti thief system. instead of powering up the servo with the RFID, you can put a heavy duty relay from the spark plug wires in your car and trigger it with the RFID, so anybody can drive it! you could start the car also with the RFID, but is a pretty seen idea. <br> <br>nice ible dude, i mean duke!
I have never seen a relay that would hold up to a spark plug voltage and surges for long. I would think you could wire in the switch to the rfid easier.
mr. stacey davids got the solution. <br>http://www.youtube.com/watch?v=-zbC6T6sfZM <br>skip to 1:52
That's a cool idea, it would be fun to touch my wallet to a predefined spot thereby stating my car. Unfortunately though I am already working on a push button start system that is tied into a Nike + door unlock system.
How would that be anti-theft? Unless you're saying disable the key-ignition so it can't be hotwired? But at that point you would probably want to hide somewhere on the dashboard. <br> <br>If that's what you were referring to, that a very creative use. Though it may be just as effective to also set it up to use the RFID tag to unlock the doors.
I really like this and is IS really informative; but me being the simple guy I am went for an easier option with the same results (well, almost same, mine was on the doors instead of the boot). <br> <br>I purchased an rfid unit that was ready made for &Acirc;&pound;20 complete with fobs from ebay. No playing about with voltage regulation or losing an arduino unit to doing what is in effect a very simple thing for it. (I'm thinking cost effectiveness here). <br>At the same time a central locking kit with 4 servos and control unit - crucially the control unit allowed a voltage input to mimic a keypress on a normal fob. <br>The system was also going to allow keyless ignition from the initial RFID response, but unfortunately the car didn't last long enough :-( <br> <br>Again, I don't want to take anything away from your 'ible, I think it's great, I've just never had the time to get into the whole arduino thing :-)
I completely understand, electronics take forever to build. <br>Personally though, I enjoyed learning more about the electronics and found that using an arduino allows for an unlimited amount of flexibility. The instillation process is one of the hardest parts (if you want it well integrated) so flexibility was important to me. <br>Also, for unlocking of the doors, I choose to use the2.4Ghz spectrum because of it's range (Nike + with arduino). <br>I am also working on a keyless ignition system, but it is important to me to use a button so I don't have to take my wallet out of my pocket (yes, I am lazy). I also need the system to allow me to active electricity on, all electricity on, and car on. <br>Again, I understand where you are coming from though.
Thank you to everyone for the comments, its quite encouraging and makes me want to make more ibles! <br>An update (that I will reflect in my ible soon) on the power issue: <br>I powered the arduino with a regular 9v (just a regular Duracell) and powered the servo separately with the same 9v. I had two sets of alligator clips connected to the 9v. One set was simply connected to the raw and ground pins on the arduino and the other set was sent to the ground wire for the servo and a variable voltage regulator which then went to the servo's vcc wire. This allowed me to play with the voltage values for the servo only. I was very surprised by what I found: the arduino ran fine with the 9v and strangely the servo ran best on 1.55v pulling 6.8 mA. I always thought this servo would take 5-6v but it clearly ran best on 1.55v. <br>Now I need to figure out how I can permanently power the system. I can find a battery that is 9v with a high amp/hour rating but need to find a power system for the servo now. I will probably try to connect it to the car like jwhitley suggested. Thanks again for the help everyone!
So tell me about the servo, before I buy one (for a different idea), since I don't know anyone who messes with servos. Does it just rotate that wheel like 90 degrees, and return it to &quot;0&quot;? Could I program it to rotate 90 degrees, and stop until I want it to return to 0? I want something that could open a spring with an electronic signal, then return it when I want it to afterwards. Am I on the right track here? Great instructable, btw.
Hi, servos are used with Arduino on lots of robot projects such as this one, where you can find sample code using the servo library:<br> <br> <a href="http://www.instructables.com/id/Simple-Walker-Robot/step25/Program/" rel="nofollow">http://www.instructables.com/id/Simple-Walker-Robot/step25/Program/</a><br> <br> There may be better examples than this, but you can get an idea.&nbsp; I am building this as my first robot project and I found it very easy to drive the servo to any position within its range (easily 90 degrees or more) and hold it there. For this 'ible, if the cord is stiff to pull, you might need a very strong servo though. I am not sure the average hobby servo will be strong enough - This depends a lot on the car.&nbsp; Good luck with your project!
I agree with Untold's comment. The code is simple if you use a limited rotational servo. If you use a unlimited rotational servo it will be harder to code because it will use pulses to detect its position. <br>As for the strength of the servo, Untold is again correct. I started with a smaller servo but needed to upgrade to the one I listed. <br>Good luck on your project duggerpato!
too many amps maybe. usb is typically 100-150 mA, and youre putting 4.5 Ah to it. thats way more current than the module wants.
Good point, I am a bit confused though because I thought that the amp hour rating was more of a maximum and that the arduino draws the current it needs (in other words, the amp hour rating is not the same as the amp rating)?
You're absolutely right. 4.5 Ah just means that the battery can provide an amp for 4.5 hours, or 100mA for 45 hours, 10mA for 450 hours, etc. - it says nothing about how much current will be drawn by your circuit, which, as you rightly state, is just determined by the voltage of the battery and the arduino itself (plus any other things you have connected to the battery). <br> <br>So a bigger Ah figure just means your battery is going to last longer - it's not going to push any more current through the circuit.
Amp Hours is the amount of amps drawn over one hour, amps is the immediate amount of amps flowing in. Amp hours is more of a size thing for things like batteries.
Closer. Amp-hours is the total charge capacity of the battery (like how much fuel in a tank). <br> <br>The basic definition is: <br>Charge (coulombs) = Current (Amps) x Run time (Sec) <br> = Current (Coulombs/sec) x Run time (Sec) <br>But it's easier to use in units of Amp x hours rather than Amp x sec, like so: <br> Charge (Amp Hours) = Current (Amps) x Run time (Hours) <br> Helps you figure out how long the battery will last (roughly, it won't discharge evenly). <br> <br>Current capacity is how fast the fuel can flow out of the system. That is what sounds more related to the issue you guys are discussin.g <br> <br>Hope this helps, <br>David <br> <br>
Thanks, that's what I was thinking, which is why I thought the listed battery would work. I will have to try the 9v to see if it is the battery or the arduino...
overall, this is kickass. can you use one arduino module for more than one rfid reader? if you put the arduino in the center console, and ran an rfid reader for the trunk, doors, and starter (maybe), could the one arduino differentiate between signals? or just fire all the servos at once? <br>
Thanks, it was fun to build! <br>That's a good idea, I didn't think of using two RFID readers. Since I have this system (http://www.sparkfun.com/tutorials/135) set up, I was just going to have the push button start work only within a specified time after the doors are unlocked. I like your idea, but I am not sure if the range of the RFID reader (even the ID-20) would be enough.
You could put a reader on the drivers seat. <br> <br>Then just for fun, I would have another project that monitors both the seat switch that triggers the seatbelt warning, and the RFID status - if someone sits in the seat without the correct RFID card, then a bogus security message will prompt them to enter a code on a keypad in order to bypass the alarm. The keypad will actually be a way to compose music over an external PA speaker. This prompt of course will inform you that your card isn't registering properly.
Haha, that could be fun, especially if I did it to a friends car while they were away...
For pranking, I would skip the RFID and just use the seatbelt switch to start the show. <br> <br>You could have a rhythm track that plays for 5 seconds whenever a key is pressed. And a countdown timer that plays the rhythm track plus any key presses that were recorded to memory. The implication being that your friend needs to enter something before the timer runs down. All the while another track keeps playing 'Please enter code'
You may need a powet filter. This &quot;smothes&quot; out the bumps in the power and gives a clean level power. DC current in cars is tricky. I have leds in my car under the dash runnig of the 12volt rail. I have the regulator and power filter in a 12volt power plug. I also have a protection fuse. <br> <br>And on the ardunio, when the programer is pluged in the unit switches to usb power. Which is 5volt at around 750ma. 750ma is the max usb can give out. This may not be enough for certian servos. A method I see that works is a relay to control the highet power for the servo. Yes it is more wiring and tweaking in the code. Hope this helps
it may be because the battery's voltage is 6V and if I'm pretty sure that unless you're running of usb the arduino need a higher voltage in order to drop it down properly to 5V (using its onboard 5V regulator)
hi, great able, one question, ever accidentally unlock trunk just walking by?
Thanks, I have never accidentally unlocked it because the range is small enough that you need to almost brush the car.
Very nice.<br> I see you are not putting the Arduino to sleep. This means it constantly takes about 40mA from the battery which is quite a bit. with 4.5A/h battery you get 110 hours of operation. If you are putting it to sleep for 1second, wakes up for polling the RFID (that will be very few mill-seconds and back to sleep) you will get an average current drawn that is substantially less and much more battery time. You could easily apply a factor of 20 to the above (so few months instead of current ~5 days). Plenty of samples for that, you can see my simple parking sensor instructable for one <a href="http://www.instructables.com/id/Simple-Parking-Sensor/" rel="nofollow">http://www.instructables.com/id/Simple-Parking-Sensor/</a>&nbsp;.<br> <br> Few things to note when doing that:<br> 1. Reduce unnecessary debugging messages and shorten them to reduce wake up time.<br> 2. Make the serial fastest you can (115,200)<br> 3. Remain time efficient in code, even at cost of space.<br> <br> I got a factor of almost 50 with sleep of 2 seconds. The downside of that is the user experience can be of a delayed response to sensing the RFID. I think one second sleep will not an issue (on average a delay of 0.5second).
Thats awesome, thanks! I will have to try that.
For a security system you would want to kill power to the ignition switch, maybe from under the hood. A simple relay activated by the RFID. This is basically how the new cars with push-button ignition work. The RFID &quot;key&quot; kicks in a relay that powers the switch so you just push the button to start. So now you can convert any car to push-button ignition if you want, and still be secure.
For your power issue, as funky says, it could be too many amps going through. <br> <br>Another possibility is not enough voltage (if it works the same as the Uno). The usual voltage I give for my ardunios is 9 volts, so try a 12 volt lead battery to see if it does the same pulsation as the 6 volt does. <br> <br>Other than that, this could work together with another project someone did who took a keyfob and a nike+ rfid tag. The guy was able to unlock his car when he was about 10 feet away without using his keys to unlock his car. <br> <br>Cool ible!
I was thinking the same about the voltage. 6v just not be enough.
Oh and Amazing build. Love the idea and plan to make one of my own.

About This Instructable


313 favorites


More by duke2luke: RFID Controlled Car Trunk
Add instructable to: