Mixer      03/05/2020

Making an RFID lock using Arduino. Arduino electronic lock Arduino safe lock

In this lesson we will learn how to do simple system, which will unlock the lock using an electronic key (Label).

In the future, you can refine and expand the functionality. For example, add the function "adding new keys and removing them from memory". In the base case, consider a simple example, when a unique key identifier is pre-specified in the program code.

In this tutorial we will need:

To implement the project, we need to install the libraries:

2) Now you need to connect the Buzzer, which will give a signal if the key worked and the lock opens, and the second signal when the lock closes.

We connect the buzzer in the following sequence:

Arduino Buzzer
5V VCC
GND GND
pin 5 IO

3) A servo will be used as the unlocking mechanism. Any servo can be chosen, depending on the dimensions you require and the forces that the servo creates. The servo has 3 pins:

More clearly, you can see how we connected all the modules in the picture below:

Now, if everything is connected, then you can proceed to programming.

Sketch:

#include #include #include // "RFID" library. #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); unsigned long uidDec, uidDecTemp; // to store the tag number in decimal format Servo servo; void setup() ( Serial.begin(9600); Serial.println("Waiting for card..."); SPI.begin(); // SPI initialization / Init SPI bus. mfrc522.PCD_Init(); // initialization MFRC522 / Init MFRC522 card.servo.attach(6); servo.write(0); // set servo to closed state ) void loop() ( // Search for a new label if (! mfrc522.PICC_IsNewCardPresent()) ( return; ) // Label selection if (! mfrc522.PICC_ReadCardSerial()) ( return; ) uidDec = 0; // Issue serial number labels. for (byte i = 0; i< mfrc522.uid.size; i++) { uidDecTemp = mfrc522.uid.uidByte[i]; uidDec = uidDec * 256 + uidDecTemp; } Serial.println("Card UID: "); Serial.println(uidDec); // Выводим UID метки в консоль. if (uidDec == 3763966293) // Сравниваем Uid метки, если он равен заданому то серва открывает. { tone(5, 200, 500); // Делаем звуковой сигнал, Открытие servo.write(90); // Поворациваем серву на угол 90 градусов(Отпираем какой либо механизм: задвижку, поворациваем ключ и т.д.) delay(3000); // пауза 3 сек и механизм запирается. tone(5, 500, 500); // Делаем звуковой сигнал, Закрытие } servo.write(0); // устанавливаем серву в закрытое сосотояние }

Let's analyze the sketch in more detail:

In order to find out the UID of the card (Labels), you need to write this sketch to arduino, assemble the circuit described above, and open the Console (Monitoring of the serial port). When you bring the tag to the RFID, a number will be displayed in the console

The resulting UID must be entered in the following line:

If (uidDec == 3763966293) // Compare the Uid of the label, if it is equal to the specified one, then the servo opens the valve.

For each card, this identifier is unique and does not repeat. Thus, when you present a card whose ID you have set in the program, the system will open access using a servo.

Video:

In this article I will show you how to do code lock from arduino. To do this, we need red and green LEDs, a buzzer, an arduino nano, an LCD display with an I2C converter, a servo drive and a 4x4 matrix keyboard. When turned on, the display will write "Enter code.",

the red LED will turn on,

and the green turns off, the servo will be set to 0°. When numbers are entered, * will light up on the display.

If the code is entered incorrectly, the display will show "Enter cod.". If the code is correct, a beep will sound, the servo will turn 180°, the display will read "Open."

the green LED will turn on,

and red will turn off. After 3 seconds, the servo will return to initial position, the red LED will turn on and the green LED will turn off, the display will write "Close.",

then the display will write "Enter code.". Now about the scheme. First we connect the arduino with wires with breadboard(power contacts).

Then we connect a matrix keyboard to contacts D9 - D2.

Then the servo. We connect it to pin 10.

Red LED to pin 11.

Green - to pin 12.

Buzzer - to pin 13.

Now upload the sketch.

