Keypad with Arduino Mega Analog inputs

I Want to connect keypad 4x4 with analog inputs of Arduino Mega as i need few more digital pins & rest of analog Pins from A8 to A15 is getting waste and finds no use for now

Please guide

Just specify the pins as A8 through A15 with whatever library you are using for the keypad, those pins are usable as digital input/output pins same as any of the digital pins.

I see

Presently have this one

const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

Use your analog pins in these arrays with the A8 A9 A10 etc notation

Just change the numbers for pin_rows and pin_columns to the appropriate pin numbers. The analog pins do need the designation that begins with A (such as A8, A9, etc). The Ax designation is actually a numeric value defined in one of the default files used by the compiler, so don't get confused that it isn't an actual number in your code.

O I see

Ok let ne go thru that one first with order of pins

You are aware that you can use analogue pins for digital IO?

Are they (A0 - A15) analog pins -- how? They are just A0-pin, .., A15-pin. They are termed as analog pins when the internal ADC is enabled which is OFF at power up. So, the A0-pin, .., A15-pin are digital IO pins/lines after power-up.

Yeah I am

So just like digital pins we can command it directly like

if (digitalRead(A8)==0)

{
	digitalWrite(LedPin13, HIGH);

}

Something like this ??

Good

So you have some options that you can look at

  1. Keypad on the analogue pins.
  2. Keypad on the digital-only pins and other inputs/outputs on the free analogue pins.

It all depends on the rest of your project what the better solution will be. If you e.g. need some pins for interrupts (INTx ), you can't move those pins to the analogue pins.

Yes

Yes!

OK le me go thru from that

What are interrupts and where they find it's application ??

Example??

Interrupts are used to temporary change the flow of a program to work on something that needs immediate attention. They are not limited to activity on a pin that I was refering to; e.g. when a byte is received on a serial port, an interrupt is triggered, the normal program flow is changed, the received byte is copied into a buffer and the normal program flow is restored. The timers in the 2560 chip can also generate interrupts.

The INTx interrupts (on a Mega, Int0..Int5) are e.g. used to count pulses from a hall sensor. Libraries for rotary encoders might also use them (I've never checked).

It's better to use "if (digitalRead(A8) == LOW)" and don't forget that you should set "pinMode(A8, INPUT);" or "pinMode(A8, INPUT_PULLUP);" to make it clear you are using the pin for an input.

Ya rotatory . . .

I see . .That's an interesting one

Yeah Yeah Sir ! I did same

Thanks :blush:

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