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-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

Or you can buy the following kits:

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

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) is 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 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

If you do not use a breadboard, wire it like this:

The wiring diagram between Arduino Nano ESP32 and Solenoid Lock  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

Pushing jumper wires into the headers is quick, but the wires can slip out. In this case, use a Screw Terminal Expansion Board for Arduino Nano. The Arduino Nano ESP32 plugs into it, and each pin gets a screw terminal. Each wire goes to the screw terminal of the same pin, as in the image below.

The wiring diagram between Arduino Nano ESP32 and Solenoid Lock Screw Terminal

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 newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-solenoid-lock */ #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

※ 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()

Relay vs MOSFET Module for the Solenoid Lock

This tutorial uses a relay to switch the solenoid lock. A 12V solenoid lock runs on DC, so a MOSFET module can also switch it.

Relay MOSFET Module
Lock control lock or unlock lock or unlock
Switching sound a click every time no sound
Switching time slow (milliseconds) very fast
Wear the contacts get old with use no moving parts to wear
Works with AC and DC locks DC locks only
Flyback diode most relay modules do not need one must add one (1N4007 or 1N5819)
Isolation yes yes, with a PC817 optocoupler
Choose it for a simple, common setup a silent lock, or a door that opens many times a day

A solenoid lock is only locked or unlocked, so the MOSFET module does not give extra control here. Its gains are: no click, and nothing wears out. Both turn on when the pin is HIGH, so the code stays the same. Do not keep the lock unlocked for a long time, or it gets hot. This is the same with a relay. The 3.3V pin of the Arduino Nano ESP32 is enough to drive the module.

A solenoid valve works the same way as a solenoid lock. To see how to wire and code one with both a relay and a MOSFET module, see the Arduino Nano ESP32 - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Arduino Nano ESP32 - MOSFET Module tutorial.

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!