Schematic code lock on arduino nano. Arduino electronic lock. Setting up an electronic lock

In this article I will show you how to make a combination 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, it will sound sound signal, the servo will rotate 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 its home 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

This project is modular, i.e. you can enable/disable 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 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(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

Arduino is the best system to copy any equipment. Most ideas would not have been possible without her. There has long been such an idea: to create a special combination lock on arduino. To open it, you need to hold down a certain key. In this case, the lock should not open, even if you know the right button. To open it, you must maintain certain intervals using muscle memory. A criminal cannot do such a thing. But this is all just a theory.

To assemble it, you need to use a special device of rectangular pulses, as well as several counters and a heap. But finished device would have great dimensions and it would be inconvenient to use. As a rule, such thoughts do not give rest. The first step in making the dream come true was the creation of a program for Arduino. It will serve as a combination lock. In order to open it, you will need to press not one key, but several, and do it at the same time. Finished scheme looks like this:

The picture quality is not the best, but the connection is made to ground, D3, D5, D7, D9 and D11.

The code is presented below:

Const intina = 3; const int inb = 5; const int inc = 9; const int ledPin = 13; int i = 1000; byte a = 0; byte b = 0; byte c = 0; byte d = 0; unsigned long time = 0; //don't forget anything that takes the value millis() unsigned long temp = 0; //store in unsigned long byte keya = ( 0, 0, 0, 0, 0, 0, 0, 0); //codes actually byte keyb = ( 1, 1, 1, 1, 0, 1, 0, 0); byte keyc = ( 1, 0, 1, 0, 1, 1, 1, 0); byte k = 0; void setup() ( pinMode(ina, INPUT_PULLUP); //3 inputs connected to buttons pinMode(inb, INPUT_PULLUP); pinMode(inc, INPUT_PULLUP); pinMode(ledPin, OUTPUT); //built-in LED on pin 13 pinMode(7, OUTPUT); pinMode(11, OUTPUT); digitalWrite(7, LOW); //replace ground digitalWrite(11, LOW); time = millis(); //needed for timing ) void blinktwice() ( //double blinking of the LED digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay( 200); ) void loop() ( if(k==0) ( blinktwice(); // prompt to enter code ) if (k == 8) ( digitalWrite(ledPin, HIGH); delay(3000); k = 0 ; ) a = digitalRead(ina); //read the signal levels from the buttons - pressed/not pressed b = digitalRead(inb); c = digitalRead(inc); delay(100); //next if - protection against false positives, if((digitalRead(ina) == a)&&(digitalRead(inb) ==b)&&(digitalRead(inc)==c)) ( if (a == keya[k]) ( if (b == keyb[k]) ( if (c == keyc[k]) ( k++; ) ) ) ) if (k==1) ( if (d ==0) ( time = millis (); d++; ) ) temp = millis(); temp = temp - time; if (temp > 10000) ( k= 0; d=0; time = millis (); ) )

In order to avoid unnecessary questions about the code, some points should be clarified. The setup function is used to assign ports. The next function is Input_Pullup, which is needed to increase the pin voltage by 5 V. This is done using a resistor. As a result, there will be no different short circuits. For greater convenience, it is recommended to use the blinktwice function. In general, when creating various programs you need to try other functions.

After assigning functions, the signal is read from the ports. If the button is pressed, then this will be indicated by the number 1, and if not - 2. Next, all values ​​are analyzed. For example, there is such a combination as 0,1,1. This means that the first key is pressed and the other two are not. If all values ​​are true, then condition 8 is also true. This is indicated by the illuminated LED on the front panel. Next, you need to enter a specific code that will serve to open the door.

The last elements of the code are used to reset the counter values. This function is executed if more than 10 seconds have elapsed since the last key press. Without this code, it was possible to iterate over everything possible options although there are quite a few of them. After creation this device it needs to be tested. More

Today's tutorial is about how to use an RFID reader with Arduino to create a simple locking system, in simple terms- 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.

 
Articles By topic:
Pasta with tuna in creamy sauce Pasta with fresh tuna in creamy sauce
Pasta with tuna in a creamy sauce is a dish from which anyone will swallow their tongue, of course, not just for fun, but because it is insanely delicious. Tuna and pasta are in perfect harmony with each other. Of course, perhaps someone will not like this dish.
Spring rolls with vegetables Vegetable rolls at home
Thus, if you are struggling with the question “what is the difference between sushi and rolls?”, We answer - nothing. A few words about what rolls are. Rolls are not necessarily Japanese cuisine. The recipe for rolls in one form or another is present in many Asian cuisines.
Protection of flora and fauna in international treaties AND human health
The solution of environmental problems, and, consequently, the prospects for the sustainable development of civilization are largely associated with the competent use of renewable resources and various functions of ecosystems, and their management. This direction is the most important way to get
Minimum wage (minimum wage)
The minimum wage is the minimum wage (SMIC), which is approved by the Government of the Russian Federation annually on the basis of the Federal Law "On the Minimum Wage". The minimum wage is calculated for the fully completed monthly work rate.