ATM System With Arduino Refuses To Work, Help will be much appreciated 😊

I am working on a code for an ATM Machine with an MFRC52 Reader, The code has to read points from a particular block on the rfid card which is like the debit card then Display it on an LCD display with an I2C module and when a button is pressed add one point to the cards internal storage and then display the new value on an LCD display after that, update the new value to the same block on the card which is like adding money to a debit card and then display that you can remove your card now

This is Where I have gotten so far

#include <Wire.h>  // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library for LCD display
#include <SPI.h>  // Include SPI library for MFRC52 reader
#include <MFRC522.h> // Include MFRC52 library for RFID reader

#define SS_PIN 10  // Define SS_PIN for MFRC52 reader
#define RST_PIN 9  // Define RST_PIN for MFRC52 reader

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC52 object

LiquidCrystal_I2C lcd(0x27, 16, 2); // Create LCD object with I2C address and size

const int buttonPin = 2; // Define button pin
int buttonState = 0; // Initialize button state

void setup() {
  Serial.begin(9600); // Initialize serial communication
  SPI.begin(); // Initialize SPI communication
  mfrc522.PCD_Init(); // Initialize MFRC52 reader
  lcd.init(); // Initialize LCD display
  lcd.backlight(); // Turn on backlight
}

void loop() {
  // Check if a card is present
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Card detected!");
    // Read data from the card
    byte blockNumber = 4; // Define block number to read
    byte buffer[18];
    byte size = sizeof(buffer);
    MFRC522::StatusCode status = mfrc522.MIFARE_Read(blockNumber, buffer, &size);
    if (status == MFRC522::STATUS_OK) {
      Serial.print("Points: ");
      Serial.println(buffer[0]);
      // Display data on the LCD
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Points: ");
      lcd.print(buffer[0]);
      // Wait for button press
      while (digitalRead(buttonPin) == LOW) {
        delay(10);
      }
      // Increment points and update the card
      buffer[0]++; // Increment points
      status = mfrc522.MIFARE_Write(blockNumber, buffer, 16);
      if (status == MFRC522::STATUS_OK) {
        // Display updated data on the LCD
        lcd.setCursor(0, 1);
        lcd.print("New points: ");
        lcd.print(buffer[0]);
        delay(1000);
        lcd.clear();
        lcd.print("Remove card.");
      }
    } else {
      Serial.println("Error reading data from card.");
    }
    mfrc522.PICC_HaltA(); // Halt card
    mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  }
}

But Something is wrong and I really need your help

THis is the Serial Monitor

And This is What is Happening
ezgif-3-c0e2a19f33

OKay so Update - I am trying this code that looks more promising, I really need help as i am brand new tot his stuff

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);

int buttonPin = 2;
int points = 0;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  lcd.init();
  lcd.backlight();
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Look for new cards
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    byte blockNumber = 4;
    byte trailerBlock = 7;
    byte buffer[18];
    byte size = sizeof(buffer);

    // Authenticate using key A
    if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &mfrc522.uid, &(mfrc522.keyA))) {
      // Read data from the block
      if (mfrc522.MIFARE_Read(blockNumber, buffer, &size) == MFRC522::STATUS_OK) {
        // Extract the points
        points = buffer[0];
        Serial.print("Points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Points: ");
        lcd.print(points);
        lcd.setCursor(0, 1);
        lcd.print("Hold card & press");

        // Wait for button press
        while (digitalRead(buttonPin) == HIGH) {}

        // Add ten points
        points += 10;
        Serial.print("New points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Credit Added");
        lcd.setCursor(0, 1);
        lcd.print("Remove card");
        delay(1000);

        // Write the new points to the block
        buffer[0] = points;
        if (mfrc522.MIFARE_Write(blockNumber, buffer, 16) == MFRC522::STATUS_OK) {
          Serial.println("Points updated on card");
        }
      }
      // Stop crypto1 communication
      mfrc522.PCD_StopCrypto1();
    }
    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
    delay(1000);
    lcd.clear();
  }
}

But Now Im gettin this
Compilation error: 'class MFRC522' has no member named 'keyA'

Hi,
keyA is on line 32

Where did you get this code?

What is this KeyA?

Try this code:

Added line 14 and modified line 34.

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);

int buttonPin = 2;
int points = 0;

MFRC522::MIFARE_Key keyA;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  lcd.init();
  lcd.backlight();
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Look for new cards
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    byte blockNumber = 4;
    byte trailerBlock = 7;
    byte buffer[18];
    byte size = sizeof(buffer);

    // Authenticate using key A
    if ((MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &keyA, &(mfrc522.uid))) {
      // Read data from the block
      if (mfrc522.MIFARE_Read(blockNumber, buffer, &size) == MFRC522::STATUS_OK) {
        // Extract the points
        points = buffer[0];
        Serial.print("Points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Points: ");
        lcd.print(points);
        lcd.setCursor(0, 1);
        lcd.print("Hold card & press");

        // Wait for button press
        while (digitalRead(buttonPin) == HIGH) {}

        // Add ten points
        points += 10;
        Serial.print("New points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Credit Added");
        lcd.setCursor(0, 1);
        lcd.print("Remove card");
        delay(1000);

        // Write the new points to the block
        buffer[0] = points;
        if (mfrc522.MIFARE_Write(blockNumber, buffer, 16) == MFRC522::STATUS_OK) {
          Serial.println("Points updated on card");
        }
      }
      // Stop crypto1 communication
      mfrc522.PCD_StopCrypto1();
    }
    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
    delay(1000);
    lcd.clear();
  }
}

