Arduino MKR WiFi 1010 - Solenoid Lock

Control electronic locks remotely! The solenoid lock (electric strike lock) provides secure electronic locking for cabinets, doors, and enclosures. This tutorial teaches you how to connect a solenoid lock to your Arduino MKR WiFi 1010 and program it for automated access control.

What You'll Learn:

Real-World Applications:

Arduino MKR WiFi 1010 solenoid lock

Alternative Option:

For heavier-duty applications, consider the electromagnetic lock. See the Arduino MKR WiFi 1010 Electromagnetic Lock tutorial for comparison.

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

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

What is a Solenoid Lock?

A solenoid lock (also called electric strike lock) is an electromechanical locking device that uses electromagnetic force to extend or retract a bolt, controlling access to doors, cabinets, and enclosures.

How Solenoid Locks Work:

Solenoid Lock Operation: Step 1: Power Applied → Current flows through coil → Creates magnetic field → Pulls metal plunger/bolt Step 2: Bolt Extends → Lock engages → Door/cabinet secured → Remains locked while powered Step 3: Power Removed → Magnetic field collapses → Spring returns bolt to rest → Lock releases → Door/cabinet unlocked

Key Characteristics:

  • Operating voltage: 12V, 24V, or 48V DC (most common: 12V)
  • Current draw: 200-500mA (depending on model)
  • Lock modes: Fail-secure (locked when unpowered) or fail-safe (unlocked when unpowered)
  • Holding force: 10-50 kg depending on model
  • Response time: <100ms lock/unlock
  • Duty cycle: Typically 100% (can remain locked continuously)

Solenoid Lock vs Electromagnetic Lock:

Comparison: Feature Solenoid Lock Electromagnetic Lock ──────────────────────────────────────────────────────────────────────────── Power Usage 200-500mA 800-1500mA Holding Force 10-50 kg 150-600 kg Installation Inside door frame Surface mounted Size Compact Larger Cost Lower Higher Best For Cabinets, drawers Heavy doors

Pinout

Solenoid Lock Pinout

The solenoid lock has two wires:

Power Connections:

  • Red wire (+): Positive terminal
    • Connect to +12V from DC power supply
    • Do NOT connect directly to Arduino (insufficient current)
    • Must use relay for switching
  • Black wire (-): Negative terminal (Ground)
    • Connect to GND of DC power supply
    • Common ground with Arduino

    Important Voltage/Current Requirements:

    Power Requirements: Solenoid Lock: 12V @ 200-500mA Arduino Pin: 3.3V @ 15mA max ⚠ Cannot power directly from Arduino! ✓ Must use relay + external 12V supply

    How It Works

    Operation States:

    • Locked (Powered):
      • 12V applied to solenoid
      • Electromagnetic coil energized
      • Metal bolt extends outward
      • Door/cabinet secured
    • Unlocked (Unpowered):
      • No voltage applied
      • Spring returns bolt inward
      • Door/cabinet released

      Relay Control Logic:

      Since solenoid locks require 12V and Arduino provides only 3.3V, we use a relay as an electrically controlled switch:

      Relay Configuration (Normally Open): Relay OFF → Solenoid Unpowered → Unlocked Relay ON → Solenoid Powered → Locked Arduino → Relay → Solenoid Lock → 12V Supply (3.3V) (Switch) (12V device)

      Control Flow:

      1. Arduino sends LOW signal (relay off) → Solenoid unpowered → Unlocked
      2. Arduino sends HIGH signal (relay on) → Solenoid powered → Locked
      3. Arduino controls timing and access conditions in code

      Learn More:

      For detailed relay operation, see Arduino MKR WiFi 1010 - Relay tutorial.

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 Solenoid Lock

This image is created using Fritzing. Click to enlarge image

No breadboard? The same circuit can be wired without one, as shown below:

The wiring diagram between Arduino MKR WiFi 1010 Solenoid Lock  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

Detailed Connections

