Skip to content

- Programming for Final Project, 1-1

This page is the process of Programming for my final Project.

Notes:
When installing "Board Manager esp32" in the Arduino IDE, selecting version 2.0.8 caused an RFID-related compile error, so Please install "Board Manager esp32" version 2.0.7 or lower. (As of 2-May-2023)

1-1: RFID2 + SSR

I generated code with ChatGPT (GPT-4) to turn on the SSR when the UID of a Mifare card read by RFID2 matches, based on the original code from Week11 RFID2 Programming code(reads the Mifare card and displays the UID of the Mifare card).

alt text

Main instructions for ChatGPT (GPT-4)

  • 1.Read the card, and if the card is registered, set PIN8 to HIGH.
  • 2.After setting PIN8 to HIGH, check only whether the registered card is placed on the RFID module without checking the card's ID. If the card is present, do nothing and keep PIN8 HIGH. If the card is not present, set PIN8 to LOW.
  • 3.After setting PIN8 to LOW, return to the processing in step 1.

The completed code is as follows.

- Programming code

#include <Wire.h>
#include <MFRC522v2.h>
#include <MFRC522DriverI2C.h>
#include <MFRC522Debug.h>

const uint8_t RFID_Address = 0x28;

MFRC522DriverI2C driver{RFID_Address, Wire}; // Use the standard Wire object for I2C driver.

MFRC522 mfrc522{driver}; // Create MFRC522 instance.

const char* registeredUids[] = {
  "5E4ABC3E", // First registered UID
  "DEE6B43E"  // Second registered UID
};

const uint8_t ssr = 8;

bool uidMatches(MFRC522::Uid *uid1, const char *uid2) {
  if (uid1->size != 4) {
    return false;
  }

  for (uint8_t i = 0; i < uid1->size; i++) {
    char buf[3] = {uid2[i * 2], uid2[i * 2 + 1], '\0'};
    uint8_t uid2Byte = strtol(buf, nullptr, 16);
    if (uid1->uidByte[i] != uid2Byte) {
      return false;
    }
  }
  return true;
}

void printCardDetails(MFRC522::Uid *uid) {
  Serial.print("Card UID: ");
  for (uint8_t i = 0; i < uid->size; i++) {
    if (uid->uidByte[i] < 0x10) {
      Serial.print("0");
    }
    Serial.print(uid->uidByte[i], HEX);
  }
  Serial.println();
}

void setup() {
  Serial.begin(115200); // Initialize serial communications with the PC for debugging.
  while (!Serial);      // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4).

  Wire.begin(); // Initialize I2C with default SDA and SCL pins.

  mfrc522.PCD_Init();   // Init MFRC522 board.
  MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial); // Show details of PCD - MFRC522 Card Reader details.
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

  pinMode(ssr, OUTPUT); // Set the LED pin as output.
}

void loop() {
  bool cardPresent = mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial();

  if (cardPresent) {
    printCardDetails(&(mfrc522.uid));

    bool isRegistered = false;

    for (uint8_t i = 0; i < sizeof(registeredUids) / sizeof(registeredUids[0]); i++) {
      if (uidMatches(&(mfrc522.uid), registeredUids[i])) {
        isRegistered = true;
        break;
      }
    }

    if (isRegistered) {
      digitalWrite(ssr, HIGH); // Turn on the LED if the card is registered
      Serial.println("Access granted.");
      while (mfrc522.PICC_IsNewCardPresent()) {
        delay(100); // Check if the registered card is still present
      }
    } else {
      digitalWrite(ssr, LOW); // Turn off the LED if the card is not registered
      Serial.println("Access denied.");
    }
  } else {
    digitalWrite(ssr, LOW); // Turn off the LED if no card is present
    }
}

- I2C Address

RFID2's I2C address "0x28" is refer in Datasheet. alt text

- Wiring & Upload

  • Wired device, then I open Arduino IDE, choose "board ESP32C3". (Tools > Board > ESP32 Arduino > XIAO_ESP32C3) .Port is "/dev/cu.usbmodem1101" in this time, Then Upload the code.

I verified that the device works as programmed!!