Thanks a ton for your help, i will get back to you

Okay, Update, I am getting no response anywhere, even in the serial monitor, nothing seems to be happening

Hi,
run this code and check if the LCD reads:
"Start:"

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);

int buttonPin = 2;
int points = 0;

MFRC522::MIFARE_Key keyA;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  lcd.init();
  lcd.backlight();
  pinMode(buttonPin, INPUT_PULLUP);
  lcd.print("Start: ");
}

void loop() {
  // Look for new cards
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    byte blockNumber = 4;
    byte trailerBlock = 7;
    byte buffer[18];
    byte size = sizeof(buffer);

    // Authenticate using key A
    if ((MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &keyA, &(mfrc522.uid))) {
      // Read data from the block
      if (mfrc522.MIFARE_Read(blockNumber, buffer, &size) == MFRC522::STATUS_OK) {
        // Extract the points
        points = buffer[0];
        Serial.print("Points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Points: ");
        lcd.print(points);
        lcd.setCursor(0, 1);
        lcd.print("Hold card & press");

        // Wait for button press
        while (digitalRead(buttonPin) == HIGH) {}

        // Add ten points
        points += 10;
        Serial.print("New points: ");
        Serial.println(points);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Credit Added");
        lcd.setCursor(0, 1);
        lcd.print("Remove card");
        delay(1000);

        // Write the new points to the block
        buffer[0] = points;
        if (mfrc522.MIFARE_Write(blockNumber, buffer, 16) == MFRC522::STATUS_OK) {
          Serial.println("Points updated on card");
        }
      }
      // Stop crypto1 communication
      mfrc522.PCD_StopCrypto1();
    }
    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
    delay(1000);
    lcd.clear();
  }
}

Nope, Nothing

Hi, for when your prof. requested this project?

See if this tw code helps you earn points with your prof.


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

#define SS_PIN 10
#define RST_PIN 9
int buttonPin = 2;
int points = 0;


MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);

MFRC522::MIFARE_Key key;
//------------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC
  SPI.begin();        // Init SPI bus
  lcd.init();
  //lcd.begin(16, 2);
  lcd.backlight();
  pinMode(buttonPin, INPUT_PULLUP);
  lcd.print("Start: ");
  mfrc522.PCD_Init(); // Init MFRC522 card

  // Prepare the key (used both as key A and as key B)
  // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
  Serial.print(F("Using key (for A and B):"));
  dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
  Serial.println();

  Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}
//--------------------------------------------------------------------------------------------------
void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;

  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));

  // Check for compatibility
  if (    piccType != MFRC522::PICC_TYPE_MIFARE_MINI
          &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
          &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("This sample only works with MIFARE Classic cards."));
    return;
  }

  // In this sample we use the second sector,
  // that is: sector #1, covering block #4 up to and including block #7
  byte sector         = 1;
  byte blockAddr      = 4;
  byte dataBlock[]    = {
    0x00, 0x00, 0x00, 0x00, //  0, 0, 0, 0,
    0x00, 0x00, 0x00, 0x00, //  0, 0, 0, 0,
    0x00, 0x00, 0x00, 0x00, //  0, 0, 0, 0,
    0x00, 0x00, 0x00, 0x00  //  0, 0, 0, 0
  };
  byte trailerBlock   = 7;
  MFRC522::StatusCode status;
  byte buffer[18];
  byte size = sizeof(buffer);

  // Authenticate using key A
  Serial.println(F("Authenticating using key A..."));
  status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("PCD_Authenticate() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  // Read data from the block
  status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("MIFARE_Read() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
  }
  Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  dump_byte_array(buffer, 16); Serial.println();
  Serial.println();

  // Authenticate using key B
  Serial.println(F("Authenticating again using key B..."));
  status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("PCD_Authenticate() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  points = buffer[0];
  Serial.print("Points: ");
  Serial.println(points);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Points: ");
  lcd.print(points);
  lcd.setCursor(0, 1);
  lcd.print("Hold card & press");

  // Wait for button press
  while (digitalRead(buttonPin) == HIGH) {}

  // Add ten points
  points += 10;
  Serial.print("New points: ");
  Serial.println(points);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Credit Added");
  lcd.setCursor(0, 1);
  lcd.print("Remove card");
  delay(1000);

  dataBlock[0] = points;

  status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("MIFARE_Write() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
  }
  Serial.println();

  // Read data from the block (again, should now be what we have written)
  Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
  Serial.println(F(" ..."));
  status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("MIFARE_Read() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
  }

  Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  dump_byte_array(buffer, 16); Serial.println();

  // Halt PICC
  mfrc522.PICC_HaltA();
  // Stop encryption on PCD
  mfrc522.PCD_StopCrypto1();

  delay(1000);
  lcd.clear();
}
//------------------------------------------------------------------------------------------------------
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

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