Electronic front door lock using Arduino. Smart electronic lock on Arduino, working via Bluetooth - front door like Spiderman


Progress does not stand still and “Smart locks” are increasingly appearing on the doors of apartments, garages and houses.

A similar lock opens when you press a button on your smartphone. Fortunately, smartphones and tablets have already entered our everyday life. In some cases, “smart locks” are connected to “cloud services” like Google Drive and opened remotely. In addition, this option makes it possible to give access to opening the door to other people.

This project will implement a DIY version of a smart lock on Arduino, which can be controlled remotely from anywhere in the world.

In addition, the project has added the ability to open the lock after identifying a fingerprint. For this purpose, a fingerprint sensor will be integrated. Both door opening options will be powered by the Adafruit IO platform.

A lock like this can be a great first step in your Smart Home project.

Setting up the fingerprint sensor

To work with a fingerprint sensor, there is an excellent library for Arduino, which greatly simplifies the process of setting up the sensor. This project uses Arduino Uno. An Adafruit CC3000 board is used to connect to the Internet.

Let's start with connecting the power:

  • Connect the 5V pin from the Arduino board to the red power rail;
  • The GND pin from the Arduino connects to the blue rail on the solderless circuit board.

Let's move on to connecting the fingerprint sensor:

  • First connect the power. To do this, the red wire is connected to the +5 V rail, and the black wire to the GND rail;
  • The white wire of the sensor connects to pin 4 on the Arduino.
  • The green wire goes to pin 3 on the microcontroller.

Now let's move on to the CC3000 module:

  • We connect the IRQ pin from the CC3000 board to pin 2 on the Arduino.
  • VBAT - to pin 5.
  • CS - to pin 10.
  • After this, you need to connect the SPI pins to the Arduino: MOSI, MISO and CLK - to pins 11, 12 and 13, respectively.

Well, at the end you need to provide power: Vin - to the Arduino 5V (red rail on your circuit board), and GND to GND (blue rail on the breadboard).

A photo of the fully assembled project is shown below:

Before developing a sketch that will load data onto Adafruit IO, you need to transfer data about your fingerprint to the sensor. Otherwise, he will not recognize you in the future;). We recommend calibrating the fingerprint sensor using the Arduino separately. If this is your first time working with this sensor, we recommend that you familiarize yourself with the calibration process and detailed instructions for working with the fingerprint sensor.

If you haven't already done so, please create an account with Adafruit IO.

After this, we can move on to the next stage of developing a “smart lock” on Arduino: namely, developing a sketch that will transmit data to Adafruit IO. Since the program is quite voluminous, in this article we will highlight and consider only its main parts, and then we will provide a link to GitHub, where you can download the full sketch.

The sketch begins by loading all the necessary libraries:

#include

#include

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_CC3000.h"

#include

#include >

After this, you need to slightly correct the sketch by inserting the parameters of your WiFi network, specifying the SSID and password:

#define WLAN_SECURITY WLAN_SEC_WPA2>

In addition, you must enter your name and AIO key to log into your Adafruit IO account:

#define AIO_SERVERPORT 1883

#define AIO_USERNAME "adafruit_io_name"

#define AIO_KEY "adafruit_io_key">

The following lines are responsible for interacting and processing data from the fingerprint sensor. If the sensor was activated (the fingerprint matched), there will be "1":

const char FINGERPRINT_FEED PROGMEM = AIO_USERNAME "/feeds/fingerprint";

Adafruit_MQTT_Publish fingerprint = Adafruit_MQTT_Publish(&mqtt, FINGERPRINT_FEED);

In addition, we need to create an instance of the SoftwareSerial object for our sensor:

SoftwareSerial mySerial(3, 4);

After this we can create an object for our sensor:

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

Inside the sketch we indicate which fingerID should activate the lock in the future. This example uses 0, which corresponds to the ID of the first fingerprint used by the sensor:

int fingerID = 0;

After this, we initialize the counter and delay in our project. Essentially we want the lock to automatically engage once opened. This example uses a delay of 10 seconds, but you can adjust this value to suit your needs:

int activationCounter = 0;

int lastActivation = 0;

