Arduino Nano ESP32 - Solenoid Lock

The Solenoid Lock is also known as the Electric Strike Lock. It can be use to lock/unlock cabinet, drawer, door. This tutorial provides instructions on how to use Arduino Nano ESP32 to control the solenoid lock.

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

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Solenoid Lock
1×12V Power Adapter
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter 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. We appreciate your support.

Overview of Solenoid Lock


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) is retracted ⇒ the door is unlocked


The solenoid lock usually uses 12V, 24V or 48V power supply. Therefore, we CANNOT connect the solenoid lock directly to Arduino Nano ESP32 pin. We have to connect it to Arduino Nano ESP32 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 Nano ESP32 to the relay, we can program for Arduino Nano ESP32 to control the solenoid lock. Learn more about relay in Arduino Nano ESP32 - Relay tutorial.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Solenoid Lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

The below code lock/unlock the door every 5 seconds

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

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • See the the lock tongue's state

Arduino Nano ESP32 - Button Controls Solenoid Lock

See Arduino Nano ESP32 - Button Controls Solenoid Lock tutorial


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


  • 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!