Arduino - Keypad - Relay
In this tutorial, we are going to learn how to use a keypad, relay, and Arduino together. In detail, If a user inputs the password on the keypad correctly, Arduino turns the relay on.
The tutorial also provides the code that turns on a relay in a period of time and then turns off without using delay() function. The Arduino code also supports multiple passwords.
Or you can buy the following sensor kit:
|1||×||DIYables Sensor Kit 30 types, 69 units|
About Keypad and Relay
If you do not know about keypad and Relay (pinout, how it works, how to program ...), learn about them in the following tutorials:
This image is created using Fritzing. Click to enlarge image
Arduino Code - turn relay on if the password is correct
The below codes turn a relay on if the password is correct
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- On Arduino IDE, Go to Tools Manage Libraries
- Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
- Click Install button to install keypad library.
- Search “ezOutput”, then find the ezOutput library by ArduinoGetStarted
- Click Install button to install ezOutput library.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Press 1234 keys and press #
- Press 9765 keys and press #
- See the result on Serial Monitor and the state of relay
Authorized passwords are pre-defined in the Arduino code.
A string is used to store the password inputted by users, called input string. In keypad, two keys (* and #) are used for special purposes: clear password and terminate password. When a key on keypad is pressed:
- If the pressed key is not two special keys, it is appended to the input string
- If the pressed key is *, input string is clear. You can use it to start or re-start inputing the password
- If the pressed key is #:
- The input string is compared with the pre-defined passwords. If it matched with one of the pre-defined passwords, the relay is turned on.
- No matter the password is correct or not, the input string is clear for the next input
Arduino Code - turn a relay on in a period of time if the password is correct
The below code turns the relay on for 5 seconds if the password is correct. After 5 seconds, the relay is turned off.
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.