rfid rc522 ต่อกับ micro sd card ไม่อ่านระหัส
เช็คที่ไลบารีครับ มี 2 แบบ ตามที่แจ้งหน้าสินค้าและบทความ
เขียนให้ออกทีละอันก่อนแล้วเอามารวมกัน ที่สำคัญคือ 2 โมดูลนี้ใช้อินเตอร์เฟสแบบ SPI เหมือนกัน จะต้องเขียนขา CS ในโคดให้ถูกต้องด้วยครับ
ถ้าไม่ได้ต่อ sd card ตัว rfid rc522 ก็อ่านได้ปกติ แต่ถ้าต่อ sd card rc522 จะไม่อ่าน
นี้คือ library ที่เขียนครับ
#include <SPI.h>
#include <MFRC522.h> // RFID module library
#include <RTClib.h> // RTC library
#include <Wire.h> // i2C/1-wire library
#include <SD.h> // SD card library
#include <LiquidCrystal_I2C.h>
#define RST_PIN 9 // RST pin for RFID module
#define SS_PIN 10 // Slave Select pine for RFID module
MFRC522 mfrc522(SS_PIN, RST_PIN); // define RFID reader class
RTC_DS1307 RTC; // define RTC class
LiquidCrystal_I2C lcd(0x27, 16, 2);
int buzzer = 8; // speaker or buzzer on pin 8
int led_pos = 3; // green LED on pin 3
int led_neg = 2; // red LED on pin 2
String UID_tagA = "69378d85"; // UID of tag that we are using
unsigned int MinsA = 0, HoursA = 0; // working minutes and hours for tag A
String readTag = "";
int readCard[4];
short UIDs_No = 1;
boolean TimeFlag[2] = {false, false};
DateTime arrival[2]; // tiem class for arrival
DateTime departure[2]; // time class for departure
int LastMonth=0; // working hours till now in a month
char DataRead=0;
void redLED(); // red LED on
void greenLED(); // green LED + buzzer on
void DisplayDateTime(); // Show screen Date and Time
void DisplayTag(); // Show screen TagData
int getID(); // read tag
boolean checkTag(); // check if tag is unknown
void errorBeep(); // error while reading (unknown tag)
void StoreData(); // store data to microSD
File myFile; // class file for reading/writing to file on SD card
// SETUP
void setup() {
Serial.begin(9600); // for testing and debugging
SPI.begin(); // run SPI library first; if not, RFID will not work
mfrc522.PCD_Init(); // initializing RFID, start RFID library
Wire.begin(); // start i2c library; if not, RTC will not work
RTC.begin(); // start RTC library
lcd.begin();
if(!RTC.isrunning())
//Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__)); // set RTC time to compiling time
Serial.println("Initial SD Card......");
pinMode(4, OUTPUT);
if (!SD.begin(4)){
Serial.println("No SD Card.");
return;
}else{
Serial.println("SD Card OK");
}
// setting DI/O
pinMode(led_neg, OUTPUT);
pinMode(led_pos, OUTPUT);
}
// MAIN PROGRAM
void loop() {
DisplayDateTime();
int succesRead = getID(); // read RFID tag
if(succesRead==1){ // if RFID read was succesful
//greenLED();
if (checkTag()){ // if tag is known, store data
greenLED();
DisplayTag();
// StoreData();
delay(500);
} else { // beeb an error; if new tag, then exit
errorBeep();
}
} else {
redLED();
}
}
// FUNCTIONS
void DisplayDateTime(){
DateTime time = RTC.now();
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Time:");
lcd.setCursor(6,1);
lcd.print(time.hour(),DEC);
lcd.print(":");
lcd.print(time.minute(),DEC);
lcd.print(":");
lcd.print(time.second(),DEC);
lcd.setCursor(0,0);
lcd.print("Date:");
lcd.setCursor(6,0);
lcd.print(time.day(),DEC);
lcd.print("/");
lcd.print(time.month(),DEC);
lcd.print("/");
lcd.print(time.year(),DEC);
delay(1000);
}
void DisplayTag(){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("TAG ID: ");
lcd.setCursor(8,1);
lcd.print(readTag);
delay(500);
}
void redLED(){ // red LED on, green LED off
digitalWrite(led_pos, LOW);
digitalWrite(led_neg, HIGH);
}
void greenLED(){ // red LED off, green LED on
digitalWrite(led_pos, HIGH);
digitalWrite(led_neg, LOW);
tone(buzzer, 440, 50); // sound; frequency of tone: 440 Hz, duration of tone: 50 ms
}
boolean checkTag(){ // check if tag is unknown
if(readTag == UID_tagA){UIDs_No = 1; return true;}
// else if(readTag == UID_tagB){UIDs_No = 2; return true;}
else {return false;}
}
void errorBeep(){ // error option
digitalWrite(led_pos, LOW);
digitalWrite(led_neg, LOW);
delay(150);
digitalWrite(led_neg, HIGH);
tone(buzzer, 440, 50);
delay(150);
digitalWrite(led_neg, LOW);
delay(150);
digitalWrite(led_neg, HIGH);
tone(buzzer, 440, 50);
}
int getID() { // Read RFID
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
// Serial.println(F("Scanned PICC's UID:"));
readTag = "";
for (int i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
// Serial.print(readCard[i], HEX);
readTag=readTag+String(readCard[i], HEX);
}
Serial.print(readTag);
Serial.print(" ");
DateTime time = RTC.now(); // read time from RTC
// departure[0] = time; // save departure time
Serial.print(time.day(),DEC) ;
Serial.print("/");
Serial.print(time.month(),DEC) ;
Serial.print("/");
Serial.print(time.year(),DEC) ;
Serial.print(" ");
Serial.print(time.hour(),DEC) ;
Serial.print(":");
Serial.println(time.minute(),DEC);
//Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void StoreData(){ // calculate and store data to SD card
DateTime time = RTC.now(); // read time from RTC
myFile = SD.open("A.txt", FILE_WRITE); // open file with history and write to it
if(myFile){ // format = " MM-DD-YYYY hh:mm (arrival), hh:mm (departure), hh (working hours today), hh (working hours this month);
myFile.print(readTag);
myFile.print(" ");
myFile.print(time.day(),DEC);
myFile.print("/");
myFile.print(time.month(),DEC);
myFile.print("/");
myFile.print(time.year(),DEC);
myFile.print(" ");
myFile.print(time.hour(),DEC) ;
myFile.print(":");
myFile.println(time.minute(),DEC);
myFile.close();
}
}
ตามที่ได้แนะนำไปครับ ว่าโมดูล 2 ตัวต่อแบบ SPI เหมือนกัน เช็คโคดที่ขา CS/SS ที่เป็นตัวเลือกว่าจะให้ทำงานที่ตัวไหนครับ
ไม่เข้าใจที่อธิบายเลยครับ
sd card 16gb
sd card modlue 5v
rfid-rc522 3.3v
arduino unoR3
ผมต่อ cs_sd ที่ digital 8
ต่อ cs_rfid ที่ digital 10
Code คล้ายๆความเห็นที่ 3
**************************************************
#include
#include
#include
// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define pins for SD Card
#define CS_SD 8
// Create a file to store the data
File myFile;
// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID);
// Variable to hold the tag's UID
String uidString;
void setup() {
// put your setup code here, to run once:
// Init Serial port
Serial.begin(9600);
while(!Serial); // for Leonardo/Micro/Zero
// Init SPI bus
SPI.begin();
// Init MFRC522
rfid.PCD_Init();
// Setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(CS_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
พออัพโหลดเเล้ว เชื่อมต่อ sd card ไม่ได้เหมือนเดิม
มีปัญหาตรง void setup
Initializing SD card...initialization failed!
เเก้ยังไงครับ