Arduino UNO R4 - Solenoid Lock

This tutorial instructs you how to use Arduino UNO R4 and the solenoid lock to lock/unlock your door, cabinet, or drawer. In detail, We will learn:

Arduino UNO R4 Solenoid Lock

Overview of Solenoid Lock

The solenoid lock, also called the electric strike lock, is used to lock or unlock items like cabinets, drawers, and doors.

Pinout

The solenoid lock has two wires:

  • Connect the positive (+) red wire to the 12V DC power supply.
  • Connect the negative (-) black bit to the ground (GND) of the DC power supply.
Solenoid Lock Pinout

How It Works

  • When the solenoid lock has power, the lock tongue (strike) moves out ⇒ the door is locked.
  • When the solenoid lock has no power, the lock tongue (strike) pulls back ⇒ the door is unlocked.

※ NOTE THAT:

The solenoid lock typically needs a 12V, 24V, or 48V power supply. We MUST NOT connect the solenoid lock directly to the Arduino UNO R4 pin. Instead, connect it through a relay to the Arduino UNO R4 pin.

If we connect the solenoid lock to a relay in the normally open setting:

  • If the relay is open, the door is unlocked.
  • If the relay is closed, the door is locked.

You can connect an Arduino UNO R4 to a relay to program it to control a solenoid lock. Find out more about using a relay with Arduino UNO R4 in our tutorial Arduino UNO R4 - Relay Tutorial.

Wiring Diagram

The wiring diagram between Arduino UNO R4 solenoid lock

This image is created using Fritzing. Click to enlarge image

Jumper wires are good for a quick test, but not for a final build. The Arduino UNO R4 works with both the Proto Screw Terminal Block Shield and the Screw Shield V2. Both turn the header pins into screw terminals. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

The wiring diagram between Arduino UNO R4 solenoid lock Screw Shield

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 Code

This code will lock and unlock the door every 5 seconds.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-solenoid-lock */ #define RELAY_PIN 3 // The Arduino UNO R4 pin connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 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); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the solenoid lock to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code and open it with Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
  • Check the position of the lock tongue

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.

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 UNO R4 - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Arduino UNO R4 - 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!