How to Add and Test the Relay for Your Keypad Entry System Arduino Project
Once the code has been added to your Arduino, you now need to add the relay that controls the door mechanism and the power supply. You use the same power supply to operate your Arduino, because it won’t be getting power from the USB connection anymore. Don’t plug your power transformer into the wall until you are ready to power up and test the system.
How to connect your power supply
You set up the breadboard with two power rails, a 12V DC rail on top for your Arduino and door lock, and a 5V DC rail on the bottom, which supplies the Max 72xx. First, connect the output lead of the 12V DC power transformer to the top two columns of your breadboard.
You probably need to cut and strip the output wire from your transformer to do this. You solder the wires to pin headers to make it easy to connect them into your breadboard.
Use your multimeter to test the transformer’s output wires, identifying the positive and negative connections. Don’t rely on the diagram on the transformer or markings on the wires. If you get the polarity of the connections wrong, you could fry your Arduino.
You can now supply the power for your Arduino, using the Vin and GND headers at the bottom of the board, instead of the USB port or the black barrel connector on the left side. Use a jumper wire to connect the 12V power rail to the Vin pin and connect the ground rail to GND.
The last step is to create a common ground between the two power rails. Use a jumper wire to connect the negative column of the 12V DC rail to the negative column of the 5V DC rail.
How to connect your transistor and relay
You also need to supply power to operate the 5V relay. Use a jumper wire to connect from the 5V header on your Arduino to the bottom power rail. The relay simply controls whether the door mechanism receives power and when it does, the lock is released. The door locking mechanism needs about 400mA to operate, which is ten times the amount of current your Arduino’s digital pins can provide.
Switching power is one of the basic and common tasks that electronic circuits do, so there are many ways to control power. This project uses an Arduino digital pin to apply power to a transistor (itself a solid-state switch) that actuates your relay.
The transistor has three pins — an emitter, a base, and a collector, labeled E, B, and C. Current will flow between the collector and emitter when current from a digital pin is applied at the base of the transistor.
The current from the collector is what actuates your relay, closing its contacts together and allowing power from your power supply to activate the door locking mechanism. In effect, you have a digital switch (the Arduino pin) that controls a digital switch (the transistor), which in turn controls an electro-mechanical switch that activates the door mechanism!
It is also possible to use a power switching transistor to control the door lock directly, but the 2N2222 transistor is cheaper and easy to find. Plus, there are a lot of applications where relay control is handy, especially if you want to electrically isolate the circuit being controlled from your Arduino to control household electricity, for example. You could easily adapt this part of the project to other applications.
Add your relay, diode, and transistor to the breadboard. Make sure the transistor is in the right orientation. The emitter should be connected to ground. Add the 2.2kΩ resistor as shown. It prevents too much current from flowing to the transistor’s base. Connect the collector to your relay and connect Pin 9 of your Arduino to your transistor’s base.
Now take a moment to double-check all your connections, paying close attention to the positive and negative polarity of the wires. It’s a pain, but you don’t want to power things up incorrectly because you might end up damaging your hardware. After checking your connections, plug in your power supply.
After your Arduino boots up, you’re ready to roll. Enter your code into the keypad to test the relay. You’ll probably hear a faint click as the relay coil is energized. Add jumper wires to the relay’s output pins and connect your multimeter’s probes to them. When you enter the right code, the meter should show a connection.
The final step is to connect the two leads of your door mechanism to the ground rail and the output of your relay. Do that and enter your access code again. If everything’s working properly, you should be rewarded by a satisfying click, as your Arduino does its magic and opens the lock.