-
Notifications
You must be signed in to change notification settings - Fork 17
/
rc522-rfid.ino
113 lines (88 loc) · 3.5 KB
/
rc522-rfid.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* FILE: RC522_RFID_Module_Example
DATE: 23/03/14
VERSION: 0.2.1 Spark
REVISIONS:
29/08/16 Version 0.2.1, modified by ScruffR: corrected Software SPI sample
23/03/14 Version 0.2, modified by Paul Kourany to run on Spark Core
with added support for Software SPI
24/07/13 Created version 0.1
This is an example of how to use the RC522 RFID module. The module allows reading
and writing to various types of RFID devices and can be found in our MFRC-522
(HCMODU0016) and Ultimate RFID (HCARDU0068) kits. This example Arduino sketch uses
the RFID library written by Miguel Balboa to read the pre-programmed serial number
from RFID cards and tags supplied with our RFID kits. Snapshots and links to the
library are available on our support forum.
PINOUT:
RC522 MODULE SPARK HARD SPI SPARK SOFT SPI
SS A2 ANY
SCK A3 ANY
MOSI A5 ANY
MISO A4 ANY
IRQ N/A N/A
GND GND GND
RST D9 ANY
3.3V 3.3V 3.3V
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
/* Include the RFID library */
/* SEE RFID.h for selecting Hardware or Software SPI modes */
#include "RFID.h"
/* Define the pins used for the SS (SDA) and RST (reset) pins for BOTH hardware and software SPI */
/* Change as required */
#define SS_PIN A2 // Same pin used as hardware SPI (SS)
#define RST_PIN D2
/* Define the pins used for the DATA OUT (MOSI), DATA IN (MISO) and CLOCK (SCK) pins for SOFTWARE SPI ONLY */
/* Change as required and may be same as hardware SPI as listed in comments */
#define MOSI_PIN D3 // hardware SPI: A5
#define MISO_PIN D4 // " " : A4
#define SCK_PIN D5 // " " : A3
/* Create an instance of the RFID library */
#if defined(_USE_SOFT_SPI_)
RFID RC522(SS_PIN, RST_PIN, MOSI_PIN, MISO_PIN, SCK_PIN); // Software SPI
#else
RFID RC522(SS_PIN, RST_PIN); // Hardware SPI
#endif
void setup()
{
Serial.begin(9600);
#if !defined(_USE_SOFT_SPI_)
/* Enable the HW SPI interface */
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.begin();
#endif
/* Initialise the RFID reader */
RC522.init();
}
void loop()
{
/* Temporary loop counter */
uint8_t i;
/* Has a card been detected? */
if (RC522.isCard())
{
/* If so then get its serial number */
RC522.readCardSerial();
Serial.println("Card detected:");
/* Output the serial number to the UART */
for(i = 0; i <= 4; i++)
{
Serial.print(RC522.serNum[i],HEX);
Serial.print(" ");
}
Serial.println();
}
else
Serial.println("Card NOT detected:");
delay(1000);
}