RFID AND SD MODULE Problem in a project

Good day!

I am having a problem with saving registered RFID tags with date/time in an attendance-based project. My plan is to save the "Given name / ID# / DATE / TIME" when a registered RFID tag is tapped in. I was satisfied with what I got with the help of PLX-DAQ, but now I want to save the data on an SD card. However, I am encountering a problem after adding components for the SD module: it does not read any RFID when tapped. I looked online to see if anyone else has encountered this problem, and for a while, I read that adding a resistor to the MISO pin would work. It did work, but now what I am encountering is that it always prints out in the serial monitoring is "Tag is not enrolled." I already tried my other code with PLX-DAQ, and there seems to be no problem reading the IDs. additionally, tried testing the 2 components individually and both works fine.

I have connected the SCK, MOSI, and MISO of the SD module to the SCK, MOSI, and MISO of the RFID reader, and then added a resistor in the MISO connection to make the 2 components works.

Below is the code that should highlight my problem.

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define greenLED 2
#define redLED 7

RTC_DS3231 rtc;
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);

#define ENA 6
#define IN1 5
#define IN2 3

struct Person {
  String name;
  String id;
};

Person registeredPersons[] = {
  {"Alice", "0736d01c"},
  {"Victoria", "739588c1"},
  {"Charlie", "f38484c1"},
  {"David", "436979c1"},
  {"Eve", "038076c1"},
  {"Frank", "931873c1"},
  {"Grace", "e3fe75c1"}
};

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();

  Serial.println("Waiting for RFID tag...");
  
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}
void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 1);
  lcd.print("Date:");
  lcd.print(now.month(), DEC);
  lcd.print("/");
  lcd.print(now.day(), DEC);
  lcd.print("/");
  lcd.print(now.year(), DEC);

  lcd.setCursor(0, 2);
  lcd.print("Time:");
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);

  if (now.hour() == 17 && now.minute() == 15 && now.second() == 0) { // Door (MOTOR) closing time
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(ENA, 255); // start motor at full speed
    delay(5000); // run for 5 seconds
    analogWrite(ENA, 0); // stop the motor
  }

  if (now.hour() == 17 && now.minute() == 17 && now.second() == 0) { // Door (MOTOR) opening time
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, 255); // start motor at full speed
    delay(5000); // run for 5 seconds
    analogWrite(ENA, 0); // stop the motor
  }

  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    String tagID = "";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      tagID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
      tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
    }

    bool isEnrolled = false;
String name = "";
for (int i = 0; i < sizeof(registeredPersons)/sizeof(Person); i++) {
  if (registeredPersons[i].id == tagID) {
    isEnrolled = true;
    name = registeredPersons[i].name;
    
    Serial.print("RFID tag detected: ");
    Serial.print(name);
    Serial.print(" (");
    Serial.print(tagID);
    Serial.print(") - ");
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    // record the name, ID, date, and time in Excel
    String data = "DATA," + name + "," + tagID + "," + now.month() + "/" + now.day() + "/" + now.year() + "," + now.hour() + ":" + now.minute() + ":" + now.second();
    Serial.println(data);
    
    break;
  }
}

if (!isEnrolled) {
  Serial.println("Tag is not enrolled.");
}


    bool isLate = now.hour() >= 17 && now.minute() >= 15; // start of the late hour (change depending on the desired time)
    bool isClosing = now.hour() >= 17 && now.minute() >= 17 && now.hour() < 6; // end time of late hour (change depending on the desired time)

    lcd.setCursor(0, 0);
    lcd.print("Name:");
    lcd.print(name);

    lcd.setCursor(0, 3);
    lcd.print("Status:");
    if (isLate) {
      lcd.print("Late");
    } else {
      lcd.print(isEnrolled ? "Recorded" : "Not Enrolled"); // print "Not Enrolled" if RFID tag is not enrolled
    }

    digitalWrite(greenLED, isEnrolled && !isLate ? HIGH : LOW);
    digitalWrite(redLED, isLate ? HIGH : LOW);

    // check if it's time to spin the motor
    if (isClosing && isEnrolled) { 
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 255); // start motor at full speed
      delay(3000); // wait for 3 seconds
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 0); // stop the motor
} else if (isLate && !isClosing && isEnrolled) {
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 255); // start motor at full speed
      delay(5000); // run for 5 seconds
      analogWrite(ENA, 0); // stop the motor
      delay(3000);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      analogWrite(ENA, 255); // start motor at full speed
      delay(3000); // wait for 3 seconds
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 0); // stop the motor
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // stop the motor
}
delay(2000); // wait for 2 seconds
lcd.clear(); // clear the LCD display

