Keypad is not working on analog pins

Hi everyone,

I am new to Arduino, and I am currently working on a project to create a credit card payment system using RFID tags and an Arduino Uno. I am using an LCD screen, the RFID sensor, and I would also like to use a keypad to enter the price of the items.

Due to the lack of digital pins, I have used both digital and analogue pins for connecting my keypad. However, when I connect the keypad, it does not allow me to enter any numbers. Can someone help??

Thanks in advance :slight_smile:

#include <Wire.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above
char price = 0;
char MoneyLeft = 10;
MFRC522 mfrc522(SS_PIN, RST_PIN); 
String value = "A9 A4 DE 8B";// Create MFRC522 instance
float cost = 0;
int c = 0;
int decimal =0;
int rs = 7;
int en=8;
int d4 = 3;
int d5= 4;
int d6= 5;
int d7 = 6;
int buzzPin = 2;
int buzztime = 500;
int t= 0;
float addition = 0;
const int buzzer =2;

String thirdscan ="";
String lastscan = "";

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
String serialnumbers[] = {"A9 A4 DE 8B", "0A FA 82 16"};
String names[] = {"Alex", "Marta"};
float account[] = {10, 20};
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'#','0','*', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 1, 0, A0, A1 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { A2, A3, A4, A5}; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char rawprice [] ={};
int i = 0;



void setup() {
 Serial.begin(9600);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
 lcd.begin(16,2);
 pinMode(buzzer, OUTPUT);
}

void loop() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
lcd.setCursor(0,0);


  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();


  content.toUpperCase();
  for (c = 0; c <= 1; c++){
       if (content.substring(1) == serialnumbers[c]) //change here the UID of the card/cards that you want to give access
  {

           
        tone(buzzer, 1600);
        delay(100);// ...for 1 sec
        noTone(buzzer);
        lcd.clear();
        if (lastscan != content.substring(1)){
          lcd.print(names[c]);
          lcd.print("'s account:");
          lcd.setCursor(0,1);
          lcd.print(account[c]);
          delay(1500);
          lcd.clear();
          lcd.print("Scan again to");
          lcd.setCursor(0,1);
          lcd.print("buy something...");
          
          }
        else{  
          
     Serial.print("How much does this cost");
       lcd.print("Item Cost: ");
       lcd.setCursor(0,1);
      for (i = 0; i<= 4; i++) {
        rawprice[i] = {kpd.getKey()};
        if (rawprice[i] == '#'){
          
          break;
        }
         
      }
      if (rawprice[1] == '*'){
        decimal = 1;
        rawprice[2] = {rawprice[2]*0.1};
        rawprice[3] = {rawprice[3]*0.01};
        price = rawprice[0] + rawprice[2] + rawprice[3];
      }
      else{
        float price = rawprice[0] + rawprice [1] + rawprice[2] + rawprice[3];
      }
      
      
       
      lcd.print(price);
        
        delay(1500);
        lcd.clear();
      float money = account[c];
      if (money - price >= 0){
      float (money -= price);
      account[c] = (float)money;
    Serial.println();
    Serial.println(money);
    lcd.print("Money left: ");
    lcd.setCursor(0,1);
    lcd.print(money);
    delay(1500);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Success!");
   }
    else{
      lcd.print("Not enough");
      lcd.setCursor(0,1);
      lcd.print("money in account");
    }
        }}
    delay(1500);
    char rawprice [] = {};
  }
  thirdscan = lastscan;
  lastscan = content.substring(1);
  content.substring(1) ="";
  }
byte rowPins[ROWS] = { 1, 0, A0, A1 };

It is really not a good idea to use digital pins 0 and 1 as they are used by the serial interface of the Uno and you are printing within the program

When you want to add something, you better test only that part. Remove everything else from the board and use a small test-sketch to test the Keypad. Or use another Arduino Uno (clone) for testing.

For an Arduino Uno, you can use all the analog pins as digital pins.
However, you may not use pin 0 and 1, because those are used for the serial monitor and to upload a sketch as UKHeliBob already wrote.

Do you use pin 11, 12, 13 ? Pin 13 is connected to the led on the Uno board. With a Arduino Uno R3, you can use pin 13 also as input.

Those LCD displays can also be used with the I2C bus, then they have a extra backpack board on the back.
Tutorial with such a I2C LCD display: I2C LCD WITH TEXT AND CUSTOM SYMBOLS - Arduino tutorial #11 - YouTube

The available DPins of UNO are: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14(A0), 15(A1), 16(A2), 17(A3), 18(A4), 19(A5).

You have used this: int buzzPin = 2;

You have used this: LiquidCrystal lcd(rs, en, d4, d5, d6, d7); ==> LiquidCrystal lcd(7, 8, 3, 4, 5, 6);

You have used this: MFRC522 mfrc522(SS_PIN, RST_PIN); ==> MFRC522 mfrc522(10, 9);

You have used this: byte colPins[COLS] = { A2, A3, A4, A5};

You have used this: byte rowPins[ROWS] = { 1, 0, A0, A1 };
Change the above to this: byte rowPins[ROWS] = { 11, 12, A0, A1 }; and accordingly change keypad connection.

GolamMostafa:
MFRC522...
byte rowPins[ROWS] = { 11, 12, A0, A1 }; and accordingly change keypad connection.

So now you are using SPI and SPI pins with different functions? Seems strange at least.

Whandall:
So now you are using SPI and SPI pins with different functions? Seems strange at least.

My bad -- I have ignored OP and Post#2 to observe that OP's sketch uses SPI Port (DPins: 10, 11, 12, 13).

Now, Post#3 is revised as follows:

The available DPins of UNO are: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14(A0), 15(A1), 16(A2), 17(A3), 18(A4), 19(A5).

Use DPin-2 for buzzPin.

Use this: byte colPins[COLS] = {3, 4, 5, 6}; and change Keypad connection accordingly.
Use this: byte rowPins[ROWS] = {A0, A1, A2, A3}; and change Keypad connection accordingly.

Use DPins: 7 and 8 for this: MFRC522 mfrc522(7, 8);

DPin-9 is unused.

Use DPins: 10, 11, 12, 13 for SPI Port

Use A4 and A5 for I2CLCD (forget the use of Parallel Port driven LCD as there is shortage of DPins in UNO).

Alex, if you look harder you can find keypads that use only the analog input to identify the key being pressed. They work very well. The one I used also allowed an identification to be placed in the transparent keycap.

Paul

Thanks for all the replies everyone,

Unfortunately, I am already using all of the digital pins (except for 0 and 1) with the RFID rc522, the LCD screen and the buzzer. So I only have 6 analogue pins, even though I need 8 for the keypad.

alexoort:
Thanks for all the replies everyone,

Unfortunately, I am already using all of the digital pins (except for 0 and 1) with the RFID rc522, the LCD screen and the buzzer. So I only have 6 analogue pins, even though I need 8 for the keypad.

Get a different keypad and you need only one analog pin.

Paul

Use an i2c port expander for the keypad and or an i2c LCD module.

More good stuff in this thread:
https://forum.arduino.cc/index.php?topic=668486.0