Keypad 4x4 lcd 16x2 - display not working

Hey guys, I am starting with arduino and I have this code that I and ChatGPT created, but it does not work. The display does not light up. Is my wiring the problem or the code?

#include <LiquidCrystal.h>

const int servoPin = A4;
const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
const int numberOfRows = 4;
const int numberOfColumns = 4;

const int rowPins[numberOfRows] = { A3, A2, A1, A0 };
const int columnPins[numberOfColumns] = { 7, 8, 9, 10 };
const char keyMap[numberOfRows][numberOfColumns] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, { '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } };

char correctCode[4] = { '1', '2', '3', '4' };
char code[4] = { 'n', 'n', 'n', 'n' };
unsigned int codePosition = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  Serial.begin(9600);
  for (int row = 0; row < numberOfRows; row++) {
    pinMode(rowPins[row], INPUT);
    digitalWrite(rowPins[row], HIGH);
  }
  for (int column = 0; column < numberOfColumns; column++) {
    pinMode(columnPins[column], OUTPUT);
    digitalWrite(columnPins[column], HIGH);
  }
  pinMode(servoPin, OUTPUT);
  digitalWrite(servoPin, LOW);
  lcd.begin(16, 2);
  resetToMainScreen();
}

void loop() {
    char pressedKey = getPressedKey();
    if (pressedKey != 0) {
      if (codePosition <= 3 && ((pressedKey >= '0' && pressedKey <= '9') || (pressedKey >= 'A' && pressedKey <= 'D'))) {
        lcd.setCursor(12 + codePosition, 0);
        lcd.print(pressedKey);
        code[codePosition] = pressedKey;
        codePosition++;
      } else if (pressedKey == '*') {
        resetToMainScreen();
      } else if (pressedKey == '#') {
        if (correctCode[0] == code[0] && correctCode[1] == code[1] && correctCode[2] == code[2] && correctCode[3] == code[3]) {
          displayWrite2Lines("Correct code", "");
          digitalWrite(servoPin, HIGH);
          delay(2000);
          digitalWrite(servoPin, LOW);
          resetToMainScreen();
        } else { 
          displayWrite2Lines("Wrong code", "try again!");
          delay(2000);
          resetToMainScreen();
        }
      }
    }
}

char getPressedKey() {
  char key = 0;
  for (int column = 0; column < numberOfColumns; column++) {
    digitalWrite(columnPins[column], LOW);
    for (int row = 0; row < numberOfRows; row++) {
      if (digitalRead(rowPins[row]) == LOW) {
        delay(20);
        while (digitalRead(rowPins[row]) == LOW);
        key = keyMap[row][column];
      }
    }
    digitalWrite(columnPins[column], HIGH);
  }
  return key;
}

void displayWrite2Lines(String text1, String text2) {
  lcd.clear();
  lcd.noCursor();
  lcd.setCursor(0, 0);
  lcd.print(text1);
  lcd.setCursor(0, 1);
  lcd.print(text2);
}

void resetToMainScreen() {
  codePosition = 0;
  for (int i = 0; i <= 3; i++) {
    code[i] = 'n';
  }
  displayWrite2Lines("Enter  code:", "# Submit * Clear");
  lcd.cursor();
}

If you use ChatGPT, you need to understand and correct the poor results it provides.

Find a simple LCD sketch and verify the wiring.

1 Like

No. you not starting in Arduino. You starting in ChatGPT.

If you were really starting out in Arduino, you would start by doing simple things to get started using Arduino, the language used in it and the use of the libraries.

In your case, for example, using a Keypad with routines within your code instead of using the keypad library.
This is not practical and is more complex.

Nope. Not even close. Check the servo examples installed with the IDE. Files|Examples|Servo|Knob, or File|Examples|Servo|Sweep.

I tested your code in the simulator and it worked correctly.
Except for the problem already reported by @camsysca
Did you connect the LCD's RW pin to gnd?