Solenoid Lock Wiring:

  • Solenoid Red (+) → Relay COM (Common)
    • Connect to relay's normally open contact
  • Solenoid Black (-) → 12V Supply GND
    • Common ground with Arduino and power supply
  • Relay NO (Normally Open) → 12V Supply +
    • Power source when relay closes

    Relay Module Connections:

    • Relay VCC → Arduino 5V
      • Powers relay coil circuitry
    • Relay GND → Arduino GND
      • Common ground
    • Relay IN → Arduino Digital Pin (e.g., D2)
      • Control signal from Arduino

      Power Supply:

      For This Project: You need TWO power sources:

      1. USB power for Arduino (programming and logic)
      2. 12V DC adapter for solenoid lock (high current device)

      Common ground connection required between both supplies.

      Safety Notes:

      • Never connect solenoid directly to Arduino pins
      • Relay isolates Arduino from high-voltage/current circuit
      • Use properly rated 12V power supply (1A minimum)
      • Check polarity before connecting

Arduino MKR WiFi 1010 Code

This code demonstrates automatic lock/unlock cycling - locks for 5 seconds, then unlocks for 5 seconds.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-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); }

Code Explanation

Pin Definition:

const int RELAY_PIN = 2;

Defines which Arduino pin controls the relay.

Lock Control:

digitalWrite(RELAY_PIN, HIGH); // Lock engaged digitalWrite(RELAY_PIN, LOW); // Lock released

HIGH = Relay closes → 12V to solenoid → Locked

LOW = Relay opens → No power → Unlocked

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

Step 1: Hardware Setup

  • Connect relay to Arduino (VCC to 5V, GND to GND, IN to D2)
  • Connect solenoid lock to relay and 12V supply following wiring diagram
  • Ensure common ground between Arduino and 12V supply
  • Connect Arduino to computer via USB
  • Connect 12V power adapter

Step 2: Upload Code

  • Copy code above into Arduino IDE
  • Select Arduino MKR WiFi 1010 board and COM port
  • Click Upload button

Step 3: Test Operation

  • Observe solenoid bolt extending (locked)
  • Wait 5 seconds
  • Observe bolt retracting (unlocked)
  • Cycle repeats automatically

Safety Testing:

  1. Listen for clicking sound from relay
  2. Check solenoid bolt movement is smooth
  3. Verify lock holds firmly when engaged
  4. Ensure unlock releases completely

Troubleshooting Common Issues

Problem: Solenoid Doesn't Lock or Unlock

Possible Causes:

  • No 12V power to solenoid
  • Relay not switching
  • Wiring error
  • Insufficient current from power supply

Solutions:

  1. Check 12V supply: Use multimeter to verify voltage
  2. Test relay: Listen for click sound when Arduino sends signal
  3. Verify wiring:

- Solenoid Red → Relay COM

- Relay NO → 12V+

- Solenoid Black → GND

  1. Check power supply rating: Must provide at least 500mA
  2. Test relay independently: Manually trigger with jumper wire

Problem: Relay Clicks But Solenoid Doesn't Move

Possible Causes:

  • Solenoid damaged
  • Insufficient voltage
  • Mechanical obstruction

Solutions:

  1. Test solenoid directly: Connect briefly to 12V (bypass relay)
  2. Measure voltage at solenoid: Should be 12V when relay on
  3. Check for binding: Ensure bolt moves freely
  4. Inspect solenoid: Look for physical damage

Problem: Weak Locking Force

Cause: Voltage drop or insufficient current.

Solutions:

// Ensure relay stays on long enough digitalWrite(RELAY_PIN, HIGH); delay(100); // Allow solenoid to fully engage
  1. Use shorter, thicker wires to 12V supply
  2. Check power supply can deliver rated current
  3. Measure voltage at solenoid terminals under load

Problem: Solenoid Gets Hot

Cause: Normal for continuous operation, but excessive heat indicates problem.

Solutions:

  1. Check voltage: Verify 12V, not higher
  2. Reduce duty cycle: Don't keep locked 100% of time if not needed
  3. Add cooling: Ensure airflow around solenoid
  4. Check current draw: Should be within spec (200-500mA)

Problem: Relay Module LED On But Relay Not Switching

Cause: Some relay modules are active-LOW triggered.

Solution: Invert logic in code:

digitalWrite(RELAY_PIN, LOW); // Lock (relay closes) digitalWrite(RELAY_PIN, HIGH); // Unlock (relay opens)

