ArduinoArduino Projects

Arduino Image Processing based Entrance lock Control System

image processing


This tutorial is focused on Image Processing using Visual Basic 2010 Express Version, Arduino UNO or Mega and 12 Volt Electronic Key. The aim of this tutorial is to use Human Detection to automatically control the Entrance Lock. Using the frontal Face XML folder, humans are monitored in RealTime, once the human face is identified, then we find the human face’s x and y coordinates and then send a command to open the Entrance Lock to the Arduino Uno or Mega Board. Such commands are sent from the Visual Basic software in serial form.

Depending on the type of XML file they use, the same program can be used to record ears, nose, human body, hands and so on. All you need is to replace the XML file of Frontal Face with the one you want. You can use this project to track virtually anything you want.

You can also create custom XML file for specific object tracking that will cover in another Tutorial. 

The EmguCV is playing key role in this venture.

Software used in this project:

  1. Visual Basic 2010 Express Edition
  2. Arduino IDE

The Amazon Purchase links are given below:

WebCam night vision supported: best deal on Amazon:
electronic lock:
Arduino Uno:
Mega 2560:
lm7805 Voltage Regulator:
330-ohm resistors pack:
female DC power jack socket:
470uf capacitors:
5×7 cm Vero board:
female headers:
connection wires:
Super Starter kit for Beginners:
Jumper Wires:
Bread Board:
12v Adaptor:
PCB plate:
Variable Supply:
Digital Multimeter:
Vero Board / stripboard:
Soldering iron kit: “best” You guys should definitely purchase this:
Solder wire:
Wire Stripper:
PCB small portable drill machine:


Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

About the Electronic Lock:

image processing

Electronic locks are available in various shapes and sizes. But the working theory is exactly the same for all the Digital Locks. The difference can only be in the voltage and current that the electronic lock coil requires to be energized. The coil that creates the magnetic field when the desired voltage is applied is inside the electronic lock.

There are two wires in the electronic switch, one is the voltage and the other is the GND cable. The digital lock used in this tutorial has two cables. You can test the electronic lock by applying the voltage across the electronic lock’s two wires.

Since this tutorial is based on the Image Processing Automatic Entry Control System, we will need driver circuit for this Electronic Lock to control this Electronic Lock automatically. Using relay, NPN transistor, or MOSFET, this lock can be operated automatically. As we only aim to open and close the electronic lock in this project and as we do not use any PWM or fast switching, Relay is the best choice in this project. Isolation will also be provided by the use of a relay.

image-processing Circuit Diagram:

image processing

As you can see, the circuit diagram is very simple. The electronic lock is controlled by means of a relay type SPDT “Single Pole Double Throw,” which is a 12v relay. As you can see in the circuit diagram, the electronic lock attaches the 12v wire from the power supply, it really doesn’t matter to which wire you connect it.The relay’s normally open connection is connected to the other electronic lock wire While the power supply ground is connected to the relay’s specific contact.

Using the relay motor, this relay is operated. A 2n2222 NPN transistor and 10k resistor are used in the relay driver. Please check the coil current of the electronic lock before choosing the transistor. To find the current, you must first find the resistance of the electronic lock coil, the voltage is already known to be 12v, then use the Ohm rule.

V = IR

You can measure the current. Use the NPN transistor whose measured value is the current of the collector. Pick transistor whose collector current is times higher than the calculated value for the best performance.

The Arduino electronic pin 13 controls the relay as shown in the above circuit diagram and ensures that the Arduino ground is connected to the 2n2222 NPN transistor emitter.

image processing Arduino Programming:

int elock = 13; // machine gun trigger motor connected here 

void setup()


    pinMode(elock, OUTPUT);
  // keep elock off by default

  digitalWrite(elock, LOW);

void loop()

  while(Serial.available() > 0) // 
    char received =;

  if(received == 'd') // human not in front

    digitalWrite(elock, LOW); 

   if(received == 'c') // HUMAN FACE ON front

digitalWrite(elock, HIGH);

} Image Processing Application Programming:

You will need to install the following two software to design the image processing application. Edition and EmguCv are expressed by Microsoft Visual Basic 2010. That’s how it looks like the final form.

image processing

Imports Emgu.CV
Imports Emgu.CV.Util

Imports Emgu.CV.Structure
Imports System.Diagnostics

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Dim facedetected As Integer

    Dim facepresent As Integer

    Dim detectTimecount As Integer

    Dim web As Capture = New Capture(0)

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' cars()
    End Sub

    Sub faces()

        Dim photo As Image(Of Bgr, Byte)
        photo = web.RetrieveBgrFrame
        Dim currentCenter As New Point()

        ' Dim eyedetection As New CascadeClassifier("C:\data\haarcascades\haarcascade_eye.xml") '  ' this one is for eye detection
        Dim facedetection As New CascadeClassifier("C:\data\haarcascades\haarcascade_frontalface_default.xml") ' this one is for the face detection

        facedetected = 0

            Dim image As Image(Of Gray, Byte) = photo.Convert(Of Gray, Byte)()

            For Each face As Rectangle In facedetection.DetectMultiScale(image, 1.1, 8, Size.Empty, Size.Empty) ' default 1.1, 8  ( while best values are 1.2 and 17 after checking)

                photo.Draw(face, New Bgr(Color.White), 4)

                currentCenter.X = CInt(CDbl(face.X + face.X + face.Width) / 2.0)
                currentCenter.Y = CInt(CDbl(face.Y + face.Y + face.Height) / 2.0)

                Label1.Text = (currentCenter.X).ToString
                Label2.Text = (currentCenter.Y).ToString

                Dim f = New MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5)
                photo.Draw("Human detected and scanning....", f, currentCenter, New Bgr(0, 255, 0)) ' New Point(10, 80) 

                Dim f2 = New MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 0.7, 0.7)
                photo.Draw("human detection ", f2, New Point(10, 20), New Bgr(0, 255, 255)) ' New Point(10, 80)

                Label3.Text = face.ToString

                facedetected = 1  ' we are storing 1 if faces are detected.


            PictureBox1.Image = photo.ToBitmap

        Catch ex As Exception

        End Try

        If facedetected = 1 Then
            lblfacedetect.Text = "Scanning..."
            Timer3.Enabled = True

            lblfacedetect.Text = "no Human..."
            Timer3.Enabled = False
            detectTimecount = 0
            facepresent = 0

            '  SerialPort1.Open()
            '  SerialPort1.Write("d")
            '  SerialPort1.Close()

        End If

        ' new addition end 

        If detectTimecount >= 3 Then  ' HUMAN FACE IN CENTER

            ' detectTimecount = 0
            Label4.Text = "Human detected"
            SerialPort1.Write("c") ' the human is on front

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "com8"
        SerialPort1.BaudRate = "9600"
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        detectTimecount = detectTimecount + 1
        Label4.Text = detectTimecount
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        SerialPort1.Write("d") ' no human
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Write("c") ' the human is on front
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Write("c") ' the human is on front
    End Sub
End Class

Watch Video Tutorial

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button