arduino rfid tutorial - my diy addiction

9
Arduino RFID Tutorial by educ8s https://www.instructables.com/member/educ8s/ https://www.youtube.com/user/educ8s https://educ8s.tv/ Dear friends welcome to another Instructable. This is Nick from educ8s.tv and today we are going to learn how to use this RFID Reader with Arduino in order to build a simple lock system. For the first time, we are going to use RFID tags with Arduino. I have built a simple project which reads the Unique ID (UID) of each RFID tag we place close to the reader and displays it on this OLED display. If the UID of the tag is equal to a predefined value that is stored in Arduino’s memory, then in the display we are going to see the “Unlocked” message. If the Unique ID of the card is not equal to the predefined value, the Unlock message won’t appear. Cool isn’t it? There is a lot to cover, so let's get started! https://www.youtube.com/watch?v=So83sH6-jwM

Upload: others

Post on 29-Nov-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino RFID Tutorial - My Diy Addiction

Arduino RFID Tutorial

by educ8s https://www.instructables.com/member/educ8s/ https://www.youtube.com/user/educ8s https://educ8s.tv/

Dear friends welcome to another Instructable. This is Nick from educ8s.tv and today we are going to

learn how to use this RFID Reader with Arduino in order to build a simple lock system.

For the first time, we are going to use RFID tags with Arduino. I have built a simple project which reads

the Unique ID (UID) of each RFID tag we place close to the reader and displays it on this OLED display.

If the UID of the tag is equal to a predefined value that is stored in Arduino’s memory, then in the display

we are going to see the “Unlocked” message. If the Unique ID of the card is not equal to the predefined

value, the Unlock message won’t appear. Cool isn’t it?

There is a lot to cover, so let's get started!

https://www.youtube.com/watch?v=So83sH6-jwM

Page 2: Arduino RFID Tutorial - My Diy Addiction

Step 1: Get All the Parts The parts needed in order to build this project are as follows: An Arduino Uno http://educ8s.tv/part/ArduinoUno An RFID Reader http://educ8s.tv/part/RFID OLED display http://educ8s.tv/part/OLED096 A small breadboard http://educ8s.tv/part/SmallBreadboard Some wires http://educ8s.tv/part/Wires Optional Parts: Powerbank http://educ8s.tv/part/Powerbank The total cost of the project is around $15.

Page 3: Arduino RFID Tutorial - My Diy Addiction

Step 2: The RC522 RFID Reader Each RFID tag has a small chip inside. If I place a flashlight under this RFID card you can see the small

chip and the coil that surrounds it. This chip does not have a battery in order to get power. It receives

power from the reader, this device, wirelessly using this big coil. The reader can read an RFID card like

this one from a distance up to 20mm. The same chip exists in keychain RFID tags as well.

Each RFID tag has a unique number that identifies it. That’s the UID that we display on the OLED display.

Except from this UID, each tag can store data. In this type of cards we can store up to 1K of data!

Impressive isn’t it? We won’t use this functionality today but will do so in a future Instructable. Today, all

we are interested in is to identify a specific card by its UID. The cost of the RFID reader and these two

RFID cards is around $4.

Page 4: Arduino RFID Tutorial - My Diy Addiction

Step 3: OLED Display

This is a very nice display to use with Arduino. It is an OLED display and that means that it has a low

power consumption. The power consumption of this display is around 10-20 mA and it depends on how

many pixels are lit.

The display has a resolution of 128×64 pixels and it is tiny in size. There are two variations of the display.

One of them is monochrome, and the other one like the one I use can display two colors, yellow and blue.

The upper part of the screen can only display yellow, the lower part only blue.

This OLED display is very bright, and it has a great library support. Adafruit has developed a very nice

library for this display. In addition to that, the display uses the I2C interface, so the connection with

Arduino is incredibly easy. You only need to connect two wires except for Vcc and GND. If you are new to

Arduino and you want an inexpensive and easy to use display to use with your project, start with display.

It is the easiest way to add a display to your Arduino project. I have prepared a detailed tutorial on how to

use this display as a YouTube video.

https://youtu.be/A9EwJ7M7OsI

Page 5: Arduino RFID Tutorial - My Diy Addiction

Step 4: Connect All the Parts

The connection with the Arduino Uno board is very simple. At first, let’s connect the power of both the

reader and the display.

Be careful, the RFID reader must be connected to the 3.3V output of the Arduino Uno or it will be

destroyed. Since the display can also work at 3.3V we connect the Vcc from both modules to the

breadboards positive rail. This rail is then connected to the 3.3V output of the Arduino Uno. Next, we

connect both GNDs to the breadboard GND rail. Then we connect the GND rail of the breadboard to

Arduino GND.

OLED Display - Arduino

Vcc 3.3V

GND GND

SCL Analog Pin 5

SDA Analog Pin 4

RFID Reader – Arduino

RST Digital Pin 9

IRQ Unconnected

MISO Digital Pin 12

MOSI Digital Pin 11

SCK Digital Pin 13

SDA Digital Pin 10

Page 6: Arduino RFID Tutorial - My Diy Addiction

The RFID reader module uses the SPI interface in order to communicate with Arduino. So we are going to

use the hardware SPI pins of the Arduino UNO. RST pin goes to digital pin 9. IRQ pin stays unconnected.

MISO pin goes to digital pin 12. MOSI pin goes to digital pin 11. SCK pin goes to digital pin 13 and lastly,

SDA pin goes to digital pin 10. That’s it. The RFID reader is connected. We now have to connect the

