Smart Electronic Castle on Arduino, working on Bluetooth - the entrance door like a spiderman. Electronic Castle on Arduino Code Castle on Arduino Uno

It so happened that we decided to install a code lock on your door at work, because we are constantly running - we run out of the office, the door to which should be closed constantly in the absence of inhabitants. Keys often turn out to be forgotten inside. In general, they decided that the code lock was an excellent way out.

I rummaged on Chinese flea markets and eBay I did not find anything cheaper and more or less serious and decided to make it my own hands. I will make a reservation immediately that the Arduino platform was chosen for its simplicity, as the experience of communication with microcontrollers was not at all.

Idea

On the door with the outside of the door there should be a keyboard on which the password is entered, the rest of the design is fixed on the inside. To control the full closure of the door is used by Geron. Going out of the cabinet, a person presses on the "*" keyboard and without waiting until the door close the closer goes on his affairs when the door is completely closed, the Geron closes and the lock will be closed. The door opens by entering the 4x digit password and clicking on "#".

Accessories

Arduino Uno \u003d $ 18
Arduino Protoshield + BreadBoard \u003d $ 6
L293D \u003d $ 1
Punch of wires 30pcs for Braddatrad \u003d $ 4
2 Sockets RJ45 \u003d $ 4
2 plugs RJ45 \u003d $ 0.5
Central locking actuator \u003d 250 rubles.
Geron \u003d freezing free from the old window.
Sewing Metal Giant Sizes \u003d Free
Case from an old hub D-Link from one and a half million iron \u003d free
Power supply from the same D-LINK hub for 12 and 5V \u003d also for free
A bunch of screws and buckets for fastening all this good to the housing \u003d 100 rubles.
The control panel from security alarm \u003d free.

TOTAL: $ 33.5 and 350 rubles.

Not so little, you will say, and you will definitely right, but for the pleasure you have to pay! And you are always nice to collect something. Before the design can be slowed down, if you use a bare MK without Arduino.

Preparation for assembly

I want to say a few words about buying a key element of the actuator design. In the local auto-store I was offered the actuators of two types: "With two wires and five". According to the saleswoman, they were absolutely the same and the difference in the number of wires did not mean absolutely nothing. However, as it turned out later, it is not so! I chose a device with two wires, it was fed from 12V. In the design with five wires, the limit switches are installed, which allows you to control the movement of the lever. I realized that I didn't bought it only when I disassembled him and change it late. The course of the lever turned out to be too short to push the tape normally, so it was necessary to modify it a bit, namely, remove two rubber washers shortening the actuator lever. For this, the body had to cut along the usual hacksaw, because the second washer was inside. Blue tape us, as always helped us in the future when assembling it back.
To control the motor motor, the L293D motors was used, which withstands the peak load of up to 1200 mA, when stopping the engine of the actuator, the peak load grown up to 600 mA.
Controls from the keyboard, dynamics and two LEDs were removed from the control alarm control panel. The console and the main device was supposed to be connected using a twisted pair and RJ45 connectors

Programming.

So, as Arduino programming experience I have not had so far. I took advantage of other people's work and articles from the site arduino.cc. Who is interested, can look at this ugly code :)

Photo and video



Arduino and actuator


Power Supply


Keyboard


Sewingale (connected to the actuator with a metal needle and on which the heat shrinking for beauty)

Video operation of the device:

Today is a lesson about how to use RFID reader with Arduino to create a simple blocking system, simple words - RFID lock.

RFID (English Radio Frequency Identification, Radio Frequency Identification) - A method for automatic identification of objects in which data stored in the so-called transponders or RFID labels is read through radio signals. Any RFID system consists of a reader (reader, reader or interrogator) and a transponder (it is also RFID label, the term RFID tag is also used).

An RFID label with Arduino will be used in the lesson. The device reads a unique identifier (UID) of each RFID tag, which we are accommodated next to the reader, and displays it on the OLED display. If the UID tag is equal to the predetermined value, which is stored in the Arduino memory, then on the display we will see the message "Unlocked" (eng., Unlocked). If a unique identifier is not equal to a predetermined value, the message "Unlocked" will not appear - see the photo below.

Castle is closed

The castle is open

Details necessary to create this project:

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

Additional details:

  • Battery (PowerBank)

The total cost of component projects was approximately 15 dollars.

Step 2: RFID reader RC522