#include #include #include #include iarduino_KB KB(9, 8, 7, 6, 5, 4, 3, 2); LiquidCrystal_I2C lcd(0x27, 16, 2); Servo servo int pass = (3, 6, 1, 8); int in; int r = 11; int g = 12; void setup() ( KB.begin(KB1); pinMode(r, OUTPUT); pinMode(g, OUTPUT); lcd.init(); lcd.backlight(); digitalWrite(g, LOW); digitalWrite(r, HIGH ); servo.attach(10); servo.write(0); lcd.setCursor(0, 0); ) void loop() ( lcd.clear(); lcd.print("Enter code."); while ( !KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.clear(); lcd.setCursor(0, 0); lcd.print("*"); while (!KB. check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.print("*"); while (!KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.print("*"); while (!KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.print("*"); if (in == pass) ( if (in == pass) ( if (in == pass) ( if (in == pass) ( lcd.clear(); lcd.setCursor(0, 0); lcd.print("Open."); tone( 13, 400, 750); servo.write(180); digitalWrite(r, LOW); digitalWrite(g, HIGH); delay(3000); lcd.clear(); lcd.setCursor(0, 0); lcd. print("Close."); tone(13, 300, 700); servo.write(0); digitalWrite(g, LOW); digitalWrite(r, HIGH); delay(1000); ) ) ) ) )

That's all. Enjoy code lock!

List of radio elements

Designation Type Denomination Quantity NoteShopMy notepad
E1 Arduino board

Arduino Nano 3.0

1 5V To notepad
E8, E9 Resistor

220 ohm

2 smd To notepad
E6 Light-emitting diode

AL102G

1 Red To notepad
E7 Light-emitting diode

AL307G

1 Green To notepad
E3 LCD displayWith I2C interface1 Green backlight To notepad
E5 ServoSG901 180 degrees To notepad
E2 Buzzer5V1 Bu To notepad
E4 Keyboard4x41 matrix To notepad
No BreadBoard640 points1 without soldering
It just so happened that at work we decided to install a combination lock on our door, because, as we constantly run in, we run out of the office, the door to which must be constantly closed in the absence of inhabitants. Keys are often forgotten inside. In general, we decided that a combination lock is a great way out.

After rummaging through Chinese flea markets and ebay, I didn’t find anything cheap and more or less serious and decided to make it myself. I will make a reservation right away that the Arduino platform was chosen for its simplicity, since there was no experience at all with microcontrollers.

Idea

On the door, on the outside of the door, there must be a keypad on which the password is entered, with inside fixed the rest of the structure. A reed switch is used to control the complete closing of the door. Leaving the office, a person presses “*” on the keyboard and, without waiting for the door to close, the door closer goes about his business, when the door is completely closed, the reed switch will close and the lock will be closed. The door is opened by entering a 4-digit password and pressing "#".

Accessories

Arduino UNO = $18
Arduino protoshield + breadboard = $6
L293D = $1
Bundle of wires 30pcs for bredboard = $4
2 RJ45 sockets = $4
2 RJ45 plugs = $0.5
central lock actuator = 250 rubles.
Reed switch = torn free of charge from the old window.
Espagnolette metal gigantic sizes = free
Case from an old D-LINK hub made of 1.5 mm iron = free
Power supply from the same D-LINK hub for 12 and 5v = also free
A bunch of screws and nuts for attaching all this stuff to the case = 100 rubles.
Control panel from burglar alarm= free.

Total:$33.5 and 350 rubles.

Not so little, you say, and you will definitely be right, but you have to pay for pleasure! And it's always nice to collect something with your own hands. In addition, the design can be greatly reduced in price if you use a bare MK without an Arduino.

Assembly preparation