mfrc522.PICC_HaltA();
} else {
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW); // turn off the red LED
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // stop the motor
}
}

Schematic


sorry I can't find the right model for some parts but hopefully it could help.

RFID Reader:
https://circuit.rocks/nfc-rfid-reader-kit-13.56mhz
SD module:
https://shopee.ph/Micro-SD-Storage-Expansion-Board-Micro-SD-TF-Card-Memory-Shield-Module-SPI-For-Arduino-Promotion-i.498112208.12411193753?sp_atk=b78c7587-0f27-456f-9ec3-f14abe129824&xptdk=b78c7587-0f27-456f-9ec3-f14abe129824

Please read and use this link: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

Always post the entire code. As all helpers are new to You code snippets are not enough.

1 Like

Many SD card modules sold for use with Arduino have a design fault which means they will not share the SPI bus with other devices. Some RFID readers may have a similar problem.

Post links to specs of the components you are using and the forum should be able to tell you if this is the problem with your devices.

I have updated the post, hopefully that should help.

No, you didn't post what I asked for. I need to follow the link to find the schematic for the SD card and RFID cards to see what components/circuit they use for voltage level conversion, if any. It is this circuit that sometimes contains the fault that prevents these modules from sharing the SPI bus.

Also did you read the forum guide as @Railroader suggested? I see that you posted your full code now, which is good, but you have not posted your schematic, as the guide suggests, or even mentioned what type of arduino you are using.

Sorry but what do you mean by the link? link of where I bought the electronics? I am new to these kind of stuff sorry. but I did put in the schematic.

Not just to the web shop, to the actual product.

It's not a schematic. I'll take a look, but I guess it will be is limited use. Google for electronics schematic to get an idea what they look like.

Hopefully that should help now, let me know if you need any more info that you need.

Thanks for posting those links. Unfortunately, the page for the SD card reader does not seem to have a schematic, so I can't tell if the module has the problem I was describing.

The page for the RFID module you gave does have a schematic. Unfortunately, it appears to be the wrong schematic. The schematic shows a USB connector, but the board pictured on the page has no USB connector.

Thanks for attempting to post a schematic. Unfortunately I can't read it, to me it's a mess of criss-crossing wires. Can you neaten it up to make it more readable, and, importantly, find the correct components corresponding to those you have?

I'll see if I could find the right rfid module for schematics, but do you think the problem lies in the electronics and not in the code?

I think the problem could lie in the electronics because some SD card modules have that fault I described. We don't know for sure because we don't have the schematic for your SD card module. That fault may be fixed with adding a resistor to the MISO pin, but that may only work with some other SPI devices. We don't know if it will work with your RFID module.

I think the problem could lie in the code. You haven't posted any code that uses both the RFID module and the SD module, so I cannot be sure about that either.

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#include <SD.h> // include the SD library
#include <SPI.h> // include the SPI library

#define SS_PIN 10
#define RST_PIN 9
#define greenLED 2                           
#define redLED 7

const int chipSelectPin = 4;
File dataFile;

RTC_DS3231 rtc;
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);

#define ENA 6
#define IN1 5
#define IN2 3

struct Person {
  String name;
  String id;
};

Person registeredPersons[] = {
  {"Alice", "0736d01c"},
  {"Victoria", "739588c1"},
  {"Charlie", "f38484c1"},
  {"David", "436979c1"},
  {"Eve", "038076c1"},
  {"Frank", "931873c1"},
  {"Grace", "e3fe75c1"}
};

bool isEnrolled = false;
String name = "";

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();

  Serial.println("Waiting for RFID tag...");
  
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);

  Serial.begin(9600);
  
  if (!SD.begin(chipSelectPin)) {
    Serial.println("Card failed, or not present");
    return;
  }

  Serial.println("Card initialized.");

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}
void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 1);
  lcd.print("Date:");
  lcd.print(now.month(), DEC);
  lcd.print("/");
  lcd.print(now.day(), DEC);
  lcd.print("/");
  lcd.print(now.year(), DEC);

  lcd.setCursor(0, 2);
  lcd.print("Time:");
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);

  if (now.hour() == 21 && now.minute() == 15 && now.second() == 0) { // Door (MOTOR) closing time
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(ENA, 255); // start motor at full speed
    delay(5000); // run for 5 seconds
    analogWrite(ENA, 0); // stop the motor
  }

  if (now.hour() == 21 && now.minute() == 21 && now.second() == 0) { // Door (MOTOR) opening time
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, 255); // start motor at full speed
    delay(5000); // run for 5 seconds
    analogWrite(ENA, 0); // stop the motor
  }

  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    String tagID = "";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      tagID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
      tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
    }

    bool isEnrolled = false;