Each RFID label has a small chip (in the photo of a white card). If you send a flashlight on this RFID card, you can see a small chip and a coil that surrounds it. This chip has no battery to produce power. It gets food from the reader wirelessly using this big coil. You can read the RFID card similar to this, from a distance of up to 20 mm.

The same chip exists in RFID tags.

Each RFID tag has a unique number that identifies it. This is a UID, which is shown on the OLED display. With the exception of this UID, each tag can store data. In this type of cards, you can store up to 1 thousand data. Impressive, isn't it? This feature will not be used today. Today, everything that interests is the identification of a specific card for its UID. The cost of RFID reader and these two RFID cards is about 4 US dollars.

Step 3: OLED display

The lesson is used by OLED monitor 0.96 "128x64 I2C.

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

The display has a resolution of 128 to 64 pixels and has a tiny size. There are two display options. One of them is monochrome, and the other, as the one that is used in the lesson, can display two colors: yellow and blue. The top of the screen can only be yellow, and the lower part is blue.

This OLED display is very bright and his excellent and very pleasant library, which is developed by AdaFruit for this display. In addition to this display uses the I2C interface, so the connection with Arduino is incredibly simple.

You only need to connect two wires, with the exception of VCC and GND. If you are new to Arduino and want to use an inexpensive and simple display in your project, start with this.

Step 4: Connect all the details

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

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

Since the display can also work by 3.3 V, we connect the VCC from both modules to the positive bus layout. Then this bus is connected to the output of 3.3 V from Arduino Uno. After that we connect both lands (GND) with a mock grounding bus. Then we connect the GND bus layout with 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 Arduino. Therefore, we are going to use SPI hardware pins from Arduino Uno.

RST output enters digital contact 9. The IRQ contact remains incoherent. Miso contact is connected to digital output 12. MOSI pin is on digital contact 11. The SCK contact switches to digital contact 13, and finally, the SDA output goes to digital output 10. That's all.

The RFID reader is connected. Now we need to connect the OLED display to Arduino using the I2C interface. Thus, the SCL output on the display proceeds to the Analog PIN 5 and SDA output on the display to analog PIN 4. If we now turn on the project and place an RF card next to the reader, we will see that the project works fine.

Step 5: Project Code

So that the project code has been compiled, we need to include some libraries. First of all, we need the MFRC522 RFID library.

To install it, go to Sketch -\u003e Include Libraries -\u003e 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 small modification. Go to the folder Arduino -\u003e LibrariesOpen the AdaFruit SSD1306 folder and edit the library Adafruitic_ssd1306.h.. Commit a string 70 and unassign the string 69, because The display has a resolution of 128x64.

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

INT Code \u003d (69,141,8,936); // UID

Then we initialize the RFID reader and the display:

Rfid.pcd_init (); display.begin (SSD1306_SWITCHCAPVCC, 0X3C);

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

If there is a tag on the reader, we read it UID and print it on the display. Then we compare the UID tag, which we just read, with the value that is stored in the code variable. If values \u200b\u200bare the same, we display 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 save more than 1 UID values \u200b\u200bso that the project recognize the more RFID labels. This is just an example.

Project Code:

#Include. #Include. #Include. #Include. #define OLED_RESET 4 ADAFRUT_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 \u003d (69,141,8,936); // this is the stored uid int coderead \u003d 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_readcardserval (); serial.print (F ("\\ Npicc Type:") ); MFRC522 :: PICC_TYPE PICCTYPE \u003d RFID.PICC_GETTYPE (RFID.UID.SAK); Serial.printLN (RFID.PICC_GETTYPENAME (PICCTYPE)); // Check Is The Picc of Classic Mifare Type If (Picctype! \u003d MFRC522 :: Picc_Type_Mifare_mini && Picctype! \u003d MFRC522 :: PICC_TYPE_MIFARE_1K && PICCTYPE! \u003d 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 \u003d String (RFID.UID.UIDBYTE) + "" + String (RFID.UID.UIDBYTE) + "" + String (RFID.UID.UIDBYTE) + "" + String (rfid.uid.uidbyte); Printuid (); int i \u003d 0; boolean match \u003d true; while (I

Step 6: Final Result

As can be seen from the lesson - you can add the RFID reader for small money to your projects. You can easily create a security system using this reader or create more interesting projects, for example, that the data from the USB disk is read only after unlocking.

The leading channel YouTube "AlexGyver" asked to make an electronic lock with their own hands. Welcome to the video cycle about the electronic locks on Arduino. In general terms, the master will explain the idea.