I would like to say a few words about the purchase of a key element of the actuator design. At a local auto shop, they offered me two types of actuators: "with two wires and with five." According to the saleswoman, they were exactly the same and the difference in the number of wires meant absolutely nothing. However, as it turned out later, this is not so! I chose a device with two wires, it was powered by 12v. The five-wire design features limit switches to control the movement of the lever. I realized that I bought the wrong one only when I took it apart and it was too late to change it. The stroke of the lever turned out to be too short to properly push the latch, therefore, it was necessary to modify it a little, namely, to remove two rubber washers shortening the stroke of the actuator lever. To do this, the body had to be sawn along with an ordinary hacksaw, because the second washer was inside. The blue duct tape to us, as always, helped us later on when assembling it back.
To control the actuator motor, the L293D motor driver was used, which can withstand a peak load of up to 1200 mA, in our case, when the actuator motor was stopped, the peak load increased to only 600 mA.
Contacts from the keyboard, speaker and two LEDs were removed from the control panel from the security alarm. The remote control and the main device were supposed to be connected using a twisted pair and RJ45 connectors

Programming.

Just like experience Arduino programming I haven't had it yet. I took advantage of other people's developments and articles from the arduino.cc website. Who cares, can look at this ugly code :)

Photo and video



Arduino and actuator


power unit


Keyboard


Espagnolette (connected to the actuator with a metal needle and on which heat shrink is put on for beauty)

Video of the device operation process:

Today's lesson is about how to use an RFID reader with Arduino to create a simple locking system, in simple words - an RFID lock.

RFID (English Radio Frequency IDentification, radio frequency identification) is a method of automatic identification of objects in which data stored in so-called transponders, or RFID tags, are read or written using radio signals. Any RFID system consists of a reader (reader, reader or interrogator) and a transponder (aka RFID tag, sometimes the term RFID tag is also used).

The tutorial will use an RFID tag with an Arduino. The device reads the unique identifier (UID) of each RFID tag that we place next to the reader and displays it on the OLED display. If the UID of the tag is equal to the predefined value that is stored in the Arduino memory, then we will see the message “Unlocked” on the display. If the unique identifier is not equal to the predefined value, the "Unlocked" message will not appear - see photo below.

The castle is closed

The castle is open

Details needed to create this project:

  • RFID Reader RC522
  • OLED display
  • Bread board
  • wires

Additional details:

  • Battery (power bank)

The total cost of the project components was approximately $15.

Step 2: RFID Reader RC522

Each RFID tag has a small chip (white card in the photo). If you point a flashlight at this RFID card, you can see a small chip and a coil that surrounds it. This chip does not have a battery to generate power. It receives power from the reader wirelessly using this large coil. It is possible to read an RFID card like this from up to 20mm away.

The same chip exists in RFID key fob tags.

Each RFID tag has a unique number that identifies it. This is the UID that is shown on the OLED display. With the exception of this UID, each tag can store data. This type of card can store up to 1,000 data. Impressive, isn't it? This feature will not be used today. Today, all that is of interest is the identification of a particular card by its UID. The RFID reader and these two RFID cards cost about $4.

Step 3OLED Display

The tutorial uses a 0.96" 128x64 I2C OLED monitor.

This is a very good display to use with Arduino. It's an OLED display and that means it has low power consumption. The power consumption of this display is about 10-20mA and it depends on the number of pixels.

The display has a resolution of 128 by 64 pixels and is tiny in size. There are two display options. One of them is monochrome, and the other, like the one used in the tutorial, can display two colors: yellow and blue. The top of the screen can only be yellow and the bottom blue.

This OLED display is very bright and has a great and very nice library that Adafruit has developed for this display. In addition to this, the display uses an I2C interface, so connecting to the Arduino is incredibly easy.

You only need to connect two wires except for Vcc and GND. If you're new to Arduino and want to use an inexpensive and simple display in your project, start here.

Step 4: Putting All the Parts Together

Communication with the Arduino Uno board is very simple. First, connect the power to both the reader and the display.

Be careful, the RFID reader must be connected to the 3.3V output from the Arduino Uno or it will be damaged.