OLED display with Arduino using the I2C interface. So, the SCL pin of the display goes to Analog Pin 5

and SDA pin of the display to Analog Pin 4. If we now power up the project and place an RFID card close

to the reader we can see that the project is working fine! Now it’s time to take a look at the code of the

project.

Step 5: The Code of the Project

In order the project code to compile we need to include some libraries. First of all, we need the MFRC522

Rfid library. In order to install it, go to Sketch -> Include Libraries -> Manage libraries. Search for

MFRC522 and install it. We also need the Adafruit SSD1306 library and the Adafruit GFX library for the

display. Install both libraries with the same procedure. The Adafruit SSD1306 library needs a small

modification. Go to the Arduino -> Libraries folder, open the Adafruit SSD1306 folder and edit the

Adafruit_SSD1306.h library. Comment line 70 and uncomment line 69. Our display has a resolution of

128x64, that’s what we are defining here. Now we are ready to take a quick look at the code.

At first, we declare the value of the RFID tag that we want the Arduino to recognize. It is an array of

integers.

int code[] = {69,141,8,136}; //This is the stored UID

Next, we initialize the RFID reader and the display.

rfid.PCD_Init();

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

After this, in the loop function, we check for a tag on the reader every 100 ms.

If there is a tag on the reader we read its UID and we print it on the display. Next, we compare the UID of

the tag we just read, with the value that is stored in the code variable. If the values are the same, we

display the UNLOCK message, else we don’t display this message.

if(match)

{

Serial.println("\nI know this card!");

printUnlockMessage();

}else

{

Serial.println("\nUnknown Card");

}

Of course, you can modify this code in order to store more than 1 UID value in order for the project

to recognize more RFID tags.

Page 7: Arduino RFID Tutorial - My Diy Addiction

Shown below is the full code for this project, you can copy and paste it into the Arduino code editor

or the .ino can be downloaded here: https://educ8s.tv/wp-content/uploads/2016/10/RFID.zip

////////////////////////////////////////////////////////////////////

// Arduino RFID Tutorial v1.02 //

// Get the latest version of the code here: //

// http://educ8s.tv/arduino-rfid-tutorial/ //

/////////////////////////////////////////////////////////////////////

#include <MFRC522.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <SPI.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

#define SS_PIN 10

#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[] = {69,141,8,136}; //This is the stored UID

int codeRead = 0;

String uidString;

void setup() {

Serial.begin(9600);

SPI.begin(); // Init SPI bus

rfid.PCD_Init(); // Init MFRC522

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)

// Clear the buffer.

display.clearDisplay();

display.display();

display.setTextColor(WHITE); // or BLACK);

display.setTextSize(2);

display.setCursor(10,0);

display.print("RFID Lock");

display.display();

}

void loop() {

if( rfid.PICC_IsNewCardPresent())

{

readRFID();

}

delay(100);

}

void readRFID()

{

Page 8: Arduino RFID Tutorial - My Diy Addiction

rfid.PICC_ReadCardSerial();

Serial.print(F("\nPICC type: "));

MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

Serial.println(rfid.PICC_GetTypeName(piccType));

// Check is the PICC of Classic MIFARE type

if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&

piccType != MFRC522::PICC_TYPE_MIFARE_1K &&

piccType != MFRC522::PICC_TYPE_MIFARE_4K) {

Serial.println(F("Your tag is not of type MIFARE Classic."));

return;

}

clearUID();

Serial.println("Scanned PICC's UID:");

printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ "

"+String(rfid.uid.uidByte[3]);

printUID();

int i = 0;

boolean match = true;

while(i<rfid.uid.size)

{

if(!(rfid.uid.uidByte[i] == code[i]))

{

match = false;

}

i++;

}

if(match)

{

Serial.println("\nI know this card!");

printUnlockMessage();

}else

{

Serial.println("\nUnknown Card");

}

// Halt PICC

rfid.PICC_HaltA();

// Stop encryption on PCD

rfid.PCD_StopCrypto1();

}

void printDec(byte *buffer, byte bufferSize) {

for (byte i = 0; i < bufferSize; i++) {

Serial.print(buffer[i] < 0x10 ? " 0" : " ");

Serial.print(buffer[i], DEC);

}

}

void clearUID()

{

display.setTextColor(BLACK); // or BLACK);

display.setTextSize(1);

Page 9: Arduino RFID Tutorial - My Diy Addiction

display.setCursor(30,20);

display.print(uidString);

display.display();

}

void printUID()

{

display.setTextColor(WHITE); // or BLACK);

display.setTextSize(1);

display.setCursor(0,20);

display.print("UID: ");

display.setCursor(30,20);

display.print(uidString);

display.display();

}

void printUnlockMessage()

{

display.display();

display.setTextColor(BLACK); // or BLACK);

display.setTextSize(2);

display.setCursor(10,0);

display.print("RFID Lock");

display.display();

display.setTextColor(WHITE); // or BLACK);

display.setTextSize(2);

display.setCursor(10,0);

display.print("Unlocked");

display.display();

delay(2000);

display.setTextColor(BLACK); // or BLACK);

display.setTextSize(2);

display.setCursor(10,0);

display.print("Unlocked");

display.setTextColor(WHITE); // or BLACK);

display.setTextSize(2);

display.setCursor(10,0);

display.print("RFID Lock");

display.display();

}

Final Thoughts & Improvements

As you can see with a very low cost we can add an

RFID reader to our projects. We can easily build a

security system with a reader like this one or build

more fun projects.