There are several options for creating an electronic lock system. Most often used to lock doors and boxes, cabinets. As well as to create caches and secret safes. Therefore, you need to make a layout with which it is convenient to work and can be clearly shown in detail the device system from the inside and outside. Therefore, I decided to make a frame with the door. To do this, you will need a square bar of 30 x 30. Plywood 10mm. Door hinges. Initially, I wanted to make a plywood box, but I remembered that everything was twisted with spare parts. Such a box has nowhere to put. Therefore, the layout will be made. If someone wants to put an electronic lock, then looking at the layout you can easily repeat everything.

All you need for the castle will find in this Chinese store.

The goal is to develop the most efficient schemes and firmware for electronic locks. You can use these results to install these systems on your doors, boxes, cabinets and caches.


The door is ready. Now you need to come up with how to open and close in electronic way. For these purposes, a powerful solenoid pike with AliExpress is suitable (link to the store above). If you submit to voltage conclusions, it will open. The coil resistance is almost 12 ohms, it means that at a voltage of 12 volts, the coil will eat about 1 amp. A lithium battery and an increase in the module will cope with such a task. Customize the appropriate voltage. Although you can and slightly more. The pitch is mounted on the inside of the door at a distance so as not to clinging the edge and could slam. Shekold must be a retaliatory part in the form of a metal box. It is uncomfortable to use it without it. We'll have to put a step, at least created the appearance of normal operation.

In idle mode, the junk opens normally, that is, if there is a handle on the door, we apply a pulse, open the door for the handle. But if you jump out, this method is no longer suitable. The enhancement converter does not cope with the load. To open the spring-made door, you will have to use large batteries and a more powerful converter. Either a network power supply and score on the autonomy of the system. In Chinese stores there are large sizes. They are suitable for boxes. Nutrition can be supplied using a relay or transistor mosphore, or a power key on the same transistor. More interestingly and less expensive option is a servo connected to a connecting rod with any locking element - a decorating or more serious valve. It may also be needed a piece of steel needles that performs the role of a connecting rod. Such a system does not need a high current. But it takes more space and more tricky management logic.

There are two types of servo drives. Small weak and large powerful, which can be calmly plugged into the holes in serious metal pins. Both shown options work both on the doors and on drawers. With the box will have to tinker, pulling the hole in the retractable wall.

The second part of

Progress does not stand still and "Smart Castles" are increasingly appearing on the doors of apartments, garages and houses.

A similar lock opens when you press the button on the smartphone. Fortunately, smartphones and tablets have already entered our use. In some cases, "smart locks" are connected to "cloud services" like a google disk and open remotely. In addition, this option makes it possible to give access to the opening of the door to other people.

This project will be implemented by the DIY version of the smart lock on Arduino, which can be managed remotely from any point of the ground.

In addition, the project added the ability to open the lock after the fingerprint identification. To do this, the fingerprint sensor will be integrated. Both options for opening doors will operate on the basis of the AdaFruit IO platform.

A similar castle can be an excellent first step in the project of your smart home.

Setting the fingerprint sensor

To work with the fingerprint sensor, there is an excellent library for Arduino, which greatly facilitates the process of adjusting the sensor. ARDUINO UNO is used in this project. AdaFruit CC3000 is used to connect to the Internet.

Let's start connecting power:

  • Connect the contact 5V from the Arduino board to the Red Rail;
  • The GND contact with Arduino connects to a blue rail on an inconceptuous circuit board.

Go to the connection of the fingerprint sensor:

  • First connect the power. To do this, the red wire is connected to the rail +5 V, and the black - with the Rail GND;
  • The white sensor wire connects to 4 to Arduino.
  • Green wire goes to contact 3 on a microcontroller.

Now we will deal with the CC3000 module:

  • Contact IRQ from CC3000 cards connect to Pin 2 on Arduino.
  • VBAT - to contact 5.
  • CS - to contact 10.
  • After that, you need to connect SPI contacts to Arduino: MOSI, MISO and CLK to contacts 11, 12 and 13, respectively.

Well, at the end it is necessary to provide meals: Vin - to Arduino 5V (red rail on your mounting board), and GND to GND (blue rail on the laype).

The photo of the fully assembled project is shown below:

Before developing Sketch, which will load data on Adafruit IO, you need to transfer data about your fingerprint sensor. Otherwise, in the future, he does not recognize you;). We recommend calibrating the fingerprint sensor using Arduino separately. If you work with this sensor for the first time, we recommend reading the calibration process and detailed instructions for working with the fingerprint sensor.