Challenge Yourself - Creative Extensions

Once you have basic solenoid lock control working, try these enhancements:

1. Keypad Access Control

Unlock with PIN code:

#include <Keypad.h> const char* correctPIN = "1234"; char enteredPIN[5]; if (strcmp(enteredPIN, correctPIN) == 0) { digitalWrite(RELAY_PIN, LOW); // Unlock delay(5000); // Stay unlocked 5 seconds digitalWrite(RELAY_PIN, HIGH); // Re-lock Serial.println("Access granted"); } else { Serial.println("Access denied"); }

2. RFID Card Access

Use RFID reader for keyless entry:

#include <MFRC522.h> const String authorizedCards[] = {"AB12CD34", "EF56GH78"}; if (isAuthorized(scannedCard)) { digitalWrite(RELAY_PIN, LOW); // Unlock delay(3000); digitalWrite(RELAY_PIN, HIGH); // Re-lock }

3. Remote WiFi Control

Unlock via web interface:

#include <WiFiNINA.h> WiFiServer server(80); if (request.indexOf("/lock") != -1) { digitalWrite(RELAY_PIN, HIGH); client.println("Door locked"); } else if (request.indexOf("/unlock") != -1) { digitalWrite(RELAY_PIN, LOW); client.println("Door unlocked"); }

4. Timed Auto-Lock

Automatically re-lock after timeout:

unsigned long unlockTime = 0; const unsigned long AUTO_LOCK_DELAY = 10000; // 10 seconds void unlock() { digitalWrite(RELAY_PIN, LOW); unlockTime = millis(); } void loop() { if (unlockTime > 0 && millis() - unlockTime > AUTO_LOCK_DELAY) { digitalWrite(RELAY_PIN, HIGH); // Auto-lock unlockTime = 0; } }

5. Door Sensor Integration

Detect if door actually closed:

const int DOOR_SENSOR_PIN = 3; // Magnetic switch void loop() { bool doorClosed = digitalRead(DOOR_SENSOR_PIN) == HIGH; if (doorClosed && shouldLock) { digitalWrite(RELAY_PIN, HIGH); // Safe to lock } else { Serial.println("Door open - cannot lock"); } }

6. Access Log

Record all lock/unlock events:

#include <SD.h> void logAccess(String user, String action) { File logFile = SD.open("access.txt", FILE_WRITE); logFile.print(millis()); logFile.print(","); logFile.print(user); logFile.print(","); logFile.println(action); logFile.close(); }

7. Multiple Lock System

Control several locks independently:

const int LOCK_PINS[] = {2, 3, 4, 5}; const int NUM_LOCKS = 4; void unlockAll() { for (int i = 0; i < NUM_LOCKS; i++) { digitalWrite(LOCK_PINS[i], LOW); } } void lockSpecific(int lockNumber) { digitalWrite(LOCK_PINS[lockNumber], HIGH); }

8. Battery Backup Detection

Alert when power fails:

const int POWER_MONITOR_PIN = A0; void loop() { int voltage = analogRead(POWER_MONITOR_PIN); if (voltage < THRESHOLD) { Serial.println("WARNING: Power failure!"); // Trigger alarm or notification } }

9. Scheduled Access

Allow entry only during specific times:

#include <RTClib.h> RTC_DS1307 rtc; void loop() { DateTime now = rtc.now(); if (now.hour() >= 9 && now.hour() < 17) { // 9 AM - 5 PM // Allow unlock } else { Serial.println("Access restricted - outside hours"); } }

10. Buzzer Feedback

Audio confirmation of lock state:

const int BUZZER_PIN = 6; void lockWithFeedback() { digitalWrite(RELAY_PIN, HIGH); tone(BUZZER_PIN, 1000, 200); // Short beep Serial.println("Locked"); } void unlockWithFeedback() { digitalWrite(RELAY_PIN, LOW); tone(BUZZER_PIN, 2000, 100); // Two quick beeps delay(100); tone(BUZZER_PIN, 2000, 100); Serial.println("Unlocked"); }

Experiment with electronic locks and build smart access control systems!

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