Electronic Door Lock – ElectroSchematics.com

Do you need to use a pushbutton to operate an electronic cabinet door lock?

In this post, I will show you how to build a simple electronic door lock using an Arduino microcontroller and a small door lock solenoid. Follow the instructions and schematic(s) below to get the hardware setup before you dive into the software part of the project.

Key Things You Will Need:

  • Arduino UNO R3 x1
  • Door lock Solenoid (12V) x1
  • Grove Yellow LED Button x 1
  • IRF9540 P-channel Power MOSFET x1
  • PC817 Optocoupler x 1

Arduino Hardware Setup

This is the schematic of the Arduino segment of the project – the first thing you need to setup!

As you can see, the circuitry has a single 5-pin connector (CN1) for both the Grove Yellow LED button and the door lock solenoid driver module. This connector also provides regulated 5VDC to the Grove module, originally supplied by the onboard 5V regulator of the Arduino UNO board. The red indicator (LED1) is an extension of the Arduino’s built-in D13 LED which is an optional component.

This setup can be powered from a regular 6F22 9V battery (or a similar power source) through the Arduino’s DC barrel jack input port.

Lock Solenoid Driver Setup

Next thing you need to setup is the door lock solenoid assembly which consists of the door lock solenoid and its driver circuitry. The schematic, as shown below, is simple, self-explanatory and straightforward!

As you can see, this is a P-channel Power MOSFET based crude load switch that has a galvanically-isolated input port. Here, the optocoupler PC817 (PC1) is driven by the control signal from D9 I/O pin of Arduino UNO, and the door lock solenoid, connected to CN2, is driven by the IRF9540 Power MOSFET (T1). The green LED is the door lock status indicator which lights up when the door lock in opened (released) state. The freewheeling diode D1 (1N4007 or MUR340) must be included as it provides protection against the back emf spike which occurs when the door lock solenoid is turned off.

This setup must be powered by an independent power supply block which is capable of catering at least 1A of current at 12VDC.

A load switch is an electronic component that has no moving parts, which works somewhat like a relay. Generally, a pair of transistors act like the switching element, one of them being a P-channel Power MOSFET, and the other an N-channel MOSFET or a small signal NPN BJT.

In other words, a load switch is comprised of two main elements – the pass transistor and the on/off control mechanism. The pass transistor is most commonly a P-Channel Power MOSFET that passes the voltage supply to a specified load when the transistor is on. There are multiple ways to implement the load switch mechanism but here a logic-high (H) signal from an Arduino circuitry is used to turn a P-Channel Power MOSFET on via an optocoupler.

That means, when input is high (H), the optocoupler turns on, the pass transistor gate is pulled to ground, and the load switch turns on, if the input voltage rail is higher than the threshold voltage of the PMOS pass transistor. When input is logic-low (L), the pass transistor is off, and its gate is pulled up to VIN. That is it!

Key Parts Explained

The Arduino UNO R3 hardly needs an explanation. I used the SMD version simply because I’ve a few “knockoffs” handy.

The 12V door lock solenoid (solenoid door lock) has a slug with a slanted cut and a good mounting bracket. It is basically an electric lock, designed for a basic cabinet or door. When 12VDC is applied, the slug pulls in so it does not stick out and the door can be opened.

Solenoids are basically electromagnets. They are made of a big coil of copper wire with an armature (a slug of metal) in the middle. When the coil is energized, the slug is pulled into the center of the coil, and this makes the solenoid able to pull from one end.

Sample Datasheet http://www.farnell.com/datasheets/2865763.pdf

This is the quick specs of the door lock solenoid used by me:

  • Operating voltage: 12VDC
  • Current Consumption: 750mA approx.
  • Coil Resistance: 16Ω approx.
  • Designed for 1-10 seconds long activation.
  • Turning the current on will unlock and turning it off will lock the mechanism.

The Grove Yellow LED button is nothing but a lighted-momentary pushbutton switch (It does not matter, you can use other colors such as red, green, or blue).