Review your connections and if it still doesn't work, change the jumpers for the LCD.
jumper are a good cause of problems.

PS: Servo need a analog pin. A4 not is analog pin.
Look this code was simulated and work ok. I have used pin 6 to servo.

#include <LiquidCrystal.h>
#include <Servo.h>
//const int servoPin = A4;
const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
const int numberOfRows = 4;
const int numberOfColumns = 4;
const int rowPins[numberOfRows] = { A3, A2, A1, A0 };
const int columnPins[numberOfColumns] = { 7, 8, 9, 10 };
const char keyMap[numberOfRows][numberOfColumns] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, { '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } };
char correctCode[4] = { '1', '2', '3', '4' };
char code[4] = { 'n', 'n', 'n', 'n' };
unsigned int codePosition = 0;
Servo myservo;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//------------------------------------------------------------------
void setup() {
  myservo.attach(6);
  Serial.begin(9600);
  for (int row = 0; row < numberOfRows; row++) {
    pinMode(rowPins[row], INPUT);
    digitalWrite(rowPins[row], HIGH);
  }
  for (int column = 0; column < numberOfColumns; column++) {
    pinMode(columnPins[column], OUTPUT);
    digitalWrite(columnPins[column], HIGH);
  }
 // pinMode(servoPin, OUTPUT);
  //digitalWrite(servoPin, LOW);
   myservo.write(0);  
  lcd.begin(16, 2);
  resetToMainScreen();
}
//------------------------------------------------------------------
void loop() {
    char pressedKey = getPressedKey();
    if (pressedKey != 0) {
      //lcd.clear();
       //Serial.println(pressedKey);
      if (codePosition <= 3 && ((pressedKey >= '0' && pressedKey <= '9') || (pressedKey >= 'A' && pressedKey <= 'D'))) {
        lcd.setCursor(12 + codePosition, 0);
        lcd.print(pressedKey);
        code[codePosition] = pressedKey;
        codePosition++;
      } else if (pressedKey == '*') {
        resetToMainScreen();
      } else if (pressedKey == '#') {
        if (correctCode[0] == code[0] && correctCode[1] == code[1] && correctCode[2] == code[2] && correctCode[3] == code[3]) {
          displayWrite2Lines("Correct code", "");
          //digitalWrite(servoPin, HIGH);
           myservo.write(180);  
          delay(2000);
          //digitalWrite(servoPin, LOW);
           myservo.write(0);  
          resetToMainScreen();
        } else { 
          displayWrite2Lines("Wrong code", "try again!");
          delay(2000);
          resetToMainScreen();
        }
      }
    }
}
//------------------------------------------------------------------
char getPressedKey() {
  char key = 0;
  for (int column = 0; column < numberOfColumns; column++) {
    digitalWrite(columnPins[column], LOW);
    for (int row = 0; row < numberOfRows; row++) {
      if (digitalRead(rowPins[row]) == LOW) {
        delay(20);
        while (digitalRead(rowPins[row]) == LOW);
        key = keyMap[row][column];
      }
    }
    digitalWrite(columnPins[column], HIGH);
  }
  return key;
}
//------------------------------------------------------------------
void displayWrite2Lines(String text1, String text2) {
  lcd.clear();
  lcd.noCursor();
  lcd.setCursor(0, 0);
  lcd.print(text1);
  lcd.setCursor(0, 1);
  lcd.print(text2);
}
//------------------------------------------------------------------
void resetToMainScreen() {
  codePosition = 0;
  for (int i = 0; i <= 3; i++) {
    code[i] = 'n';
  }
  displayWrite2Lines("Enter  code:", "# Submit * Clear");
  lcd.cursor();
}

Servos need a digital pin, not analog. But A4 will do, though it really should be referred to as "18" as it's being used as D18.
Servos also need some form of servo library; the default, Servo.h, is a good beginner level choice; if it is to be used,
#include <Servo.h>
is missing from the code, as is the necessary invocation of servo, and the pin attach statement. I deliberately did not explicitly mention those facts in my first comment, as I wanted the poster to go look at the examples, not just slavishly cut and paste from my response.

You are correct, it was my mistake of not remembering that the servo.h library, which I used, does the PWM work on any pin.

Corrected code for pin A4.

#include <LiquidCrystal.h>
#include <Servo.h>
const int servoPin = A4;
const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
const int numberOfRows = 4;
const int numberOfColumns = 4;
const int rowPins[numberOfRows] = { A3, A2, A1, A0 };
const int columnPins[numberOfColumns] = { 7, 8, 9, 10 };
const char keyMap[numberOfRows][numberOfColumns] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, { '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } };
char correctCode[4] = { '1', '2', '3', '4' };
char code[4] = { 'n', 'n', 'n', 'n' };
unsigned int codePosition = 0;
Servo myservo;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//------------------------------------------------------------------
void setup() {
  myservo.attach(servoPin);
  Serial.begin(9600);
  for (int row = 0; row < numberOfRows; row++) {
    pinMode(rowPins[row], INPUT);
    digitalWrite(rowPins[row], HIGH);
  }
  for (int column = 0; column < numberOfColumns; column++) {
    pinMode(columnPins[column], OUTPUT);
    digitalWrite(columnPins[column], HIGH);
  }
  //pinMode(servoPin, OUTPUT);
  //digitalWrite(servoPin, LOW);
   myservo.write(0);  
  lcd.begin(16, 2);
  resetToMainScreen();
}
//------------------------------------------------------------------
void loop() {
    char pressedKey = getPressedKey();
    if (pressedKey != 0) {
      //lcd.clear();
       //Serial.println(pressedKey);
      if (codePosition <= 3 && ((pressedKey >= '0' && pressedKey <= '9') || (pressedKey >= 'A' && pressedKey <= 'D'))) {
        lcd.setCursor(12 + codePosition, 0);
        lcd.print(pressedKey);
        code[codePosition] = pressedKey;
        codePosition++;
      } else if (pressedKey == '*') {
        resetToMainScreen();
      } else if (pressedKey == '#') {
        if (correctCode[0] == code[0] && correctCode[1] == code[1] && correctCode[2] == code[2] && correctCode[3] == code[3]) {
          displayWrite2Lines("Correct code", "");
          //digitalWrite(servoPin, HIGH);
           myservo.write(180);  
          delay(2000);
          //digitalWrite(servoPin, LOW);
           myservo.write(0);  
          resetToMainScreen();
        } else { 
          displayWrite2Lines("Wrong code", "try again!");
          delay(2000);
          resetToMainScreen();
        }
      }
    }
}
//------------------------------------------------------------------
char getPressedKey() {
  char key = 0;
  for (int column = 0; column < numberOfColumns; column++) {
    digitalWrite(columnPins[column], LOW);
    for (int row = 0; row < numberOfRows; row++) {
      if (digitalRead(rowPins[row]) == LOW) {
        delay(20);
        while (digitalRead(rowPins[row]) == LOW);
        key = keyMap[row][column];
      }
    }
    digitalWrite(columnPins[column], HIGH);
  }
  return key;
}
//------------------------------------------------------------------
void displayWrite2Lines(String text1, String text2) {
  lcd.clear();
  lcd.noCursor();
  lcd.setCursor(0, 0);
  lcd.print(text1);
  lcd.setCursor(0, 1);
  lcd.print(text2);
}
//------------------------------------------------------------------
void resetToMainScreen() {
  codePosition = 0;
  for (int i = 0; i <= 3; i++) {
    code[i] = 'n';
  }
  displayWrite2Lines("Enter  code:", "# Submit * Clear");
  lcd.cursor();
}

Why concentrate on coding when wiring is not verified?

Why concentrate on anything, when the OP is clearly not present?