Introduction
Hi! This will be my third update for this challenge, in the last update I made first steps in trying to make the slider mechanism for the skeleton key. For this update I will be taking a look at the center of the project, the Arduino MKR1000, as well as RFID module, to be specific the RC522. As I've talked in my first blog (Fingerprint Skeleton Key - Concept - Design for a Cause Challenge - Blog Post #1 ), there were multiple ways and ideas for how my device would recognize a door, and in the end I settled for RFID. I will go through basics of connecting the module to the Arduino, as well as making a mounting mechanism for the tags.
Why RFID?
RFID stands for Radio-frequency identification is a form of wireless communication that utilizes electromagnetic waves. A simple system requires a RFID tag, antenna and a reader. A lot of modern locks have RFID antennas and readers integrated in them, while we carry a small blue tag on our key chain, but that of course means the lock needs to powered. So I plan on reversing things a bit, the user will be carrying the RFID RC522 module with them, while the tags would be on the doors. The reason for this is that I want to make a device that is easy to set up without needing to modify a lot of other stuff, for example no new locks would have to be installed. All the door needs is a tag. There are 2 types of tags, active and passive. The active tags require a power source like a battery, while the passive ones don't. This is one of the main reason I chose to go with RFID, since the passive tags are really inexpensive, and they don't need any power at all. Now it's time to take a look at our module.
RC522
The RC522 is the most commonly used RFID module when it comes to Arduino projects. It is a really low cost module, I found my full set with a module one card and one tag for around 3$. Besides that, I only bought a few more tags, watching out that they are made for the same frequency as my module, which in this case is 13.56MHz. One more great thing about this module is its size, the module is 40x60mm, which is a great thing, since I am trying to make my project as compact as possible. Just one thing to keep in mind about this module, is that requires 3.3V unlike most Arduino modules that can work on 5V. First off all, we need to solder the pins to module and then we can get to connecting it to the Arduino.
{gallery} RC522 |
---|
Arduino
Now comes the part where we need to connect this to the Arduino, specifically the MKR1000 the board that all else will be based around.
There are a lot of great tutorials and libraries online to follow for the RC522. I personally went with the most popular option with the miguelbalboa library. The library can be found on github (https://github.com/miguelbalboa/rfid). There are some great example codes that came with the library with instructions for wiring up the RC522 for some of the Arduino boards. All we need to do with RFID is read the UID of the tags, with that information, based on the tags that we have stored on the Arduino, we can go and check what tag that is, and by doing that, see what key we need to pick. To try out the RC522, I've built this simple circuit:
Circuit
Most of you have already probably noticed an Arduino Uno besides the MKR1000, the reason it is in the picture is because the RC522 will work only with a 3.3V supply as I've mentioned above, and the MKR1000 doesn't have that output (excluding analog pins). I don't have a 3.3V voltage regulator at the moment, but will get it when I get to the assembly of the whole skeleton key. The grounds on the boards aren't connected since they are both attached to the same computer, and the LED-s will be the representation of the 4 different tags, wired to digital pins 1, 2, 3 and 4. As for wiring the RC522, the pins are connected like this:
- 3.3V on RC522 --- 3.3V on Arduino Uno
- RST on RC522 --- Pin 6 on Arduino MKR1000
- GND on RC522 --- GND on Arduino MKR1000
- MISO on RC522 --- MISO on Arduino MKR1000 (Pin 10)
- MOSI on RC522 --- MOSI on Arduino MKR1000 (Pin 8)
- SCK on RC522 --- SCK on Arduino MKR1000 (Pin 9)
- SDA (SS) on RC522 --- Pin 7 on Arduino MKR1000
Code
Now all that's left to do is code the Arduino. As I've already said, I'm using the miguelbalboa RFID library which can be found here (https://github.com/miguelbalboa/rfid). We need to do 2 things, first we need to write down all the UIDS of the tags on paper, which can be done using one of the example codes that are provided with the library, the example is DumpInfo. All that this example does is writes all of the data from the tag to the serial monitor, here is an example of what we should expect to see in the serial monitor.
At the top there we can see the line Card UID, and this is the information we want to write down. So I went through all of my tags and wrote their UID-s, now all that's left is to compare when we see a UID with one of the ones we wrote down.
#include <SPI.h> #include <MFRC522.h> constexpr uint8_t RST_PIN = 6; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = 7; // Configurable, see typical pin layout above MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance void setup() { Serial.begin(9600); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); pinMode(1,OUTPUT); pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); digitalWrite(1,LOW); digitalWrite(2,LOW); digitalWrite(3,LOW); digitalWrite(4,LOW); } void loop() { // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } // Dump debug info about the card; PICC_HaltA() is automatically called //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); I commented this lines because the code works faster, it takes a second or two for it to write to the serial monitor //Tag 1 if (mfrc522.uid.uidByte[0] == 0xa0 && mfrc522.uid.uidByte[1] == 0xe4 && mfrc522.uid.uidByte[2] == 0x7b && mfrc522.uid.uidByte[3] == 0x4d){ digitalWrite(1,HIGH); delay(400); digitalWrite(1,LOW); } //Tag 2 if (mfrc522.uid.uidByte[0] == 0xf0 && mfrc522.uid.uidByte[1] == 0x45 && mfrc522.uid.uidByte[2] == 0x75 && mfrc522.uid.uidByte[3] == 0x4d){ digitalWrite(2,HIGH); delay(400); digitalWrite(2,LOW); } //Tag 3 if (mfrc522.uid.uidByte[0] == 0xd0 && mfrc522.uid.uidByte[1] == 0x9f && mfrc522.uid.uidByte[2] == 0x78 && mfrc522.uid.uidByte[3] == 0x4d){ digitalWrite(3,HIGH); delay(400); digitalWrite(3,LOW); } //Tag 4 if (mfrc522.uid.uidByte[0] == 0x40 && mfrc522.uid.uidByte[1] == 0x6a && mfrc522.uid.uidByte[2] == 0xbe && mfrc522.uid.uidByte[3] == 0x4f){ digitalWrite(4,HIGH); delay(400); digitalWrite(4,LOW); } }
To check if the UID-s match all we need is a if per tag, or a switch, and check them byte by byte, and if they match, all we need to is flash the corresponding LED. In the code above you can see the UID-s of my tags, so that's the only thing that needs to be changed for different tags.
Test
All that's left to do now is test all of this, here is a video of me testing with all of the different tags!
Summary
This was a pretty simple, but pretty rewarding part of the build, one of the 3 big things has been finished with this blog and that is the door recognition system. I only had some trouble powering the board at first because it required 3.3V, but adding another Arduino solved that easily, though of course, I will be getting a small voltage regulator for it, since I will need to power some other stuff with 3.3V as well. My fingerprint module finally arrived, so I will be taking a look at that, and doing a blog about it as soon as I manage to do something with it, but in the meanwhile, I will be working on the final version of the slider mechanism. Thanks for reading the blog, hope you liked it!
Milos
Top Comments