String name = "";
 for (int i = 0; i < sizeof(registeredPersons)/sizeof(Person); i++) {
    if (registeredPersons[i].id == tagID) {
      isEnrolled = true;
      name = registeredPersons[i].name;

      Serial.print("RFID tag detected: ");
      Serial.print(name);
      Serial.print(" (");
      Serial.print(tagID);
      Serial.print(") - ");
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println();

      // record the name, ID, date, and time in the log.txt file on the SD card
      String data = name + "," + tagID + "," + now.month() + "/" + now.day() + "/" + now.year() + "," + now.hour() + ":" + now.minute() + ":" + now.second() + "\n";
dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile) {
  dataFile.print(data);
  dataFile.close();
} else {
  Serial.println("Error opening log.txt");
}
      break;
    }
  }

if (!isEnrolled) {
  Serial.println("Tag is not enrolled.");
}
    bool isLate = now.hour() >= 21 && now.minute() >= 15; // start of the late hour (change depending on the desired time)
    bool isClosing = now.hour() >= 21 && now.minute() >= 21 && now.hour() < 6; // end time of late hour (change depending on the desired time)

    lcd.setCursor(0, 0);
    lcd.print("Name:");
    lcd.print(name);

    lcd.setCursor(0, 3);
    lcd.print("Status:");
    if (isLate) {
      lcd.print("Late");
    } else {
      lcd.print(isEnrolled ? "Recorded" : "Not Enrolled"); // print "Not Enrolled" if RFID tag is not enrolled
    }

    digitalWrite(greenLED, isEnrolled && !isLate ? HIGH : LOW);
    digitalWrite(redLED, isLate ? HIGH : LOW);

    // check if it's time to spin the motor
    if (isClosing && isEnrolled) { 
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 255); // start motor at full speed
      delay(3000); // wait for 3 seconds
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 0); // stop the motor
} else if (isLate && !isClosing && isEnrolled) {
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 255); // start motor at full speed
      delay(5000); // run for 5 seconds
      analogWrite(ENA, 0); // stop the motor
      delay(3000);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      analogWrite(ENA, 255); // start motor at full speed
      delay(3000); // wait for 3 seconds
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 0); // stop the motor
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // stop the motor
}
delay(2000); // wait for 2 seconds
lcd.clear(); // clear the LCD display

mfrc522.PICC_HaltA();
} else {
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW); // turn off the red LED
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // stop the motor
}
}

I tried doing other code for checking with the same result I wanted with this one, it always assumed that the RFID tag is not enrolled even though the tag is inside the string so it basically just print out "Tag is not enrolled" in the serial monitoring and for the saving it does not save any for it read it as not enrolled.

Change it to print out the tag ID that is not recognised. I wonder if it is reading every tag as "0000000" or "ffffffff" for example.

Also please fix your code indentation, it's a mess, and that can lead to coding errors. Just click Tools->Auto Format.

bool isEnrolled = false;
String name = "";
bool tagMatched = false; // added variable to check if tag is matched or not
for (int i = 0; i < sizeof(registeredPersons) / sizeof(Person); i++) {
  if (registeredPersons[i].id == tagID) {
    isEnrolled = true;
    name = registeredPersons[i].name;
    tagMatched = true; // set tagMatched to true if tag is matched
    Serial.print("RFID tag detected: ");
    Serial.print(name);
    Serial.print(" (");
    Serial.print(tagID);
    Serial.print(") - ");
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    // record the name, ID, date, and time in the log.txt file on the SD card
    String data = name + "," + tagID + "," + now.month() + "/" + now.day() + "/" + now.year() + "," + now.hour() + ":" + now.minute() + ":" + now.second() + "\n";
    dataFile = SD.open("log.txt", FILE_WRITE);
    if (dataFile) {
      dataFile.print(data);
      dataFile.close();
    }
  }
}

// if tag is not matched with any registeredPersons, print out "Unknown" in the Serial monitor
if (!tagMatched) {
  Serial.print("Unknown RFID tag detected: ");
  Serial.print(tagID);
  Serial.print(" - ");
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}

I guess this code should do it right?

alright, I did use the auto format thank you didn't know such thing exist.

Sure.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.