Hello. I'm working on an ARDUINO R4 WIFI RFID magnetic lock that opens when a tag is brought to the reader.
To track how the lock works I use serial monitor that shows that my tag is read and I get access but my lock doesn't react/doesn't open.
I guess I messed up my code which is quite simple. Yet, I don't see what I did wrong. Could anyone check the code please?
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5
#define SS_PIN 10
#define LOCK_PIN 4
byte readCard[4];
String MasterTag = "638CB6F5";
String tagID = "";
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
// Initiating
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
Serial.begin(9600); // LCD screen
Serial.print(" Access Control ");
Serial.print("Scan Your Card>>");
}
void loop()
{
//Wait until new tag is available
while (getID())
{
if (tagID == MasterTag)
{
Serial.print(" Access Granted!");
digitalWrite(LOCK_PIN, HIGH);
// You can write any code here like opening doors, switching on a relay, lighting up an LED, or anything else you can think of.
}
else
{
Serial.print(" Access Denied!");
}
Serial.print(" ID : ");
Serial.print(tagID);
delay(2000);
Serial.print(" Access Control ");
Serial.print("Scan Your Card>>");
}
}
//Read new tag if available
boolean getID()
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
Haha. My bad, some coder I am. I'll fix that, thanks for pointing it out.
I've got an external 5V, 1.5A power supply. I connected the lock to it directly and it worked/ it opened. The thing is my mechanic relay doesn't have that "clicking" sound when it gets voltage from IO. I tried that relay with almost the same wiring to light up an LED and it worked just fine.
Here is a sketch I use to to light up the LED
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#define LED_BUILTIN 4 // Configurable, set LED pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
No. You should take some time to look at some examples and learn the basics of using digital IO on an Arduino.
You're right. I definitely need to do that.
You need a line in setup to set the pinMode to OUTPUT. You still need the #define in order to use LOCK_PIN in place of the number 4. pinMode(LOCK_PIN, OUTPUT)
I added pinMode(LOCK_PIN, OUTPUT) to void setup() and it worked. Now, I have another issue - my lock can only work for 5-10 sec. so I added delay(2000); and digitalWrite(LOCK_PIN, LOW); to my sketch. The sketch opens the lock for 2 sec then closes it and if I place a tag to the reader again the lock doesn't respond. It seems that the reader stops reading ... What am I missing?
Here is my new sketch
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5
#define SS_PIN 10
#define LOCK_PIN 4
byte readCard[4];
String MasterTag = "638CB6F5";
String tagID = "";
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
// Initiating
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
Serial.begin(9600); // LCD screen
Serial.print(" Access Control ");
Serial.print("Scan Your Card>>");
pinMode(LOCK_PIN, OUTPUT);
}
void loop()
{
//Wait until new tag is available
while (getID())
{
if (tagID == MasterTag)
{
Serial.print(" Access Granted!");
digitalWrite(LOCK_PIN, HIGH);
delay(2000);
digitalWrite(LOCK_PIN, LOW);
// You can write any code here like opening doors, switching on a relay, lighting up an LED, or anything else you can think of.
}
else
{
Serial.print(" Access Denied!");
}
Serial.print(" ID : ");
Serial.print(tagID);
delay(2000);
Serial.print(" Access Control ");
Serial.print("Scan Your Card>>");
}
}
//Read new tag if available
boolean getID()
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
Ok. Post the modified code. You should know by now that you need to show the code you're using if you want someone to help with it. If I guess at how you made those changes then I could end up with something completely different. You may have made a mistake there. In fact we know you did because it doesn't work as you expect.
This is a part of getID() function. Once it scans the new card, inside the for loop it converts the 4 bytes of the uid into a string and concatenates to form a single string.
I know it's part of the function you copied from somewhere. But what does that particular line do? Look at the library docs. Do you want that line in your code?
Go look at the documentation for the library you're using. Open up the .h and .cpp file and take a look and see if there are comments. Go to the github page for it and see if there is a readme that describes the functions.