Arduino Nano - Solenoid Lock

This tutorial instructs you how to use Arduino Nano to control the solenoid lock, which is also referred to as the electric strike lock. We can apply this secure cabinets, drawers, and doors.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Solenoid Lock

The Solenoid Lock Pinout

The Solenoid Lock has two wires:

  • The Positive (+) wire (red) must be connected to the 12V of the DC power supply
  • The Negative (-) wire (black) must be connected to the GND of the DC power supply
solenoid lock pinout

How It Works

  • When the Solenoid Lock is powered, the lock tongue is extended and the door is locked.
  • When the Solenoid Lock is NOT powered, the lock tongue is retracted and the door is unlocked.

※ NOTE THAT:

The solenoid lock typically requires 12V, 24V or 48V power supply. Consequently, it CANNOT be connected directly to an Arduino Nano pin. A relay must be used to connect it to the Arduino Nano pin.

If we link the solenoid lock to a relay (in the normally open mode):

  • When the relay is not activated, the door is unlocked
  • When the relay is activated, the door is locked

Connecting Arduino Nano to a relay allows us to program it to control the solenoid lock. To learn more about relays, please refer to the Arduino Nano - Relay tutorial.

Wiring Diagram

The wiring diagram between Arduino Nano and Solenoid Lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

The code below will cause the door to be locked and unlocked every five seconds.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-solenoid-lock */ #define RELAY_PIN 2 // The Arduino Nano pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin 3 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, LOW); // unlock the door delay(5000); digitalWrite(RELAY_PIN, HIGH); // lock the door delay(5000); }

Detailed Instructions

  • Copy the code and open it with Arduino IDE.
  • Click the Upload button on Arduino IDE to compile and upload the code to Arduino Nano.
  • Check out the lock tongue's condition.

Arduino Nano - Button Controls Solenoid Lock

  • Diagram of Wiring
The wiring diagram between Arduino Nano and Solenoid Lock

This image is created using Fritzing. Click to enlarge image

  • Writing code for an Arduino Nano board.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-solenoid-lock */ #include <ezButton.h> #define BUTTON_PIN 8 // The Arduino Nano pin connected to button's pin #define RELAY_PIN 2 // The Arduino Nano pin connected to relay's pin ezButton button(BUTTON_PIN); // create ezButton object for pin 12; void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds digitalWrite(RELAY_PIN, HIGH); // lock the door } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); digitalWrite(RELAY_PIN, LOW); // unlock the door in 10 seconds delay(10000); // 10 seconds digitalWrite(RELAY_PIN, HIGH); // lock the door again } }

Detailed Instructions

  • Install the ezButton library. Refer to How To for instructions.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to upload the code to the Arduino Nano.
  • Push the button once.
  • Check out the lock tongue's state over 10 seconds.

※ NOTE THAT:

In the code above, we utilized the delay function. Thus, no debouncing for the button is necessary. Nevertheless, we still included the code with debouncing in the event that you would like to perform additional tasks without making use of the delay function. For more information, please refer to How to use millis() instead of delay().

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!