arduino keypad & servo
Arduino keypad & servo
Thanks for sharing the image. I see that your circuit contains an Arduino Uno, a 4x4 keypad, a 16x2 LCD display (not using I2C), and a servo motor. Below is the code to implement your requirements:
- When the correct sequence `9865` is entered, the LCD displays `"Lock Open"` and the servo motor rotates to 90 degrees.
- When the correct sequence `6754` is entered, the LCD displays `"Locked"` and the servo motor returns to 0 degrees.
- If an incorrect sequence is entered, the LCD will display `"Wrong Password"`.
### Components Used:
- **Arduino Uno**
- **4x4 Keypad**
- **Servo Motor**
- **16x2 LCD Display**
- **Breadboard & Jumper Wires**
### Wiring Setup:
1. **Keypad**:
- Connect the row pins of the keypad to Arduino pins: `9, 8, 7, 6`.
- Connect the column pins of the keypad to Arduino pins: `5, 4, 3, 2`.
2. **LCD Display**:
- `RS` to pin `12`
- `EN` to pin `11`
- `D4` to pin `5`
- `D5` to pin `4`
- `D6` to pin `3`
- `D7` to pin `2`
- `VSS` to `GND`
- `VDD` to `5V`
- `RW` to `GND`
- Potentiometer for contrast adjustment (center pin to `V0`)
3. **Servo Motor**:
- Signal pin to `pin 10`
- Power pin to `5V`
- Ground pin to `GND`
### Arduino Code:
```cpp
#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>
// Define the number of rows and columns of the keypad
const byte ROWS = 4;
const byte COLS = 4;
// Define the symbols on the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define the pins connected to the rows and columns of the keypad
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Create an LCD object (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Create a Servo object
Servo myServo;
// Passwords
String openPassword = "9865";
String closePassword = "6754";
String inputPassword = "";
void setup() {
lcd.begin(16, 2); // Initialize the LCD
myServo.attach(10); // Attach the servo motor to pin 10
myServo.write(0); // Set the servo to the locked position (0 degrees)
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey(); // Read the pressed key
if (key) {
if (key == '#') {
// Check entered password
if (inputPassword == openPassword) {
lcd.clear();
lcd.print("Lock Open");
myServo.write(90); // Unlock the servo (90 degrees)
delay(2000);
} else if (inputPassword == closePassword) {
lcd.clear();
lcd.print("Locked");
myServo.write(0); // Lock the servo (0 degrees)
delay(2000);
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(2000);
}
// Clear input and reset LCD display
inputPassword = "";
lcd.clear();
lcd.print("Enter Password:");
} else if (key == '*') {
// Clear input
inputPassword = "";
lcd.clear();
lcd.print("Enter Password:");
} else {
// Add the key to the input password and display it
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
}
```
### Explanation:
1. **Libraries**:
- `Keypad.h`: Handles keypad inputs.
- `LiquidCrystal.h`: Manages the LCD display.
- `Servo.h`: Controls the servo motor.
2. **Password Logic**:
- When `#` is pressed, the code checks if the entered password matches `openPassword` or `closePassword`.
- If the correct password (`9865`) is entered, it displays `"Lock Open"` on the LCD and rotates the servo to `90` degrees.
- If the correct password (`6754`) is entered, it displays `"Locked"` on the LCD and rotates the servo back to `0` degrees.
- If an incorrect password is entered, the LCD displays `"Wrong Password"`.
3. **Password Entry**:
- The user can enter the password using the keypad.
- `*` clears the current input.
- `#` is used to confirm the password.
### Notes:
- Ensure the contrast of the LCD is set correctly using a potentiometer so that you can see the text clearly.
- Adjust the servo motor power connection if necessary to ensure it has enough current to move.
This code will allow you to use the keypad to control the servo motor and display relevant messages on the LCD.
Comments
Post a Comment