# Keypad and servo not working

**URL:** <https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425>\
**Category:** Programming\
**Created:** [June 19, 2024, 4:20pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425 "2024-06-19T16:20:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![coder-infinitez](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@coder-infinitez](https://forum.arduino.cc/u/coder-infinitez)\
**Post date:** [June 19, 2024, 4:20pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/1 "2024-06-19T16:20:14Z")

</div>

```arduino
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the custom library
#include <Servo.h>

// Keypad configuration
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display

// Servo configuration
Servo myServo;

String inputCode = "";
const String pass1 = "4444";
const String pass2 = "5555";
const String pass3 = "1729";

void setup() {
  lcd.begin(); // Initialize the LCD with 16 columns and 2 rows
  lcd.backlight();
  myServo.attach(10); // Attach servo to pin 10
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
  myServo.write(0);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    if (key == '#') {
      checkPassword();
      inputCode = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    } else if (key == '*') {
      inputCode = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    } else {
      inputCode += key;
      lcd.setCursor(0, 1);
      lcd.print(inputCode);
    }
  }
}

void checkPassword() {
  if (inputCode == pass1 || inputCode == pass2 || inputCode == pass3) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Access Granted");
    myServo.write(90); // Rotate servo to 90 degrees
    delay(3000); // Keep servo in position for 3 seconds
    myServo.write(0); // Rotate servo back to 0 degrees
    lcd.clear();
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Access Denied");
    delay(2000); // Display Access Denied for 2 seconds
    lcd.clear();
  }
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
}

```

Hi,  
The servo motor is just pulsing and not moving and the 789C row of the 4x4 keypad does not work  
We tried changing the servo and the keypad.  
Thank you

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 19, 2024, 4:24pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/2 "2024-06-19T16:24:06Z")

</div>

Welcome to the forum

Which Arduino board are you using and which pins of the Arduino do you have the LCD connected to ?

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [June 19, 2024, 4:33pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/3 "2024-06-19T16:33:44Z")

</div>

> [@coder-infinitez](#):
>
> We tried changing the servo and the keypad.

Try the test sketch you used to familiarize yourself with servos and the test sketch you used to familiarize yourself with keypads.

If you don't have two sketches (three actually, the LCD clu,d use one, too) like that, write them.

Test your wiring and your hardware.

Divide and conquer.

How are you providing power to the servo?

a7

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [June 19, 2024, 7:12pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/4 "2024-06-19T19:12:52Z")

</div>

Post an annotated schematic showing exactly how you have this wired, be sure to include all power sources.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 20, 2024, 11:57am UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/5 "2024-06-20T11:57:13Z")

</div>

> [@coder-infinitez](#):
>
> ```arduino
> } else {
> inputCode += key;
> lcd.setCursor(0, 1);
> lcd.print(inputCode);
> }
> 
> ```

in loop(), the default always appends to `inputCode`. But what if the keypad wasn't pressed? don't you need to check for `NO_KEY` and only process a return value when it is not NO\_KEY?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 20, 2024, 12:33pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/6 "2024-06-20T12:33:41Z")

</div>

> [@coder-infinitez](#):
>
> and the 789C row of the 4x4 keypad does not work

Check your Keypad wiring as per Fig-1:

 ![keypadxy](https://europe1.discourse-cdn.com/arduino/original/4X/c/d/1/cd1e2d2af6f3afe2ef788d6e6286b3080b3ac088.png)  
Figure-1:

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [June 20, 2024, 12:37pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/7 "2024-06-20T12:37:18Z")

</div>

> [@gcjr](#):
>
> only process a return value when it is not NO\_KEY

I think this is what does that

```arduino
  char key = keypad.getKey();

  if (key) { // is there a key

```

Besides, if that wasn't doing it, the string would rapidly become the size of the known universe.

a7

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 20, 2024, 12:42pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/8 "2024-06-20T12:42:08Z")

</div>

thanks, i missed that

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2024, 12:42pm UTC](https://forum.arduino.cc/t/keypad-and-servo-not-working/1273425/9 "2024-12-17T12:42:12Z")

</div>

Closed on December 17, 2024, 12:42pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