int activationTime = 10 * 1000;

In the body of the setup() function, we initialize the fingerprint sensor and ensure that the CC3000 chip is connected to your WiFi network.

In the body of the loop() function we connect to Adafruit IO. The following line is responsible for this:

After connecting to the Adafruit IO platform, we check the last fingerprint. If it matches and the lock is not activated, we send "1" to Adafruit IO for processing:

if (fingerprintID == fingerID && lockState == false) (

Serial.println(F("Access granted!"));

lockState = true;

Serial.println(F("Failed"));

Serial.println(F("OK!"));

lastActivation = millis();

If within the loop() function the lock is activated and we have reached the delay value indicated above, we send “0”:

if ((activationCounter - lastActivation > activationTime) && lockState == true) (

lockState = false;

if (! fingerprint.publish(state)) (

Serial.println(F("Failed"));

Serial.println(F("OK!"));

You can download the latest version of the code on GitHub.

It's time to test our project! Don't forget to download and install all the necessary libraries for Arduino!

Make sure you have made all the necessary changes to the sketch and upload it to your Arduino. After that, open the Serial Monitor window.

When the Arduino connects to the WiFi network, the fingerprint sensor will start flashing red. Place your finger on the sensor. The ID number should be displayed in the serial monitor window. If it matches, the message "OK!" will appear. This means that the data has been sent to the Adafruit IO servers.

Diagram and sketch for further configuration of the lock using the example of an LED

Now let's move on to the part of the project that is directly responsible for controlling the door lock. To connect to a wireless network and activate/deactivate the lock, you will need an additional Adafruit ESP8266 module (the ESP8266 module does not have to be from Adafruit). Using the example below, you can evaluate how easy it is to exchange data between two platforms (Arduino and ESP8266) using Adafruit IO.

In this section we will not work directly with the lock. Instead, we will simply connect the LED to the pin where the lock will be connected later. This will give us the opportunity to test our code without delving into the details of the lock design.

The scheme is quite simple: first install the ESP8266 on the breadboard. After this, install the LED. Don't forget that the long (positive) leg of the LED is connected through a resistor. The second leg of the resistor is connected to pin 5 on the ESP8266 module. We connect the second (cathode) of the LED to the GND pin on the ESP8266.

The fully assembled circuit is shown in the photo below.


Now let's look at the sketch we are using for this project. Again, the code is quite large and complex, so we will only look at its main parts:

We start by connecting the necessary libraries:

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

Configuring WiFi settings:

#define WLAN_SSID "your_wifi_ssid"

#define WLAN_PASS "your_wifi_password"

#define WLAN_SECURITY WLAN_SEC_WPA2

We also configure Adafruit IO parameters. Same as in the previous section:

#define AIO_SERVER "io.adafruit.com"

#define AIO_SERVERPORT 1883

#define AIO_USERNAME "adafruit_io_username"

#define AIO_KEY "adafruit_io_key"

We indicate which pin we connected the LED to (in the future this will be our lock or relay):

int relayPin = 5;

Interaction with the fingerprint sensor, as in the previous section:

const char LOCK_FEED PROGMEM = AIO_USERNAME "/feeds/lock";

Adafruit_MQTT_Subscribe lock = Adafruit_MQTT_Subscribe(&mqtt, LOCK_FEED);

In the body of the setup() function we indicate that the pin to which the LED is connected should operate in OUTPUT mode:

pinMode(relayPin, OUTPUT);

Within the loop() loop, we first check if we are connected to Adafruit IO:

After this, we check what signal is being received. If "1" is transmitted, we activate the pin that we declared earlier, to which our LED is connected. If we receive "0", we transfer the contact to the "low" state:

Adafruit_MQTT_Subscribe *subscription;

while ((subscription = mqtt.readSubscription(1000))) (

if (subscription == &lock) (

Serial.print(F("Got: "));

Serial.println((char *)lock.lastread);

// Save the command to string data

String command = String((char *)lock.lastread);

if (command == "0") (

digitalWrite(relayPin, LOW);

if (command == "1") (

digitalWrite(relayPin, HIGH);

You can find the latest version of the sketch on GitHub.

It's time to test our project. Don't forget to download all the required libraries for your Arduino and check if you have made the correct changes to the sketch.

To program the ESP8266 chip, you can use a simple USB-FTDI converter.

Upload the sketch to the Arduino and open the Serial Monitor window. At this stage, we simply checked whether we were able to connect to Adafruit IO: we will look at the available functionality further.

Testing the project

Now let's start testing! Go to your Adafruit IO's user menu, under the Feeds menu. Check whether the fingerprint and lock channels are created or not (in the print screen below these are the fingerprint and lock lines):


If they do not exist, you will have to create them manually.

Now we need to ensure data exchange between the fingerprint and lock channels. The lock channel must take the value "1" when the fingerprint channel takes the value "1" and vice versa.

To do this, we use a very powerful Adafruit IO tool: triggers. Triggers are essentially conditions that you can apply to configured channels. That is, they can be used to interconnect two channels.

Create a new reactive trigger from the Triggers section in Adafruit IO. This will provide the ability to exchange data between the fingerprint sensor and lock channels:


This is what it should look like when both triggers are configured:

All! Now we can actually test our project! We put our finger on the sensor and see how the Arduino began to wink with an LED that corresponds to data transmission. After this, the LED on the ESP8266 module should start blinking. This means that it has started receiving data via MQTT. The LED on the circuit board should also turn on at this moment.

After the delay you set in the sketch (the default is 10 seconds), the LED will turn off. Congratulations! You can control the LED with your fingerprint from anywhere in the world!

Setting up an electronic lock

We've reached the last part of the project: directly connecting and controlling the electronic lock using Arduino and a fingerprint sensor. The project is not easy, you can use all the sources in the form in which they are presented above, but connect a relay instead of an LED.

To directly connect the lock, you will need additional components: a 12 V power supply, a jack for connecting power, a transistor (in this example an IRLB8721PbF MOSFET is used, but you can use another one, for example, a TIP102 bipolar transistor. If you are using a bipolar transistor, you will need add a resistor.

Below is an electrical diagram for connecting all components to the ESP8266 module:


Note that if you are using a MOSFET transistor, you will not need a resistor between pin 5 of the ESP8266 module and the transistor.

The fully assembled project is shown in the photo below:


Power the ESP8266 module using the FTDI module and connect the 12V power supply to the jack. If you used the pins recommended above for connection, you won’t have to change anything in the sketch.

Now you can put your finger on the sensor: the lock should work in response to your fingerprint. The video below shows the automatic smart lock project in action:

Further development of the Smart Lock project

Our project has released remote control of a door lock using a fingerprint.

Feel free to experiment, modify the sketch and binding. For example, you can replace an electronic door lock with a relay to control the power of your 3D printer, robotic arm or quadcopter...

You can develop your "smart home". For example, remotely activate an irrigation system on Arduino or turn on the lights in a room... Don't forget that you can simultaneously activate an almost unlimited number of devices using Adafruit IO.

Leave your comments, questions and share your personal experiences below. New ideas and projects are often born in discussions!

The other day I was watching The Amazing Spider-Man and in one scene Peter Parker remotely opens and closes a door from his laptop. As soon as I saw this, I immediately realized that I also needed such an electronic lock for my front door.

After some fiddling around, I assembled a working model of a smart lock. In this article I will tell you how I assembled it.

Step 1: List of Materials





To assemble an electronic lock on Arduino you will need the following materials:

Electronics:

  • 5V wall adapter

Components:

  • 6 latch screws
  • cardboard
  • wires

Tools:

  • soldering iron
  • glue gun
  • drill
  • drill
  • pilot hole drill
  • stationery knife
  • computer with Arduino IDE program

Step 2: How the lock works

The idea is that I can open or close the door without a key, and without even going near it. But this is just the basic idea, because you can also add a knock sensor so that it reacts to a special knock, or you can add a voice recognition system!

A servo lever connected to the bolt will close it (0°) and open it (60°) using commands received via the Bluetooth module.

Step 3: Wiring Diagram


Let's first connect the servo to the Arduino board (note that although I used an Arduino Nano board, the Uno board has exactly the same pinout).

  • The brown wire of the servo is grounding, we connect it to the ground on the Arduino
  • the red wire is a plus, we connect it to the 5V connector on the Arduino
  • orange wire is the servo drive source pin, connect it to pin 9 on the Arduino

I advise you to check the operation of the servo before proceeding with the assembly. To do this, in the Arduino IDE program, select Sweep in the examples. After making sure that the servo is working, we can connect the Bluetooth module. You need to connect the rx pin of the Bluetooth module to the tx pin of the Arduino, and the tx pin of the module to the rx pin of the Arduino. But don't do it yet! Once these connections are soldered, you will not be able to upload any codes to the Arduino, so download all your codes first and only then solder the connections.

Here is the connection diagram between the module and the microcontroller:

  • Rx module – Tx board Arduino
  • Tx module – Rx board
  • Vcc (positive terminal) of the module is 3.3v of the Arduino board
  • Ground is connected to Ground (grounding to grounding)

If the explanation seems unclear to you, please follow the wiring diagram provided.

Step 4: Test

Now that we have all the working parts, let's make sure that the servo can move the latch. Before mounting the latch onto the door, I assembled a test sample to make sure the servo was powerful enough. At first it seemed to me that my servo was weak and I added a drop of oil to the latch, after which it worked fine. It is very important that the mechanism slides well, otherwise you risk being locked in your room.

Step 5: Electrical Housing



I decided to put only the controller and Bluetooth module in the case and leave the servo outside. To do this, draw the outline of the Arduino Nano board on a piece of cardboard and add 1 cm of space around the perimeter and cut it out. After this, we also cut out five more sides of the body. You will need to cut a hole in the front wall for the controller's power cord.

Case side dimensions:

  • Bottom – 7.5x4 cm
  • Cover – 7.5x4 cm
  • Left side wall – 7.5x4 cm
  • Right side wall – 7.5x4 cm
  • Front wall – 4x4 cm (with a slot for the power cord)
  • Back wall – 4x4 cm

Step 6: Application

To control the controller, you need an Android or Windows gadget with built-in Bluetooth. I did not have the opportunity to test the application on Apple devices; maybe some drivers will be needed.

I'm sure some of you have the opportunity to check this out. For Android, download the Bluetooth Terminal application, for Windows, download TeraTerm. Then you need to connect the module to your smartphone, the name should be linvor, the password should be 0000 or 1234. Once the pairing is established, open the installed application, go to options and select “Establish a connection (insecure).” Now your smartphone is an Arduino serial interface monitor, which means you can exchange data with the controller.

If you enter 0, the door will close and the message “Door is closed” will appear on the smartphone screen.
If you enter 1, you will see the door open and the screen will say "Door Open".
On Windows, the process is the same, except that you need to install the TeraTerm application.

Step 7: Install the latch


First you need to connect the servo to the latch. To do this, you need to cut off the plugs from the mounting holes of the drive housing. If we put the servo down, the mounting holes should be flush with the bolt. Then you need to place the servo lever in the latch slot, where the latch handle was. Check how the lock moves in the body. If everything is fine, secure the servo arm with glue.

Now you need to drill pilot holes for the screws in the door. To do this, attach the latch to the door and use a pencil to mark the holes for the screws on the door leaf. Drill holes for screws approximately 2.5 cm deep in the marked places. Attach the latch and secure it with screws. Check servo operation again.

Step 8: Power


To complete the device, you will need a power supply, a cord, and a mini-usb plug to connect to the Arduino.
Connect the ground pin of the power supply to the ground pin of the mini usb port, connect the red wire to the red wire of the mini usb port, then run the wire from the lock to the door hinge, and from there to the socket.

Step 9: Code

#include Servo myservo; int pos = 0; int state; int flag=0; void setup() ( myservo.attach(9); Serial.begin(9600); myservo.write(60); delay(1000); ) void loop() ( if(Serial.available() > 0) ( state = Serial.read(); flag=0; ) // if the state is "0" the DC motor will turn off if (state == "0") ( myservo.write(8); delay(1000); Serial. println("Door Locked"); ) else if (state == "1") ( myservo.write(55); delay(1000); Serial.println("Door UnLocked"); ) )

Step 10: Completed Arduino-Based Lock

Enjoy your remote control lock and don't forget to "accidentally" lock your friends in the room.

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

  • Locking mechanism. Serves to OPEN and CLOSE the door. This project explores the use of three different mechanisms:
    • Servo. There are big ones, there are small ones. Very compact, and coupled with a heavy deadbolt - an excellent option
    • Electric car door lock. It's a big and powerful thing, but it consumes crazy currents.
    • Solenoid latch. A good option because it closes itself

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

  • Button inside. Serves 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 for CLOSING the door, as well as for WAKING up to save energy. Can be placed on the door handle (palm side or finger side), on the door itself, or on the jamb
  • End cap to close the door. Serves to automatically close the lock when the door is closed. It could 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/remember a new key/combination, etc. May be hidden somewhere in the case
  • Light-emitting diode to indicate operation. RGB LED, red and green colors are used (when mixed they give yellow):
    • The green light is on - the lock is OPEN. Lights up so you don't forget to close the door
    • Yellow is on - the system has woken up and is waiting for a password to be entered
    • Flashing red - low battery

Any of these elements can be excluded from the system:

  • We remove the limit switch. In the firmware in the settings we also disable it (setting tail_button). Now you have to press a button to close the lock.
  • Remove the external button. In the firmware in the settings we also disable it (setting wake_button). Now the system does not need to be woken up, it wakes up on its own (energy consumption is slightly higher). And now we don’t have a close button on the front of the door, and we need a limit switch. Or the lock is a latch
  • 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 the code can be rewritten to suit your needs
  • Door closed, press OUTSIDE - wake up, wait for password/RFID tag/electronic key/fingerprint input
  • The door is closed, the system is awake, waiting for the password to be entered. The time can be adjusted (setting sleep_time)
  • The door is closed, a password/tag/key, etc. has been entered. - open
  • The door is closed, press INSIDE - open
  • The door is open, press OUTSIDE - close
  • The door is open, press INSIDE - close
  • The door is open, the LIMIT is pressed - close

The lock is designed to operate on battery power in a low energy saving mode (enable disable: setting sleep_enable), namely:

  • Wake up every few seconds, monitor the EVENT (optional if there is no button outside. You can enable it in the settings wake_button)
  • Monitor the battery voltage every few minutes (on/off setting battery_monitor)
  • If the battery is discharged (the 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
    • When you press a button, the red LED flashes
    • stop monitoring the EVENT (i.e. password entry/label, etc.)

When the system is not sleeping, press the password change button (hidden button). We find ourselves in password change mode:
Enter a password consisting of numbers ( MAXIMUM 10 DIGITS!!!)

  • 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 old password will remain

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

  • Password processing is done in such a way that the correct password is only counted 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 because it ends in 345.
  • If the password is entered correctly, 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 password change button in password entry mode, you will also exit it

Today is a lesson on 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, is read or written using radio signals. Any RFID system consists of a reading device (reader, reader or interrogator) and a transponder (also known as RFID tag, sometimes the term RFID tag is also used).

This tutorial will use an RFID tag with 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 ID is not equal to a predefined value, the "Unlocked" message will not appear - see photo below.

The castle is closed

The lock is open

Parts needed to create this project:

  • RFID reader RC522
  • OLED display
  • Bread board
  • Wires

Additional details:

  • Battery (powerbank)

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

Step 2: RFID Reader RC522

Each RFID tag contains a small chip (white card shown in photo). If you shine a flashlight on this RFID card, you can see the small chip and the 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 also 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. Except for this UID, each tag can store data. This type of card can store up to 1 thousand data. Impressive, isn't it? This feature will not be used today. Today, all that is of interest is identifying a specific card by its UID. The cost of the RFID reader and these two RFID cards is about $4.

Step 3: OLED Display

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

This is a very good display to use with Arduino. This is an OLED display and that means it has low power consumption. The power consumption of this display is around 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 lesson, can display two colors: yellow and blue. The top of the screen can only be yellow, and the bottom can only be 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 Vcc and GND. If you are new to Arduino and want to use an inexpensive and simple display in your project, start here.

Step 4: Connecting all the parts

Communication with the Arduino Uno board is very simple. First, let's 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 operate at 3.3V, we connect the VCC from both modules to the positive rail of the breadboard. This bus is then connected to the 3.3V output from the Arduino Uno. Then we connect both grounds (GND) to the breadboard grounding bus. Then we connect the breadboard GND bus 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 SPI interface to communicate with Arduino. So we are going to use hardware SPI pins from Arduino UNO.

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.

The RFID reader is 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 the analog pin of Pin 5 and the SDA pin on the display to the analog Pin 4. If we now turn on the project and place the RFID card near the reader, we can see that the project is working fine.

Step 5: Project Code

In order for the project code to compile, 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(Library Management). 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 needs to recognize. This is an array of integers:

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

Then we 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 there is a tag on the reader, we read its UID and print it on the display. We then compare the UID of the tag we just read with the value that is stored in the code variable. If the values ​​are the same, we will 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 using this reader or create more interesting projects, for example, so that data from a USB drive is read only after unlocking.

Introducing a door lock controlled by an RF key.

The lock works like this: Presented OUR key (RFID tag) - the lock closed, presented the key again - the lock opened. To visualize the operation of the lock, six two-color LEDs (line) were used. When closing, a red light flashes, when opening, a green light flashes. If you bring someone else's key, the red LEDs will blink.

I decided to use the car's central locking drive as an actuator. You can buy a new one, or a used one, the difference in price is not big, so I used a new one, it is more reliable. The drive rod is connected to the latch. The latch is still Soviet, strong. And I don’t need powerful “anti-vandalism”.

The “machine” is controlled via two wires. One polarity extends the rod, reverse polarity retracts the rod. At a voltage of 12 volts, the current is 6 amperes, a lot...
There are no limit switches in the "machine".

Based on the fact that the lock circuit (by design) has a guaranteed power supply, the battery is 12 volts, to ensure the operation of the lock, in the event of a loss of ~220 volts. Developed a bridge control circuit for the “machine”. A special feature of the circuit is its nonlinearity, which ensures reliable operation of the lock mechanism, and at the same time, gentle operation of the “machine” and key transistors.

In the diagram (above), the “Close” arm is highlighted in red, and the “Open” arm is highlighted in green. The arms are powered separately, through resistors (located in the power supply). Power separation of the bridge arms was introduced to eliminate false positives.

Explanation: Through 33-Ohm resistors (on the power supply diagram), a voltage of 12 volts charges the capacitors (2000-μF in each arm). When the control voltage arrives from the Arduino_ProMini controller, 168 to the “Close” input (or similarly to “Open”), through the PVT322 optocoupler - the corresponding key arm opens. In this case, the following happens: At the moment of opening the keys, the energy from the capacitors powerfully “pulls” the motor of the “machine”. As the capacitors discharge (this happens quickly), the motor of the “machine” is powered by a current limited by resistors (33 Ohms). Thanks to this, at the end of the process of “closing” - “opening” the lock, the rod moves quite slowly.

This method of engine control is optimal.

The power supply circuit is transformer. In general, the lock circuit is powered by a 12-volt, 2.8-Ah battery. And the power supply circuit maintains the battery at its nominal level. The Power LED indicates normal operation of the power supply.

All diodes are 1N4007 (I forgot to indicate on the diagram, and the person asked a question - which ones?).

(1) A maximum current limiter has been assembled. Resistor R 1 The upper current threshold is set to 300 mA.
On the integral stabilizer LM317 (2) voltage stabilizer assembled. The stabilization voltage is adjusted by a resistorR 2 . The battery voltage should be 13.7 volts.

Voltage from the battery is supplied to three points.
Through resistors (33-Ohm each) on (X), (Y) - power supply to the arms of the “driver” keys of the “machine” motor.

I assemble most of my devices from what came to hand. This project is no exception. As a housing I use the housing :) from electronic ballast:

LEDs No.-2 ... No.-7 are two-color. They are located in a line. Used to visualize the processes of “opening” and “closing” the lock. Embellishment.







2024 gtavrl.ru.