This project aims to implement door lock system on Raspberry Pi Zero W. The project uses RFID reader and fingerprint sensor for dual access modes. When the user tries to access the system the data is fetched from a MySQL database on the Raspberry Pi Zero, then unlocks the solenoid lock if it identifies the RFID ID or the fingerprint. The system also is capable to register new users and save it to database.

Hardware Used

Software Used

  • VNC Viewer
  • Python 3 IDLE
  • MySQL


Raspberry Pi Zero W

Image result for raspberry pi zero w
The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python.
The Raspberry Pi Zero W is ideal for making embedded Internet of Things (IoT) projects. At the heart of the Raspberry Pi Zero W is a 1GHz BCM2835 single-core processor, the same as the B+ and A+, with 512MB RAM.
Image result for micro usb to usb adapter
Since we are using Raspberry Pi Zero and it is has only micro USB port we will be needing this Micro USB to USB adapter for our external peripherals.
This is the Raspberry Pi Zero W pinout.
Optical Fingerprint Sensor
This image has an empty alt attribute; its file name is main-qimg-0d44ed8230be0d367f347c1b9f75db21
The figure above hows the read process for capturing fingerprints.
This image has an empty alt attribute; its file name is robu-4-15.jpg
The fingerprint sensor is connected to any microcontroller or system with TTL UART serial, and send packets of data to take photos, detect prints, hash and search. In our case we will connect this fingerprint sensor to Raspberry Pi Zero’s UART serial pins. You can also enroll new fingerprints directly . Up to 162 fingerprints can be stored in the sensor’s onboard FLASH memory.
RFID Module
This image has an empty alt attribute; its file name is rc522-rf-ic-card-sensor-2f-s50-card-2f-keychain-for-arduino-500x500.jpg
RFID means radio-frequency identification. RFID uses electromagnetic fields to transfer data over short distances. RFID is useful to identify people, to make transactions and others.
The RFID tags included in the kit are 1 white card-type tag, and 1 keychain type tag. The tag is placed a few centimeters from the reader in order to make a reading. This RFID system operates at 3.3V and has a frequency of 13.56 MHz.
This image has an empty alt attribute; its file name is 1602-16x2-lcd-iici2ctwispi-serial-interface-module-display-blueyellow-diymore_980_1024x1024.jpg
This is a 16×2 LCD display screen with I2C interface. It uses only 4 pins the SCL,SDA,VCC and GND when interfacing with your Raspberry Pi. The advantage in using this is to save on the number of pins used on the Raspberry Pi.
This image has an empty alt attribute; its file name is DC-12V-2A-Small-Solenoid-Electromagnetic-Electric-Control-Cabinet-Drawer-Lockers-Lock-Pudsh-push-Design-Automatic.jpg_640x640.jpg
This is a solenoid lock that operates at 12V, with maximum current of 2A. Useful for doors and cabinets etc. as safety lock.
Related image
The module above is an adapter module to translate between UART TTL signals and USB signals. The Universal Asynchronous Receiver-Transmitter (UART) is a device for asynchronous serial communication in which the data format and transmission speeds are configurable. A UART is usually an individual (or part of an) integrated circuit (IC) used for serial communications over a computer or peripheral device serial port. UARTs are now commonly included in microcontrollers. The Raspberry Pi already has 2 GPIO pins for UART, so we are able to connect the sensor directly to the Raspberry Pi (according to the data sheet, the logic high level of the sensor (3,3V) is not to high for the mini computer).

Hardware Setup

This is the schematic diagram for the door lock system using Raspberry Pi Zero W.
This is the connection from your fingerprint sensor to your USB to TTL Converter module.
This image has an empty alt attribute; its file name is download.jpeg
This the pinout for the fingerprint reader sensor.
This is the actual circuit setup for the Door Lock system using Raspberry Pi Zero W.

Software Setup

Before we start on our project we need to install and setup the different libraries and tools to get our codes to run properly.

Setup for Fingerprint Sensor

First (if not already done), you have to add the PM Codeworks repository to your system:

Afterwards, you have to install the suitable GPG signing key to be sure of the integrity of the packages:

After an update of the local package sources, you are able to install the fingerprint package:

If you connect the fingerprint sensor to your Raspberry Pi via UART TO USB adapter module, the device should become available via the path “/dev/ttyUSB0”. By default only the root user is able to use serial devices. To make sure that the normal user (e.g. “pi”) is able to use the fingerprint sensor, you have to add him to the user group “dialout”:

After this reboot your Raspberry Pi. Then you can now test your fingerprint sensor. Connect the fingerprint sensor via converting board to your Raspberry Pi and change to the example directory of the fingerprint library:

For example, test the “index” program (it lists the available fingerprint slots):

If everything works fine, you will get an output similar to the following:

Setup for RFID reader:

First, you need to enable SPI for your Raspberry Pi Zero. SSH to your Pi or use VNC and open the terminal. Go to configuration settings via:

On Raspberry Pi desktop menu, select the Interfacing Options, then select SPI. Once you have selected SPI it will prompt if you would like to enable SPI, and just select Yes . Then reboot your raspberry pi.

Now we need to install the spidev library we will use:

This installs spidev for both Python 2 and Python 3.
Now we need the SPI-py to drive our SPI interface in python. To install it we will use the following commands:

Now to download our RFID Library, we will use the following commands:

After downloading the library try to run some test on the examples of the library. Try first.

The script waits for a tag to be detected by the RFID reader. When it finds a tag it reads the UID and prints it on the screen. The script runs in a loop and will keep waiting and displaying any detected UIDs.

This will be your output if there is no problem on running the script. This script allows you to determine the UID of the tag or card that was supplied with the reader.

If this error occurs:

try to execute this commands:

Setup for I2C LCD

Before we get into programming, we need to make sure the I2C module is enabled on the Pi and install a couple of tools that will make it easier to use I2C.

First, on a terminal log in to your Pi and enter sudo raspi-config  to access the configuration menu. Then arrow down and select “Advanced Settings”:

Then select “I2C Enable/Disable automatic loading”:

Choose “Yes” at the next prompt, exit the configuration menu, and reboot the Pi to activate the settings.

Now we need to install a program called I2C-tools, which will tell us the I2C address of the LCD when it’s connected to the Pi. Enter the following commands:

Now we need to install SMBUS library, which is the Python library we’re going to use to access the I2C bus on the Pi:

Now reboot the Pi and log in again. With your LCD connected, enter i2cdetect -y 1 in the terminal. This will show you a table of addresses for each I2C device connected to your Pi:

The I2C address of the LCD is 21. Take note of this number, we’ll need it later.

In using the library

Copy this script for the library, then save it in a file named

There are a couple of things you may need to change in the script above, depending on your set up. In line 19 there is a function that defines the port for the I2C bus (I2CBUS = 0). Older Raspberry Pi’s use port 0, but newer models use port 1. So depending on which RPi model you have, you might need to change this from 0 to 1.
Next, put the I2C address of your LCD in line 22 of the library script. For example, my I2C address is 21, so I’ll change line 22 to ADDRESS = 0x21.

You can now test your I2C LCD by using this script

For more examples:

Setup for MySQL database

Begin by installing MYSQL to your Raspberry Pi by running the following command:

After installation let’s load up the MYSQL command-line tool.

Om the MYSQL comman-line tool, begin creating a database where we will be storing all of the data that we will be utilizing for our RFID attendance system.

In this case my database name is card.

Next we need to create user . To create user:

Now that we have created our user we need to give it the rights to access our database card using this command:

This command will give our “admin” user full privileges on any table within our database.

Before we create our tables, we need to utilize the “use” command so that we are directly interacting with the “card” database.

Begin interacting with the database by running the following command.

We can now start creating the tables where all our data will be stored by running this command.

After creating table you can leave the MYSQL tool by entering exit;

Now after setting up all the necessary tool and libraries, here is the application Python script for our system.


This is the application script which will integrate both RFID reader and Fingerprint sensor after the set up process is done. Assuming that the database is empty and you are using new card and adding new user just run this script to add new users.

Python script to register a new user

Python script for Door Access routine

When the user is added to the database you can now run this python script to access your door lock system.

Code Breakdown

In this section of the code, we create our connection to our MYSQL server. For this function, we pass in all the information required to make the connection such as the host, user, database name and the password.

This code is to interact with the database and to execute SQL queries.

This part of the code is where the RFID wait for a card to be scanned and authenticate the UID of the card.

This is for execution of query to the database when the card is scanned and fetch the registered data from database with its values.

This is query for fetching the saved fingerprint template according to its positionNumber.

This is query for the data to be inserted to our database. db.commit() must be called to execute Insert and Update queries.

This is the actual test video of the system with its functionalities.


The overall system is relatively basic but covers everything you need for a good security system. You can further extend both the back-end and add front-end functionalities to implement new features, a nicer user interface and much more.


Tags : 12V/2A Solenoid Lockdatabasedoordoor accessDoorlock systemFingerprinti2cI2C 1602 LCDlcdMySQLR307 Fingerprint SensorRaspberry Pi ZerorfidRFID Reader kitRPisecurityspiUART

Leave a Response