Arduino - Solenoid Lock

The Solenoid Lock is also known as the Electric Strike Lock. It can be use to lock/unlock cabinet, drawer, door. In this tutorial, we are going to learn how to control the solenoid lock using Arduino.

An alternative to the Solenoid Lock is Electromagnetic Lock. You can learn more in Arduino - Electromagnetic Lock tutorial

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Solenoid Lock

Pinout

Solenoid Lock includes two wires:

  • Positive (+) wire (red): needs to be connected to 12V of DC power supply
  • Negative (-) wire (black): needs to be connected to GND of DC power supply
Solenoid Lock Pinout

How It Works

  • When the Solenoid Lock is powered, the lock tongue (strike) is extended ⇒ the door is locked
  • When the Solenoid Lock is NOT powered, the lock tongue (strike) retracted ⇒ the door is unlocked

※ NOTE THAT:

The solenoid lock usually uses 12V, 24V or 48V power supply. Therefore, we CANNOT connect the solenoid lock directly to Arduino pin. We have to connect it to Arduino pin via a relay

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

  • When relay is open, door is unlocked
  • When relay is closed, door is locked

By connecting Arduino to the relay, we can program for Arduino to control the solenoid lock. Learn more about relay in Arduino - Relay tutorial.

Wiring Diagram

Arduino Solenoid Lock Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The real wiring diagram:

Arduino Solenoid Lock real connection

Arduino Code

The below code lock/unlock the door every 5 seconds

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-solenoid-lock */ // constants won't change const int RELAY_PIN = A5; // the Arduino pin, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 3 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // uhlock the door delay(5000); digitalWrite(RELAY_PIN, LOW); // lock the door delay(5000); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See the the lock tongue's state

Arduino - Button Controls Solenoid Lock

  • Wiring Diagram
Arduino Button Solenoid Lock Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Arduino Code
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-solenoid-lock */ #include <ezButton.h> // constants won't change const int BUTTON_PIN = 12; // Arduino pin connected to button's pin const int RELAY_PIN = A5; // Arduino pin connected to relay's pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 12; void setup() { Serial.begin(9600); // initialize serial pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds digitalWrite(RELAY_PIN, LOW); // 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, HIGH); // unlock the door in 10 seconds delay(10000); // 10 seconds digitalWrite(RELAY_PIN, LOW); // lock the door again } }

Quick Steps

  • Install ezButton library. See How To
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Press button one time.
  • See the the lock tongue's state during 10 seconds.

※ NOTE THAT:

In the above code, we used the delay function. Therefore, we do not need to debouncing for button. However, We still provide the code with debouncing just in case you want to do more tasks without using delay function. See How to use millis() instead of delay()

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES