# Programming a keypad without a library - doesn't work

**URL:** <https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085>\
**Category:** Programming\
**Created:** [December 27, 2023, 12:06am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085 "2023-12-27T00:06:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 12:06am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/1 "2023-12-27T00:06:03Z")

</div>

Hello, I tried to program a 4x4 keypad to my liking using the 'keypad.h' library but as I am relatively new to the arduino platform and coding in general, I couldn't figure out how I could utilise the language reference to the corresponding library.

So I decided to to program a 4x4 keypad to my liking without using a library, I came up with the following code:

```arduino
// all the needed libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// initialising the pins of the lcd
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 
/*schematic of the keypad
  C4 C3 C2 C1
R1 * * * * 
R2 * * * *
R3 * * * *
R4 * * * *
*/

// variables for the column pins
int C4_pin = 2;
int C3_pin = 3;
int C2_pin = 4;
int C1_pin = 5;

// booleans for the column pins, to check if these pins are HIGH or LOW
bool C4_value = true;
bool C3_value = true;
bool C2_value = true;
bool C1_value = true;

//variables for the row pins
int R1_pin = 6;
int R2_pin = 7;
int R3_pin = 8;
int R4_pin = 9;

// booleans for the row pins, to check if these pins are HIGH or LOW
bool R1_value = true;
bool R2_value = true;
bool R3_value = true;
bool R4_value = true;

// array for the column pins
byte columnPins[4] = {C4_pin, C3_pin, C2_pin, C1_pin};

// array for the row pins
byte rowPins[4] = {R1_pin, R2_pin, R3_pin, R4_pin};

void setup() {
  Serial.begin(9600);
    // to initialise the row pins and set their mode
    for(int i = 0; i<4; ++i){
      pinMode(rowPins[i], OUTPUT);
      digitalWrite(rowPins[i], LOW);}
    // to initialise the column pins and set their mode
    for(int x = 0; x<4; ++x){
      pinMode(columnPins[x], OUTPUT);
      digitalWrite(rowPins[x], HIGH);}}
      

void loop() {
  // to check if a button has been pressed
    if(digitalRead(columnPins[0] == LOW)){C4_value == false;}
    if(digitalRead(columnPins[1] == LOW)){C3_value == false;}
    if(digitalRead(columnPins[2] == LOW)){C2_value == false;}
    if(digitalRead(columnPins[3] == LOW)){C4_value == false;}
    
    // to figure out the placemant of the pressed button: column 4
    if(C4_value == false){digitalWrite(rowPins[0], HIGH);}
    if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");
    Serial.println("wished character");}
    
    if(C4_value == false){digitalWrite(rowPins[1], HIGH);}
    if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
    
    if(C4_value == false){digitalWrite(rowPins[2], HIGH);}
    if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
    
    if(C4_value == false){digitalWrite(rowPins[3], HIGH);}
    if (digitalRead(columnPins[0]) == HIGH){lcd.print("wished character");}
    
    // to figure out the placemant of the pressed button: column 3
    if(C3_value == false){digitalWrite(rowPins[0], HIGH);}
    if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
    
    if(C3_value == false){digitalWrite(rowPins[1], HIGH);}
    if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
    
    if(C3_value == false){digitalWrite(rowPins[2], HIGH);}
    if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
  
    if(C3_value == false){digitalWrite(rowPins[3], HIGH);}
    if (digitalRead(columnPins[1]) == HIGH){lcd.print("wished character");}
    
    // to figure out the placemant of the pressed button: column 2
    if(C2_value == false){digitalWrite(rowPins[0], HIGH);}
    if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
    
    if(C2_value == false){digitalWrite(rowPins[1], HIGH);}
    if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
    
    if(C2_value == false){digitalWrite(rowPins[2], HIGH);}
    if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
    
    if(C2_value == false){digitalWrite(rowPins[3], HIGH);}
    if (digitalRead(columnPins[2]) == HIGH){lcd.print("wished character");}
    
    // to figure out the placemant of the pressed button: column 1
    if(C1_value == false){digitalWrite(rowPins[0], HIGH);}
    if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
    
    if(C1_value == false){digitalWrite(rowPins[1], HIGH);}
    if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
    
    if(C1_value == false){digitalWrite(rowPins[2], HIGH);}
    if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
    
    if(C1_value == false){digitalWrite(rowPins[3], HIGH);}
    if (digitalRead(columnPins[3]) == HIGH){lcd.print("wished character");}
  
    }

```

The problem is, it doesn't work for some reason. I uploaded a photo of my circuit that I built, I doubt that there is anything wrong with that so I think that there's something wrong with either my code or my understanding of 4x4 keypads. Any help would be greatly appreciated.

Note: my knowledge of 4x4 keypads is based of this article: [How to Set Up a Keypad on an Arduino - Circuit Basics](https://www.circuitbasics.com/how-to-set-up-a-keypad-on-an-arduino/)

Note: the uploaded diagram of the circuit doesn't contain a 4x4 keypad so you would have to imagine one, sorry.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/c/6/0c6cc964304a72df469548eb9ab3986e6be7ec29.png)

---

<div class="post-metadata">

**Author:** ![odometer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/odometer/32/1013044_2.png) [@odometer](https://forum.arduino.cc/u/odometer)\
**Post date:** [December 27, 2023, 1:28am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/3 "2023-12-27T01:28:12Z")

</div>

> [@psbs1](#):
>
> ```arduino
> // to check if a button has been pressed
> if(digitalRead(columnPins[0] == LOW)){C4_value == false;}
> if(digitalRead(columnPins[1] == LOW)){C3_value == false;}
> if(digitalRead(columnPins[2] == LOW)){C2_value == false;}
> if(digitalRead(columnPins[3] == LOW)){C4_value == false;}
> 
> ```

I believe you need to review the difference between `=` and `==`, and when to use one and when to use the other.

---

<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:** [December 27, 2023, 2:35am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/4 "2023-12-27T02:35:38Z")

</div>

@psbs1, I think one of these should be C1\_val

```arduino
    if(digitalRead(columnPins[0] == LOW)){C4_value == false;}
    if(digitalRead(columnPins[1] == LOW)){C3_value == false;}
    if(digitalRead(columnPins[2] == LOW)){C2_value == false;}
    if(digitalRead(columnPins[3] == LOW)){C4_value == false;}

```

and yes, check out **=** versus **==**.

Also, you never set those values, and maybe some others, back to true so next time they might get set false.

Whether you switch to a library and figure out libraries or not, I think it would be worth, now or one day, getting your code as you conceived it, to work.

I can't look closer from where I am. At a glance it appears that you are trying to implement something you've learned about as far as scanning a keyboard goes.

Libraries come with examples, they shouldn't be too hard.

a7

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [December 27, 2023, 3:09am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/5 "2023-12-27T03:09:05Z")

</div>

> [@psbs1](#):
>
> it doesn't work

~~Try this site...~~ See post **#6 and post #7**... safety concern... **do not use this link verbatim**

> **[How to Read Keypad with Arduino without Library](https://www.ee-diary.com/2020/10/How-to-Read-Keypad-with-Arduino-without-Library.html)**
>
> In this tutorial you will learn how to write keypad program with Arduino without using library.

... it works as a simulation for me. (HOWEVER: see post #6 and post #7... **simulation only**

> **[keypadWithoutLibrary - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/385233845980132353)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [December 27, 2023, 4:44am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/6 "2023-12-27T04:44:03Z")

</div>

!!!! DO NOT !!!! set all the row and column pins to OUTPUT mode at the same time.  
Pressing a key on the keypad will directly short an output HIGH to an output LOW, likely damaging your Arduino.

```arduino
void setup() {
  Serial.begin(9600);
  // to initialise the row pins and set their mode
  for (int i = 0; i < 4; ++i) {
    pinMode(rowPins[i], OUTPUT);
    digitalWrite(rowPins[i], LOW);
  }
  // to initialise the column pins and set their mode
  for (int x = 0; x < 4; ++x) {
    pinMode(columnPins[x], OUTPUT);
    digitalWrite(rowPins[x], HIGH);
  }
}

```

If you look at the code in the Keypad library, the column pins are all set to INPUT\_PULLUP, and the row pins are also set to INPUT mode. During the scan of the keypad, one row pin at a time is set to OUTPUT mode and used to output a LOW, then the column pins are each read to determine if a key is pressed, after which the row pin is set back to HIGH and then to INPUT mode. At no time is there more than one pin set to OUTPUT mode.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [December 27, 2023, 5:13am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/7 "2023-12-27T05:13:22Z")

</div>

> [@xfpd](#):
>
> Try this site...
> 
> [How to Read Keypad with Arduino without Library | ee-diary](https://www.ee-diary.com/2020/10/How-to-Read-Keypad-with-Arduino-without-Library.html)
> 
> ... it works as a simulation for me.

Be careful with that code on a real Arduino, I notice all the column pins are in OUTPUT mode. Pressing two keys in the same row at the same time will short two columns together.

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 7:41am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/8 "2023-12-27T07:41:11Z")

</div>

It isn't a school assignment, I just coudn't get the hang of the library's functions as I couldn't find any information about them.

I programmed that circuit and drew the schematic of the circuit at 1AM my time, so they both contain a load of errors and as others in this thread may have pointed out, some coding errors certainly were the issue. Forgive me.

Thank you for pointing out that I didn't initialise the lcd, I did change the code to include Serial before posting this post but I decided to delete to prevent others from asking why only one button would print as I only wrote Serial.print under one button.

Thanks

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 7:43am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/9 "2023-12-27T07:43:32Z")

</div>

Yep thank you, the second if statement should contain a single equal sign instead of two.

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 7:47am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/10 "2023-12-27T07:47:07Z")

</div>

Thank you alto777,

I will change one to C1, thank you for pointing that one out. I should've used a single equals to sign to asign a value to variable and as you stated, I will make a piece of could that will set them back to true.

Thanks,

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 7:48am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/11 "2023-12-27T07:48:03Z")

</div>

Thank you Xfpd,  
I will be sure to check your sites out.

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 8:05am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/12 "2023-12-27T08:05:02Z")

</div>

Thank you for pointing that out, I did not know that. Could you please explain to me how the normal INPUT mode is different than the INPUT\_PULLUP mode?

Thanks a lot for your explanation, I now know how to properly rewrite my code.

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 8:12am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/13 "2023-12-27T08:12:14Z")

</div>

The lcd is initialised, my bad

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 27, 2023, 8:55am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/14 "2023-12-27T08:55:26Z")

</div>

> [@psbs1](#):
>
> the second if statements should contain a single equal sign instead of two.

this is not the only error in this line

> [@alto777](#):
>
> `if(digitalRead(columnPins[0] == LOW)){C4_value == false;}`

see the parenthesis

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [December 27, 2023, 11:13am UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/15 "2023-12-27T11:13:47Z")

</div>

> [@psbs1](#):
>
> Could you please explain to me how the normal INPUT mode is different than the INPUT\_PULLUP mode?

The INPUT\_PULLUP mode activates an internal pull-up resistor, so that the input pin is pulled to HIGH level when no key is pressed. If you do not use that, and there are no external pull-up or pull-down resistors, then the input would be floating when no key is pressed, causing it to randomly read HIGH or LOW. Floating inputs are very sensitive to picking up any electrical noise.

---

<div class="post-metadata">

**Author:** ![psbs1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/psbs1/32/981206_2.png) [@psbs1](https://forum.arduino.cc/u/psbs1)\
**Post date:** [December 27, 2023, 1:27pm UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/17 "2023-12-27T13:27:12Z")

</div>

Thank you very much!

---

<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:** [June 24, 2024, 1:27pm UTC](https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/18 "2024-06-24T13:27:42Z")

</div>

Closed on June 24, 2024, 1:27pm UTC

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