i understand there is a library to make using these devices easy, but since it looks like a simple enough circuit, i'd like to try it out from first principles.
as it involves connecting Arduino pins from an OUTPUT to an INPUT, i'd like to just confirm that what i'm about to do is infact safe !
my understanding is that it basically follows the same logic for multiplexing LED matrices, ie. drive a row (or column) HIGH, and then read which column (or row) is also HIGH meaning, the corresponding key is pressed.
my code then is like this;
(it's just a glorified continuity test to confirm which keys are pressed)
const byte row1 = 6;
const byte row2 = 7;
const byte row3 = 8;
const byte row4 = 9;
const byte col1 = 10;
const byte col2 = 11;
const byte col3 = 12;
const byte col4 = 13;
void setup() {
pinMode(row1,OUTPUT);
pinMode(row2,OUTPUT);
pinMode(row3,OUTPUT);
pinMode(row4,OUTPUT);
// col-pins are 'normal' INPUT with external pull-down 10k resistors
Serial.begin(9600);
}
void loop() {
digitalWrite(row1,HIGH);
Serial.print("row1:ON\t");
Serial.print(digitalRead(col1));
Serial.print(digitalRead(col2));
Serial.print(digitalRead(col3));
Serial.print(digitalRead(col4));
digitalWrite(row1,LOW);
Serial.println("\trow1:OFF");
digitalWrite(row2,HIGH);
Serial.print("row2:ON\t");
Serial.print(digitalRead(col1));
Serial.print(digitalRead(col2));
Serial.print(digitalRead(col3));
Serial.print(digitalRead(col4));
digitalWrite(row2,LOW);
Serial.println("\trow2:OFF");
digitalWrite(row3,HIGH);
Serial.print("row3:ON\t");
Serial.print(digitalRead(col1));
Serial.print(digitalRead(col2));
Serial.print(digitalRead(col3));
Serial.print(digitalRead(col4));
digitalWrite(row3,LOW);
Serial.println("\trow3:OFF");
digitalWrite(row4,HIGH);
Serial.print("row4:ON\t");
Serial.print(digitalRead(col1));
Serial.print(digitalRead(col2));
Serial.print(digitalRead(col3));
Serial.print(digitalRead(col4));
digitalWrite(row4,LOW);
Serial.println("\trow4:OFF");
}
are there any safety concerns that i might have overlooked with the above ?
...
OK, so i had a peek at the library code and at first glance, it looks like it dispenses with the external resistors and uses INPUT_PULLUP
.
this of course inverts the logic, and that means the one 'axis' is driven LOW, and then a key press is also indicated by a LOW reading on the input pin.
this means that the four output pins are normally ALL driving HIGH, right ?
how much current is then flowing out (and back IN?) the Arduino ?