Button Controlled Electromagnetic Lock – Arduino Tutorial

3,068 views

Introduction

A push button-controlled electromagnetic lock is a system that uses an Arduino UNO microcontroller to control the locking and unlocking of a 12V DC Electromagnetic lock. The system is activated by pressing a pushbutton, which sends a signal to the microcontroller.

An electromagnetic lock is a type of locking mechanism that uses an electromagnet to hold a metal armature plate in place, keeping the door or gate locked. This design scheme also employs a 12V SPDT relay to reliably power up the electromagnetic lock circuit, adhering to the code workflow of an Arduino uno microcontroller.

Hardware Components

You will require the following hardware for Electromagnetic Lock with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Electromagnetic Lock1
4.Relay1
5.Power Adapter12V1
6.DC Power Jack1
7.Button1
8.Power Adapter for Arduino9V1
9.Breadboard1
10.Jumper Wires1

Electromagnetic Lock with Arduino UNO

  1. Import the necessary libraries at the top of the sketch. This can include the “Arduino.h” library.
#include <Arduino.h>
  1. Declare variables and objects. Declare a variable to store the state of the pushbutton and also the pin numbers of the pushbutton, the relay and the electromagnetic lock.
int buttonPin = 2;   // pushbutton pin
int relayPin = 8;    // relay pin
int lockPin = 7;     // electromagnetic lock pin
int buttonState;
  1. In the setup() function, initialize the serial communication at a baud rate of 9600, configure the pushbutton, relay, and electromagnetic lock pins as inputs and outputs respectively.
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(relayPin, OUTPUT);
  pinMode(lockPin, OUTPUT);
}
  1. In the loop() function, read the state of the pushbutton using the digitalRead() function and store it in the buttonState variable. Then use an if-else statement to control the state of the relay and electromagnetic lock.
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(lockPin, LOW);
    Serial.println("Lock Unlocked");
  } else {
    digitalWrite(relayPin, LOW);
    digitalWrite(lockPin, HIGH);
    Serial.println("Lock Locked");
  }
  delay(200); // add a delay to avoid
  1. Connect the 12V DC Electromagnetic Lock to the electromagnetic lock pin of the Arduino using a suitable wire. Connect the 12V SPDT relay to the relay pin of the Arduino using a suitable wire. Connect the 12V DC adapter to the power supply of the electromagnetic lock and the relay.
  2. Upload the code to the Arduino Uno microcontroller.
  3. Test the circuit by pressing the push button. When the button has pressed the state of the lock should change and the state of the lock should be printed on the serial monitor.
  4. Make any necessary adjustments to the code to suit your specific needs and the setup of your circuit.

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoRelayLOCK
5VVCCVCC To +12V
GNDGNDGND
D3SIG

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

Arduino Code – Button Controls Electromagnetic Lock Without Debouncing

const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin

// variables will change:
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode

  digitalWrite(RELAY_PIN, HIGH);     // lock the door
  currentButtonState = digitalRead(BUTTON_PIN);
  
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial.println("The button is pressed");
    digitalWrite(RELAY_PIN, LOW); // unlock the door in 10 seconds
    delay(10000); // 10 seconds
    digitalWrite(RELAY_PIN, HIGH); // lock the door again
  }
}

Arduino Code – Button Controls Electromagnetic Lock With Debouncing

#include <ezButton.h>

// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);         // initialize serial
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  button.setDebounceTime(50); // set debounce time to 50 milliseconds

  digitalWrite(RELAY_PIN, HIGH); // lock the door
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()) {
    Serial.println("The button is pressed");
    digitalWrite(RELAY_PIN, LOW); // unlock the door in 10 seconds
    delay(10000); // 10 seconds
    digitalWrite(RELAY_PIN, HIGH); // lock the door again
  }
}

Working Explanation

The working of this circuit is essentially very simple. The code is written to define the pin numbers for the pushbutton and the relay in the setup() function, and also to initialize the serial communication. In the loop() function, the code reads the state of the pushbutton, controls the relay based on that state, and posts the status of the lock to the serial monitor.

The code uses the digitalRead() and digitalWrite() functions to read the state of the pushbutton and control the relay, respectively. The digitalRead() function is used to check if the button is pressed, and based on that, the digitalWrite() function controls the state of the relay to lock or unlock the electromagnetic lock. The Serial.begin() and Serial.println() functions are used to initialize serial communication and post the status of the lock to the serial monitor.

Applications

  • Surveillance systems
  • Retail automation
  • Health care facilities
  • Transportation
  • Airports and other transportation hubs

Conclusion.

We hope you have found this Arduino – Button Control Electromagnetic Lock Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.