If you have not done this yet, you will start an account on AdaFruit IO.

After that, we can proceed to the next stage of the development of a "smart lock" on Arduino: namely, the development of the sketch, which will transmit the data on the AdaFruit IO. Since the program is quite voluminous, in the article we will highlight and consider only its main parts, and then let's give a link to GitHub, where you can download a full sketch.

Skatch begins with loading all the necessary libraries:

#Include.

#Include.

#Include.

#Include "adafruit_mqtt.h"

#InClude "adafruit_mqtt_cc3000.h"

#Include.

#Include. >

After that, you need to correctly correct the sketch, inserting the parameters of your WiFi network, specifying SSID and password (Password):

#Define Wlan_Security WLAN_SEC_WPA2\u003e

In addition, you must enter the name and AIO key (Key) to enter your ADAFRUTI IO account:

#Define Aio_Serverport 1883.

#Define Aio_USERNAME "adafruitic"

#Define Aio_Key "adafruit_io_key"\u003e

The following lines are responsible for interaction and data processing from the fingerprint sensor. If the sensor has been activated (the imprint of the coincided), will be "1":

const char fingerprint_feed progmem \u003d aio_username "/ feeds / fingerprint";

Adafruit_mqtt_publish fingerprint \u003d adafruit_mqtt_publish (& mqtt, fingerprint_feed);

In addition, you need to create an instance of the SoftWareSerial object for our sensor:

SoftWareSerial MYSERIAL (3, 4);

After that, we can create an object for our sensor:

Adafruit_fingerprint finger \u003d adafruit_fingerprint (& myserial);

Inside the Sketch, we specify which fingerid should activate the lock in the future. This example uses 0 that corresponds to the first fingerprint ID, which is used by the sensor:

iNT FINGERID \u003d 0;

After that, initialize the counter and delay (Delay) in our project. In essence, we want the lock automatically work after opening. This example uses a delay of 10 seconds, but you can adjust this value for your own needs:

iNT ActivationCounter \u003d 0;

iNT LastActivation \u003d 0;

int ActivationTime \u003d 10 * 1000;

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

In the body of the LOOP () function connect to AdaFruit IO. The following line is responsible for it:

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

