We use cookies to provide our customers with an optimal site experience. View our privacy statement to learn more about how we use cookies and how to manage your settings. By proceeding on our website you consent to the use of cookies.

How to Make a Raspberry Pi Voice-Activated Door Lock

This month, we looked a lot at the Raspberry Pi and how we can program it using Python. So, in this project, we will combine a simple external circuit with the Raspberry Pi to create a simple door-locking mechanism that takes advantage of the speech recognition library!

First, gather the hardware needed for this project. The goal of this project is to have a locking mechanism that will only unlock for a spoken password. The lock itself uses a simple 12V solenoid that is driven using a relay and 2N3904 transistor, which is controlled by the Raspberry Pi. R1 is a current limiting resistor so that neither the Raspberry Pi output pin nor the 2N3904 transistor draw too much current. If they do draw too much current, they could be permanently damaged, and considering that the GPIO on a Raspberry Pi is not replaceable, this is something that must not happen!

Our circuit also includes a diode in parallel with the solenoid. Under normal conditions, this diode is in reverse bias mode and conducts no current. However, when the solenoid switches, a large negative voltage forms, which can damage the BJT. This is why the diode is included; it will conduct electricity when a negative voltage is present, and this protects the BJT as well as the Raspberry Pi.

The whole system is provided power with the help of an ATX PSU, which provides both 5V and 12V. Even though most users connect a micro USB lead into the Pi as a source of power, a 5V source can be directly connected to the GPIO. An ATX PSU is also used because the solenoid in this project is 12V, and 5V is not enough to engage it (ATX PSUs supply 5V and 12V). This PSU also has a switch connected between the green wire and black wire found on the large header; this enables the PSU to switch on and off!

Software

The software needed to run this project is rather simple, and it takes advantage of Python and the speech recognition library. The program starts by importing the speech recognition library, the GPIO library, the time library, and declaring some variables.

With the variables declared, the GPIO pin that controls the solenoid needs to be configured. Thanks to the GPIO library, this is incredibly easy and is done using the code below. The first line defines the GPIO number that we are using (GPIO4), the second line configures our numbering system to use the BCM number system (not too important), the third line sets up GPIO4 to be an output pin, and the last line sets this output to be low (this ensures that the solenoid is in its lock position).

Once all the variables have been declared and the GPIO pins have been configured, the main loop is executed. The first task in this loop is a second loop, which listens for speech and then sends this speech to Google’s speech recognition service. If this speech is understood, then the string speechString will contain the string that was spoken, and the program will exit out of the microphone loop. If the speech was not understood, the program will repeat the microphone loop.

Copy Code

voiceOK = False

# Keep running until the voice is understood by Google while(voiceOK == False):

With the audio converted to a string, we must now compare this string to the password. If the two strings match, then we turn the solenoid on, which unlocks the door for two seconds before relocking the door (this prevents doors being left unlocked). If the strings do not match, then the system denies entry and goes back to the microphone loop to await the next spoken command.

This project requires the use of an external circuit connected to the Raspberry Pi, there are multiple options available. These options include a breadboard, a stripboard, a matrix board, and even a custom PCB. However, to keep this project simple, I have used a breadboard to hold the transistor driver and jumper wires to connect the Raspberry Pi to the driver.

To execute our program, we can access the Pi over LAN using SSH (secure shell). Since our program runs indefinitely, we only have to log in once and execute the Python program, and then the system should start working.