This module is useful to use the LED to show the status of the button. There is a small-signal N-Channel MOSFET wired onboard to control the LED. Below you can see the schematic of the Grove Yellow LED Button Module. For further details, checkout this link https://wiki.seeedstudio.com/Grove-LED_Button/

Let’s go to the next session – the Software!

The Arduino Sketch/Door Lock Code

There are two ways to implement door lock control button function using Arduino. Before I go any further though, I would like to thank an unknown maker for posting the base code on a forum that I adapted precisely for this project.

Here is the complete code, I recommended looking it over first, and then tweak it for your proposed project/application, if necessary.



* Electronic Door Lock v1

* Arduino Uno R3 + Grove Yellow LED Button

* & Solenoid Door Lock Driver v1(IRF9540)

* TKHareendran/06.2021


const int KEY_PIN = 8;//IN::Grove

const int DRIVE_PIN = 9;//OUT::MOSFET

const int GLED_PIN = 13;//Debug

const int YLED_PIN = 12;//OUT::Grove

int buttonPushCounter = 0;

int buttonState = 0;

int lastButtonState = 0;

bool lockOn = false;

unsigned long previousMillis = 0;

const unsigned long interval =
10000;//Lock Release Time::10s

const unsigned long YLED_PINInterval =

void setup()






digitalWrite(DRIVE_PIN, LOW);


void loop() {

buttonState = digitalRead(KEY_PIN);

unsigned long currentMillis = millis();

if ( buttonState == LOW )


previousMillis = currentMillis;

digitalWrite(DRIVE_PIN, HIGH);

digitalWrite(GLED_PIN, HIGH);

digitalWrite(YLED_PIN, LOW);

lockOn = true;


if ( lockOn )


if (currentMillis – previousMillis >= interval – YLED_PINInterval )

digitalWrite(YLED_PIN, (millis() / 300) %2);//Lock Alert!

if (currentMillis – previousMillis >= interval)


digitalWrite(DRIVE_PIN, LOW);

digitalWrite(GLED_PIN, LOW);

digitalWrite(YLED_PIN, LOW);

lockOn = false;





The code is tailored in a simple way. If the button is pressed, the door lock solenoid gets energized (lock released) for 10 seconds. The Yellow LED inside the button always remains in the off state, but it starts flashing for a while just before the end of the 10 seconds time period. It is a quick notification to let the user know that the lock will be activated again soon!

This little project can be modified as a secret pattern door lock. If so, the door lock solenoid will only get energised when a secret pattern is entered through a single pushbutton (short and long press) or multiple pushbuttons (secret number or password). If I get enough requests from regular visitors of this site, I will post a do-it-yourself project about it later.

Look, this Arduino library (https://github.com/mathertel/OneButton) can improve the usage of a single button for input. It helps to employ a digital input pin with a single pushbutton for detecting some of the typical button press events like single clicks, double clicks and long/short time pressing. This enables you to reuse the same button for multiple functions and simplifies the hardware setup.

On a side note, the load switch idea helps you to drive even power-hungry door lock solenoids because the pass transistor is a Power MOSFET. Even so, if you don’t like to go with it, you can simply rig up a non-isolated solenoid driver circuitry as depicted here https://cdn-shop.adafruit.com/product-files/412/412_solenoid_driver.pdf . Besides, you can power your entire build up from a 12VDC power source, but look what you’re doing!

For advanced hobbyists, this Texas Instruments application report (https://www.ti.com/lit/an/slvae59/slvae59.pdf) provides various methods on how to drive solenoids and relays using TI integrated motor driver ICs. In this document, you can see solenoid fundamentals, basic configurations, different driving methods, etc.

And, this is the application note of NXP’s Programmable Solenoid Controller PT2000 https://www.nxp.com/docs/en/data-sheet/MC33PT2000.pdf

Why stop here?

I will show you the basics of building an electronic door lock that can be the basis for many interesting security projects. The best part is that most of the components required are either cheap or parts you can find at your favorite online storefront.

My real goal here is not to show you how to build an electronic door lock prototype. I am trying to inspire you start thinking and designing smart electronic door locks on your own, using your own brilliant ideas. I encourage you to alter the basic design and functionality to suit your needs. Good Luck!

Leave a Comment