if (fingerprintid \u003d\u003d FingerID && Lockstate \u003d\u003d False) (

Serial.printLN (F ("Access Granted!"));

lockstate \u003d True;

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

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

lastActivation \u003d Millis ();

If, within the LOOP () function, the lock is activated and we reached the delay value, which was indicated above, sending "0":

if ((ActivationCounter - LastActivation\u003e ActivationTime) && Lockstate \u003d\u003d True) (

lockstate \u003d 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! Do not forget to download and install all the necessary libraries for Arduino!

Make sure you have made all the necessary changes to the Sketch and download it to your Arduino. After that, open the serial monitor window.

When Arduino connects to the WiFi network, the fingerprint sensor will flash red. Legend your finger to the sensor. The Serial Monitor window should appear the ID number. If it coincides, the message will appear, "OK!". This means that the data was sent to the AdaFruit IO servers.

Scheme and Sketch for further lock setting on the example of the LED

Now we will deal with the part of the project that is directly responsible for managing the door lock. To connect to the wireless network and activate / deactivate the lock, you will need an optional ADAFRUTI ESP8266 module (the ESP8266 module does not have to be from AdaFruit). On the example, which we will consider below, you will be able to evaluate how easy it is to ensure the exchange of data between two platforms (Arduino and ESP8266) using AdaFruit IO.

In this section, we will not work directly with the lock. Instead, we simply connect the LED to the contact, where the lock will be connected hereinafter. This will give the opportunity to test our code, without deepening in the features of the castle design.

The scheme is quite simple: first set the ESP8266 on the Breadboard. After that, set the LED. Do not forget that the long (positive) leg of the LED is connected via the resistor. The second resistor foot is connected to contact 5 on the ESP8266 module. The second (cathode) of the LED is connected to the GND Pin on ESP8266.

The fully assembled scheme is shown in the photo below.


Now let's figure it out with a sketch that use for this project. Again, the code is quite voluminous and complicated, so we will consider only its main parts:

We start with the connection of the necessary libraries:

#Include.

#Include "adafruit_mqtt.h"

#Include "adafruit_mqtt_client.h"

Configure WiFi:

#Define Wlan_ssid "Your_Wifi_ssid"

#Define WLAN_PASS "Your_Wifi_Pall"

#Define Wlan_Security WLAN_SEC_WPA2.

Also configure the parameters of AdaFruit IO. Just as in the previous section:

#Define Aio_Server "Io.adafruit.com"

#Define Aio_Serverport 1883.

#Define Aio_USERNAME "adafruit_io_" user "

#Define Aio_Key "adafruit_io_key"

We indicate to which Pine we have connected the LED (in the future it will be our lock or relay):

int relaypin \u003d 5;

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

const char lock_feed progmem \u003d aio_username "/ Feeds / Lock";

ADAFRUIT_MQTT_SUBSCRIBE LOCK \u003d ADAFRUIT_MQTT_SUBSCRIBE (& MQTT, LOCK_FEED);

In the body of the setup () function, we specify that the PIN to which the LED is connected should operate in Output mode:

pinmode (Relaypin, Output);

Within the loop () cycle, first check whether we connected to AdaFruit IO:

After that, check which signal comes. If "1" is transmitted, activate the contact that we have announced earlier to which our LED is connected. If we got "0", we translate contact to the "Low" state:

Adafruit_mqtt_subscribe * subscription;

while ((Subscription \u003d MQTT.ReadSubscription (1000))) (

if (Subscription \u003d\u003d & Lock) (

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

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

// Save the command to data type data

String Command \u003d String (Char *) lock.Lastread);

if (Command \u003d\u003d "0") (

digitalWrite (Relaypin, Low);

if (Command \u003d\u003d "1") (

digitalWrite (Relaypin, High);

You can find the latest version of Sketch on GitHub.

It's time to test our project. Do not forget to download all the necessary libraries for your Arduino and check whether you have made changes to the sketch.

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

Load the Skatch on Arduino and open the serial monitor window. At this stage, we simply checked whether to connect to ADAFRUTI IO: Affordable functionality we will look further.

Test project

Now proceed to testing! Go to your Adafruit IO user menu, in Feeds menu. Check, created or no channels for the fingerprint and lock (on the print screen below it is the rows Fingerprint and Lock):


If there is no, you have to create manually.

Now we need to provide data exchange between FingerPrint and Lock channels. The Lock channel must make the value "1" when the FingerPrint channel takes the value "1" and vice versa.

To do this, use a very powerful tool AdaFruit IO: triggers. Triggers are in essence conditions that you can apply to the configured channels. That is, they can be used to interrelation of 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 channels and the castle:


This is how it should look when both triggers are customized:

Everything! Now we can really test our project! Apply your finger to the sensor and see as Arduino began to wink by the LED, which corresponds to data transmission. After that, the LED is blinking on the ESP8266 module. This means that he began to receive data via MQTT. The LED on the circuit board at this point should also turn on.

After the delay, which you installed in the Sketch (by default, this value is 10 seconds), the LED will turn off. Congratulations! You can control the LED with a fingerprint, being at any point in the world!

Customize electronic castle

We got to the last part of the project: the direct connection and control of the electronic lock using Arduino and the fingerprint sensor. The project is not easy, you can use all the sources in the form in which they are set out higher, but instead of the LED to connect the relay.

To directly connect the lock, you will need additional components: power supply to 12 V, Jack for power supply, transistor (in this example, used IRLB8721PBF MOSFET, but you can also use another, for example, the bipolar transistor TIP102. If you use a bipolar transistor, you will need Add a resistor.

Below is the electrical circuit of connecting all components to the ESP8266 module:


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

The fully assembled project is shown in the photo below:


Wave an ESP8266 module using the FTDI module and connect the power supply of 12 V to Jack. If you used the recommended pins to connect, you do not have to change anything in the sketch.

Now you can lean your finger to the sensor: the lock must work, responding to your fingerprint. The video below shows the project of an automatic "smart" castle in action:

Further development of the project "Smart Castle"

In our project released remote control of the door lock using a fingerprint.

You can safely experiment, modify the sketch and strapping. For example, you can replace the door electronic lock on the relay to manage your 3D printer, manipulator or quadcopter ...

You can develop your smart home. For example, remotely activate the irrigation system on Arduino or include light in the room ... Do not forget that you can simultaneously activate the almost unlimited number of devices using AdaFruit IO.

Leave your comments, questions and share your personal experience below. New ideas and projects are often born in the discussion!