Maker.io main logo

How to Make a Raspberry Pi Voice-Activated Door Lock

2018-07-10 | By Maker.io Staff

License: GNU Lesser General Public License Raspberry Pi

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!

 

 

Things You Will Need

Schematic

Schematic

Check out the full schematic here.

Hardware

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.

Copy Code
import RPi.GPIO as GPIO
import time
import speech_recognition as sr

# Configure Varaibles
command = ""
password = "open this door please"
voiceOK = False
r = sr.Recognizer()

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).

Copy Code
# Configure GPIO
solenoidPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(solenoidPin, GPIO.OUT)
GPIO.output(solenoidPin, GPIO.LOW)

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):

print("Speak:")
audio = r.listen(source)

try:
speechString = r.recognize_google(audio)
print(speechString)
voiceOK = True
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))

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.

Copy Code
        # Determine if the password is correct

if(speechString == password):
GPIO.output(solenoidPin, GPIO.HIGH)
print("ACCESS GRANTED")
time.sleep(2)
GPIO.output(solenoidPin, GPIO.LOW)
else:
print("ACCESS DENIED")

Full Code

Copy Code
import RPi.GPIO as GPIO
import time
import speech_recognition as sr

# Configure Varaibles
command = ""
password = "open this door please"
voiceOK = False
r = sr.Recognizer()

# Configure GPIO
solenoidPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(solenoidPin, GPIO.OUT)
GPIO.output(solenoidPin, GPIO.LOW)

while(1):
with sr.Microphone() as source:

voiceOK = False

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

print("Speak:")
audio = r.listen(source)

try:
speechString = r.recognize_google(audio)
print(speechString)
voiceOK = True
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))

# Determine if the password is correct

if(speechString == password):
GPIO.output(solenoidPin, GPIO.HIGH)
print("ACCESS GRANTED")
time.sleep(2)
GPIO.output(solenoidPin, GPIO.LOW)
else:
print("ACCESS DENIED")

Construction

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.

Construction

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.

制造商零件编号 CF14JT1K00
RES 1K OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
¥0.81
Details
制造商零件编号 2N3904BU
TRANS NPN 40V 0.2A TO92-3
onsemi
¥1.55
Details
制造商零件编号 OSA-SH-205DM5,600
RELAY GEN PURPOSE DPST 5A 5V
TE Connectivity Potter & Brumfield Relays
¥15.34
Details
制造商零件编号 1N5817
DIODE SCHOTTKY 20V 1A DO41
onsemi
More Info
Details
制造商零件编号 700-00012
BREADBOARD TERM STRIP 1.75X1.38"
Parallax Inc.
¥33.63
Details
制造商零件编号 153
JUMPER WIRE M TO M VARIOUS
Adafruit Industries LLC
¥40.29
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum