Arduino - Keypad - Servo Motor
In this tutorial, we are going to learn how to use a keypad, servo motor, and Arduino together:
- If a user inputs an authorized password on the keypad, Arduino rotates the servo motor to 90°.
- After a period of time, the Arduino rotates the servo motor back to 0° without using delay() function.
- The Arduino code also supports multiple passwords.
|1||×||Arduino UNO or Genuino UNO|
|1||×||USB 2.0 cable type A/B|
|1||×||5V Power Adapter|
|1||×||DC Power Jack|
About Keypad and Servo Motor
If you do not know about keypad and servo motor (pinout, how it works, how to program ...), learn about them in the following tutorials:
Image is developed using Fritzing. Click to enlarge image
Arduino Code - rotates Servo Motor if the password is correct
The below code turns the servo motor to 90° for 5 seconds if the password is correct. After 5 seconds, it turns the servo motor to 0°.
- 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.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Press 12345#
- Press 5642B#
- See the result on Serial Monitor and the state of servo motor
Authorized passwords are pre-defined in the Arduino code.
A string is used to store the password inputted by users, called input_password. 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 one of two special keys, it is appended to the input_password
- If the pressed key is *, input_password is clear. You can use it to start or re-start inputing the password
- If the pressed key is #:
- The input_password is compared with the pre-defined passwords. If it matched with one of the pre-defined passwords, rotate the servo motor to 90°.
- Regardless the password is correct or not, the input_password is clear for the next input
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.