Experiment of controlling relay with MF RC522 RF card based on Arduino UNO R3
[Copy link]
Hardware support: 1. An Arduino Uno R3
2. An MF RC522 contactless RF receiver module + RF card
3. A 5v electronic relay
Software tools: IDE (arduino-1.0.5-r2)
Hardware connection: RC522 working voltage 3.3v
Arduino RC522
D5————————RST
D10————————SDA(CS)
D11————————MOST
D12————————MISO
D13————————SCK
GND————————GND
VCC(3.3V)————————VCC(3.3V)
Arduino Relay
D7————————IN
GND————————CND
VCC(5V)————————VCC(5V)
Add the library file of the MF RC522 RF module to the IDE (arduino-1.0.5-r2) library.
Find the libraries folder in the IDE (arduino-1.0.5-r2) folder and copy the MF RC522 library file to the current
Code:
#include <SPI.h>
#include <RFID.h>
RFID rfid(10,5); //D10--card reader MOSI pin, D5--card reader RST pin
int led = 9;
int relay=7;
bool state=true;
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(led, OUTPUT); //Set the pin mode: output mode or input mode. The part before the comma in the brackets corresponds to the pin being set, and the part after the comma corresponds to setting output or input. The input mode is INPUT, and the pin output is OUTPUT.
pinMode(relay,OUTPUT);
digitalWrite(relay,HIGH);//For output port, write: digitalWrite(,); For input port, read: digitalRead(,); digitalWrite(theRedLed,HIGH) pin outputs high level, digitalWrite(theRedLed,LOW) pin outputs low level.
}
void loop()
{
unsigned char type[MAX_LEN];
//Find card
if(rfid.isCard()) {
Serial.println("Find the card!");
;
//Show card type
ShowCardType(type);
//Read the card serial number
if(rfid.readCardSerial()) {
Serial.print("The card's number is : ");
Serial.print(rfid.serNum[0],HEX);
Serial.print(rfid.serNum[1],HEX);
Serial.print(rfid.serNum[2],HEX);
Serial.print(rfid.serNum[3],HEX);
Serial.print(rfid.serNum[4],HEX);
Serial.println(" ");
ShowUser(rfid.serNum);
}
//Card selection, can return the card capacity (lock the card to prevent multiple readings), remove this line to read cards continuouslySerial.println
(rfid.selectTag(rfid.serNum));
}
rfid.halt();
}
void ShowCardType( unsigned char* type)
{
Serial.print("Card type: ");
if(type[0]==0x04&&type[1]==0x00)
Serial.println("MFOne-S50");
elseif(type[0]==0x02&&type[1]==0x00)
Serial.println("MFOne-S70");
elseif(type[0]==0x44&&type[1]==0x00)
Serial.println("MF-UltraLight");
elseif(type[0]==0x08&&type[1]==0x00)
Serial.println("MF-Pro");
elseif(type[0]==0x44&&type[1]==0x03)
Serial.println("MF Desire");
else
Serial.println("Unknown");
}
void ShowUser( unsigned char* id)
{
//EE9B 9C 38 D1
if(id[0]==0xB4 && id[1]==0x24 && id[2]==0x37 &&id[3]==0x5D ) {
Serial.println("HelloMary!");
state=RelayStatus(state);
}
elseif(id[0]==0x44 && id[1]==0x93 && id[2]==0xA8 &&id[3]==0x3A) {
Serial.println("Hello MicroHao!");
state=RelayStatus(state);
}
elseif(id[0]==0x24 && id[1]==0xC7 && id[2]==0xA7 &&id[3]==0x3B) {
Serial.println("Hello Tom!");
state=RelayStatus(state);
}
else{
Serial.println("Hello unkown guy!");
BlinkLED();
}
}
bool RelayStatus(bool status)
{
if(status)
{
digitalWrite(led, HIGH); // turnthe LED on (HIGH is the voltage level)
digitalWrite(relay,LOW);
return false;
}
digitalWrite(led, LOW); // turnthe LED on (HIGH is the voltage level)
digitalWrite(relay,HIGH);
return true;
}
void BlinkLED()
{
digitalWrite(relay,HIGH);
for(int i=0;i<3;i++)
{
digitalWrite(led, HIGH); // turnthe LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(led, LOW); // turnthe LED off by making the voltage LOW
delay(1000);
}
}
|