Introduction
RFID is short for radio frequency identification. It is a wireless application to transfer data in the purpose of identifying and tracking tags. In this experiment, we will use an RFID module, a relay, and an I2C LCD1602 to assemble an entrance guard system.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * RFID module
– 1 * RFID key tag
– 1 * Relay
– 1 * I2C LCD1602
– Several jumper wires
– 1 * 3-Pin anti-reverse cable
– 1 * 4-Pin anti-reverse cable
– 1 * Breadboard
Experimental Principle
First, you need to know the ID of the RFID key tag and write the ID to the rfidTest file. Compile the code. We can see “Welcome!” display on the I2C LCD1602. Swipe the RFID key ring on the RFID module. If the password is correct, the normally open contact of the relay will be closed and the LCD will display a string “ID:5AE4C955” “hello SunFounder”, and then “Welcome!” two seconds later; if the password is incorrect, the normally open contact of the relay will be disconnected and the LCD will display a string “Hello unknown guy” , and then “Welcome!” two seconds later
Note: For this module, please use a 3.3V power supply, or it will get burnt.
Experimental Procedures
Step 1: Build the circuit
The wiring between RFID and SunFounder Uno is as follows:
RFID | SunFounder Uno |
VCC | 3.3V |
RST | 2 |
GND | GND |
MISO | 3 |
MOSI | 4 |
SCK | 5 |
NSS | 6 |
IRQ | 7 |
The wiring between I2C LCD1602 and SunFounder Uno:
I2C LCD1602 | SunFounder Uno |
GND | GND |
VCC | 5v |
SDA | A4 |
SCL | A5 |
The wiring between Relay Module and SunFounder Uno:
Relay Module | SunFounder Uno |
SIG | 8 |
VCC | 5V |
GND | GND |
Step 2: Open the getId file with Arduino IDE (Please refer to the example code in LEARN -> Get Tutorials on our website). Compile the code
Note: Here you need to add a library. Refer to the description in Lesson 4 previously in the manual.
Step 3: Upload the sketch to the SunFounder Uno board
Then place the RFID key tag in the induction zone of the RFID module. You will see the following values printed on Serial Monitor:
Step 4: Now, you may know the ID of your RFID key tag (e.g. my magnetic card ID is 5AE4C955).
Open the rfidTest file and replace the ID in the sketch with the ID you just note down (divide the ID into four parts and fill them according to the following format), as follows:
Step 5: Upload the sketch into SunFounder Uno board
Now, swipe the RFID key tag on the RFID module. If the password is correct, the LCD will display a string “ID:5AE4C955” “Hello SunFounder”, and then display “Welcome!” two seconds later. If the password is incorrect, the LCD will display a string “Hello unknown guy”, and then display “Welcome!” two seconds later.
Code
getId
/******************************************* * function:get the id of RFID key * RFID SunFounder Uno * VCC 3.3V * RST 2 * GND GND * MISO 3 * MOSI 4 * SCK 5 * NSS 6 * IRQ 7 ****************************************/#include”rfid1.h” RFID1 rfid;//create a variable type of RFID1uchar serNum[5]; // array to store your IDvoid setup() { Serial.begin(9600); //initialize the serial rfid.begin(7, 5, 4, 3, 6, 2); ////rfid.begin(IRQ_PIN,SCK_PIN,MOSI_PIN,MISO_PIN,NSS_PIN,RST_PIN) delay(100);//delay 1s rfid.init(); //initialize the RFID } void loop() { uchar status; uchar str[MAX_LEN]; // Search card, return card types status = rfid.request(PICC_REQIDL, str); if (status != MI_OK) { return; } // Show card type rfid.showCardType(str); //Prevent conflict, return the 4 bytes Serial number of the card status = rfid.anticoll(str);if (status == MI_OK) { Serial.print(“The card’s number is: “); memcpy(serNum, str, 5); rfid.showCardID(serNum);//show the card ID Serial.println(); Serial.println(); } delay(500);rfid.halt(); //command the card into sleep mode } |
rfidTest
/******************************************* * name:RFID Entrance Guard System * function:first: get the id of RFID key by the getid.ino file ,then divide the ID into four parts and fill them in loop() function * swipe the RFID key ring on the RFID module. * If the password is correct, the normally open contact of the relay will be closed and the LCD will display a string “ID:5AE4C955” “hello SunFounder”, * and then “Welcome!” two seconds later; * if the password is incorrect, the normally open contact of the relay will be disconnected and the LCD will display a string “Hello unknown guy” , * and then “Welcome!” two seconds later * connection: * RFID SunFounder Uno * VCC 3.3V * RST 2 * GND GND * MISO 3 * MOSI 4 * SCK 5 * NSS 6 * IRQ 7 ****************************************/ //Email:support@sunfounder.com //Website:www.sunfounder.com#include”rfid.h” #include <LiquidCrystal_I2C.h> #include <Wire.h> LiquidCrystal_I2C lcd(0x27,16,2); RFID rfid; ////create a variable type of RFID #define relayPin 8 //relay module attach to pin8uchar serNum[5]; // array to store your IDvoid setup() { lcd.init(); //initialize lcd lcd.backlight(); //turn on the backlight Serial.begin(9600); rfid.begin(7, 5, 4, 3, 6, 2);//rfid.begin(IRQ_PIN,SCK_PIN,MOSI_PIN,MISO_PIN,NSS_PIN,RST_PIN) delay(100); rfid.init(); //initialize the RFID pinMode(relayPin, OUTPUT); //set relayPin as OUTPUT digitalWrite(relayPin,HIGH); //and high level //Serial.begin(9600); lcd.setCursor(0,0); lcd.print(” Welcome! “); //print” Welcome! “ delay(2000);//delay 2s} void loop() { uchar status; uchar str[MAX_LEN]; // Search card, return card types status = rfid.request(PICC_REQIDL, str); if (status != MI_OK) { return; } // Show card type rfid.showCardType(str); //Prevent conflict, return the 4 bytes Serial number of the card status = rfid.anticoll(str);if (status == MI_OK) { //Serial.print(“The card’s number is: “); lcd.setCursor(0,0); lcd.print(” ID: “); memcpy(serNum, str, 5); rfid.showCardID(serNum);//show the card ID // Serial.println();// Check people associated with card ID uchar* id = serNum; if( id[0]==0x4B && id[1]==0xE6 && id[2]==0xD1 && id[3]==0x3B ) { digitalWrite(relayPin,LOW); // Serial.println(“Hello Dannel!”); lcd.setCursor(0,1); lcd.print(” Hello Dannel! “); delay(2000); lcd.clear(); digitalWrite(relayPin,HIGH); } //if the card id is 5AE4C955,then relay connect else if(id[0]==0x5A && id[1]==0xE4 && id[2]==0xC9 && id[3]==0x55) { digitalWrite(relayPin,LOW); //Serial.println(“Hello SunFounder”); lcd.setCursor(0,1); lcd.print(“Hello SunFounder”); delay(2000); lcd.clear(); digitalWrite(relayPin,HIGH); } else { //Serial.println(“Hello unkown guy!”); lcd.setCursor(0,1); lcd.print(“Hello unkown guy”); delay(2000); lcd.clear(); } } lcd.setCursor(0,0); lcd.print(” Welcome! “); delay(2000); rfid.halt(); //command the card into sleep mode } |