Experiment 1: IC card door ban

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it


Out of interest, I made a small experiment of opening the door with IC card by using Internet query and my own brain, which is also the first finished product I made. The method of how to make IC card is specially recorded here, and I will continue to update other experiments I do in the future. Please pay attention

Tip: the following is the main content of this article. The following cases can be used for reference

1, Prepare items?

An Arduino board, an induction module of RC522, a 12V electronic lock, a relay and a device that can provide 12V voltage (I use 8 1.5V batteries in series)

2, Use steps

1. Load Library

Open this interface in Arduino, and then click the management library. The following interface will appear
Install the second MFRC522. If you can't find MFRC522 in this library installer, you can download the library installation package in GITHUB to download the library.
Oil pipe address: https://www.youtube.com/watch?v=KQiVLEhzzV0
Code address: https://www.viralsciencecreativity.com/post/arduino-rfid-sensor-mfrc522-tutorial

2. Looking for source code

I found the video about the access control card in a teacher's blog, and then found the source code under the video. Of course, there are many access card source codes, and I list only one of them.
Tubing address

The code is as follows (example):

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 4 //define green LED pin because I didn't use LED lights in my experiment, I sent the relay to port 4
#define LED_R 5 //define red LED similarly, no LED is used, so the LED in the later program_ R has no effect on program results
#Define BUZZER 2 / / BUZZER pin 2 port is a defined BUZZER, and the BUZZER is still not used, so the BUZZER has no impact on the program result. 

In order to detect whether the card is detected or not, there are often cases in which the card is not detected
int i=0;

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //define servo name controls the steering gear
void setup() 
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
 // myServo.attach(3); //servo pin
 // myServo.write(0); //servo start position
  pinMode(LED_G, OUTPUT);
  digitalWrite(LED_G, HIGH);
  pinMode(LED_R, OUTPUT);
  //pinMode(BUZZER, OUTPUT);
  Serial.println("Put your card to the reader...");

void loop() 
//In order to detect whether the card is detected or not, there are often cases in which the card is not detected
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  Serial.print("Message : ");
  if (content.substring(1) == "27 42 90 B4") //change here the UID of the card/cards that you want to give access
    Serial.println("Authorized access");
    digitalWrite(LED_G, LOW);
  //  tone(BUZZER, 500);
   // noTone(BUZZER);
   // myServo.write(180);
  //  myServo.write(0);
    digitalWrite(LED_G, HIGH);
 else   {
    Serial.println(" Access denied");
   // digitalWrite(LED_R, HIGH);
   // tone(BUZZER, 300);
   // delay(1000);
    //digitalWrite(LED_R, LOW);

3. Hardware connection

Connect Arduino to RC522 according to the above figure. The physical object after connection is shown in the figure below.

4 read card number

Connect the connected hardware to the computer and read the card number of the IC card.
When uploading, pay attention to selecting the right development board and port
After uploading the program to Arduino and placing the card in RC522, open the port monitor and a string of characters will appear, as shown in the figure
Replace 27 42 90 B4 (the number may be different for different cards) to the position shown in the program

Replace the yellow part with the number just displayed by the port monitor

4 connect relay and electronic lock

Connect the electronic lock and relay as shown in the figure (changed to IN-Digital4 in the figure)

The physical object after connection is shown in the figure (it may be a little messy, hahaha, don't care)

The last step

Connect the connected hardware to the computer, upload the program to Arduino, close the IC card to RC522, and the lock of the door lock will go in.

6 warm tips

1. If the IC card cannot be detected, the electronic lock can be disconnected and tested again. There may be a problem with the connection of the electronic lock
2. Be sure to distinguish the role of the relay. The relay is a switch, and distinguish the role of each port of the relay.
3. If there is a problem, you must be patient to read the program at once and understand the logical relationship in the program. If you do not put the card lock in and the card lock comes out, it must be because the logic is wrong. You only need to change the logic in the program.


This is the first time I have written so much experimental experience (I don't have enough brain, think of the word first, ha ha ha). I'm sure there are many mistakes in language and logic. Take your time, ha ha ha ha. After that, there will be more small experiments, the language will be more accurate, the description will be clearer and the logic will be clearer. It allows readers to easily make what they want to do. Thanks for watching, hahaha. If there is anything I don't understand, please leave a message in the comment area and I will answer it. I am also a vegetable chicken and a Xiaobai, ha ha ha. I wish you all good night!!!

Tags: arduino

Posted on Thu, 11 Nov 2021 15:36:43 -0500 by robinas