Since the display can also run at 3.3V, we connect the VCC from both modules to the positive breadboard rail. This rail is then connected to the 3.3V output from the Arduino Uno. Then we connect both grounds (GND) to the breadboard ground bus. We then connect the GND bus of the breadboard to the Arduino GND.

OLED Display → Arduino

SCL → Analog Pin 5

SDA → Analog Pin 4

RFID Reader → Arduino

RST → Digital Pin 9

IRQ → Not connected

MISO → Digital Pin 12

MOSI → Digital Pin 11

SCK → Digital Pin 13

SDA → Digital Pin 10

The RFID reader module uses the SPI interface to communicate with the Arduino. So we are going to use the Arduino UNO hardware SPI pins.

The RST pin goes to digital pin 9. The IRQ pin remains disconnected. The MISO pin goes to digital pin 12. The MOSI pin goes to digital pin 11. The SCK pin goes to digital pin 13, and finally the SDA pin goes to digital pin 10. That's it.

RFID reader connected. Now we need to connect the OLED display to the Arduino using the I2C interface. So the SCL pin on the display goes to analog Pin 5 and the SDA on the display to analog Pin 4. If we now turn on the project and place the RFID card next to the reader, we can see that the project is working fine.

Step 5: Project Code

In order for the project code to be compiled, we need to include some libraries. First of all, we need the MFRC522 Rfid library.

To install it, go to Sketch -> Include Libraries -> Manage libraries(Management of libraries). Find MFRC522 and install it.

We also need the Adafruit SSD1306 library and the Adafruit GFX library for display.

Install both libraries. The Adafruit SSD1306 library needs a little modification. Go to folder Arduino -> Libraries, open the Adafruit SSD1306 folder and edit the library Adafruit_SSD1306.h. Comment out line 70 and uncomment line 69 because The display has a resolution of 128x64.

First, we declare the value of the RFID tag that the Arduino should recognize. This is an array of integers:

int code = (69,141,8,136); // UID

We then initialize the RFID reader and display:

Rfid.PCD_Init(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

After that, in the loop function, we check the tag on the reader every 100ms.

If the reader has a tag, we read its UID and print it on the display. We then compare the UID of the tag we just read with the value stored in the code variable. If the values ​​are the same, we display the UNLOCK message, otherwise we will not display this message.

If(match) ( Serial.println("\nI know this card!"); printUnlockMessage(); )else ( Serial.println("\nUnknown Card"); )

Of course, you can change this code to store more than 1 UID value so that the project recognizes more RFID tags. This is just an example.

Project code:

#include #include #include #include #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #define SS_PIN 10 #define RST_PIN 9 MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; int code = (69,141,8,136); //This is the stored UID int codeRead = 0; String uidString; void setup() ( Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // Clear the buffer.display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(10,0); display.print("RFID Lock"); display.display(); ) void loop() ( if(rfid.PICC_IsNewCardPresent()) ( readRFID(); ) delay(100); ) void readRFID() ( rfid.PICC_ReadCardSerial(); Serial.print(F("\nPICC type: ") ); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); Serial.println(rfid.PICC_GetTypeName(piccType)); // Check is the PICC of Classic MIFARE type if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) ( Serial.println(F("Your tag is not of type MIFARE Classic.")); return; ) clearUID(); Serial.println(" Scanned PICC"s UID:"); printDec(rfid.uid.uidByte, rfid.uid.size); uidString = String(rfid.uid.uidByte)+" "+String(rfid.uid.uidByte)+" "+ String(rfid.uid.uidByte)+ " "+String(rfid.uid.uidByte); printUID(); int i = 0; boolean match = true; while(i

Step 6: Final Result

As you can see from the lesson - for little money you can add an RFID reader to your projects. You can easily create a security system with this reader or create more interesting projects, for example, so that data from a USB drive is read only after unlocking.

This project is modular, i.e. you can connect / disconnect different elements and get different functionality. The pictures above show a variant with full functionality, namely:

  • locking mechanism. Used to OPEN and CLOSE the door. This project considers the use of three different mechanisms:
    • Servo. There are big ones, there are small ones. Very compact, and coupled with a heavy deadbolt - a great option
    • Electric car door lock. A big and powerful thing, but it just eats insane currents
    • Solenoid latch. A good option, as it closes itself

    In the firmware settings, you can select any of the three types (setting lock_type)

  • Button inside. Used to OPEN and CLOSE the door from the inside. Can be placed on the door handle (palm side or finger side), on the door itself, or on the jamb
  • Button outside. Serves to CLOSE the door, as well as to WAKE up from energy saving. Can be placed on the door handle (palm side or finger side), on the door itself, or on the jamb
  • trailer for closing the door. Serves for automatic closing of the lock when the door is closed. They may be:
    • Tact button
    • Hall sensor + magnet on the door itself
    • Reed switch + magnet on the door itself
  • secret access reset button. Used to reset the password / enter a new password / memorize a new key / combination, etc. May be hidden somewhere in the body
  • Light-emitting diode to indicate operation. RGB LED, red and green colors are used (when mixed they give yellow):
    • Green on - the lock is OPEN. Lit to remember to close the door
    • Solid yellow - the system is awake and waiting for a password
    • Flashing red - low battery

Any of these elements can be excluded from the system:

  • We remove the trailer. In the firmware in the settings, we also turn it off (setting tail_button). Now to close the lock, you need to press the button
  • Remove the outside button. In the firmware in the settings, we also turn it off (setting wake_button). Now the system does not need to be woken up, it wakes up by itself (power consumption is slightly higher). And also we no longer have a close button on the front of the door, and we need a limit switch. Either the castle is a heck
  • We remove the internal button. This option is suitable for cabinets and safes. You don't need to change anything in the settings.
  • We remove the LED. You don't need to change anything in the settings.
  • The access reset button can be unsoldered after the first use, or you can rewrite the code for yourself
  • Door closed, pressed OUTSIDE - wake up, wait for password entry/RFID tag/electronic key/fingerprint
  • The door is closed, the system has woken up, waiting for the password to be entered. The time can be set (setting sleep_time)
  • Door closed, password/tag/key entered, etc. - open
  • Door closed, pressed INSIDE - open
  • Door open, pressed OUTSIDE - close
  • Door open, pressed INSIDE - close
  • The door is open, the LIMIT switch is pressed - close

The lock is designed to operate on battery power in low power saving mode (Enable Disable: setting sleep_enable), namely:

  • Wake up every few seconds, follow the EVENT (optional, if there is no button outside. You can enable it in the settings wake_button)
  • Monitor battery voltage every few minutes (on/off setting battery_monitor)
  • If the battery is discharged (voltage is set in the setting bat_low):
    • open the door (optional, can be configured in the firmware open_bat_low)
    • prohibit further opening and closing
    • flashing red LED when pressing the button
    • stop following an EVENT (i.e. password entry/label, etc.)

When the system is awake, press the change password button (hidden button). We fall into password change mode:
Enter a password from numbers ( MAXIMUM 10 NUMBERS!!!)

  • When you press *, the password is written to memory and the system exits the password change
  • When you press #, the password is reset (you can enter it again)
  • If you do not press anything for 10 seconds, we will automatically exit the password change mode, the password will remain the old one

When the system is not sleeping (woke up by the button or sleep is disabled), press * to enter the password entry mode
If the system sleeps and periodically wakes up to check the EVENT, then press * and hold until the red LED lights up
Password input mode:

  • Password processing is done in such a way that the correct password is counted only when the correct sequence of numbers is entered, that is, if the password is 345, then you can enter any numbers until the sequence 345 appears, i.e. 30984570345 will open the lock since it ends in 345.
  • If the password is correct, the door will open
  • If you do not press anything, after 10 seconds the system will return to normal (standby) mode
  • If you press #, we will immediately exit the password entry mode
  • If you press the secret button to change the password in the password entry mode, then we will also exit it