Activity 1. Basics with keypad 4x4
This activity uses a keypad 4x4. Five LEDs are connected to the Arduino Uno. The LEDs are turned on / off by the keypad. (50 minutes)
Activity 1
- If "0" is pressed, all LEDs turn off
- If "1" is pressed, the first LED turns on
- If "2" is pressed, the second LED turns on
- If "3" is pressed, the third LED turns on
- If "4" is pressed, the fourth LED turns on
- If "5" is pressed, the fifht LED turns on
- If anything else is pressed, all LEDs turn on
Step 1. Draw the circuit in Tinkercad.

Step 2. Study the code and write it on the microcontroller:
/* Keypad and LEDs
Circuit Connections: **LEDs PIN_0 => LED1_Anode - LED1_Cathode = > Resistor 150Ω => Gnd
PIN_1 => LED2_Anode - LED2_Cathode = > Resistor 150Ω => Gnd
PIN_2 => LED3_Anode - LED3_Cathode = > Resistor 150Ω => Gnd
PIN_3 => LED4_Anode - LED4_Cathode = > Resistor 150Ω => Gnd
PIN_4 => LED5_Anode - LED5_Cathode = > Resistor 150Ω => Gnd
**Keypad
PIN_5 => Column4
PIN_6 => Column3
PIN_7 => Column2
PIN_8 => Column1
PIN_9 => Row4
PIN_10 => Row3
PIN_11 => Row2
PIN_12 => Row1 */
//inlcude the library #include <Keypad.h>
#define led1 0 //give the name "led1" to PIN_0
#define led2 1 //give the name "led2" to PIN_2
#define led3 2 //give the name "led3" to PIN_2
#define led4 3 //give the name "led4" to PIN_3
#define led5 4 //give the name "led5" to PIN_4
const byte cols = 4; //four columns
const byte rows = 4; //four rows
//keypad output char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//configure the Arduino Uno - Keypad interface
byte row_pins[] = {12,11,10,9}; //pins connect to the rows
byte col_pins[] = {8, 7, 6, 5}; //pins connect to the columns
Keypad keypad = Keypad(makeKeymap(keys), row_pins, col_pins, rows, cols);
//variable to save keypad's charachters
char key;
//The setup() function initializes and sets the initial values
//It will only run once after each power up or reset
void setup(){
//Configure the PIN_0, PIN_1, PIN_2, PIN_3, PIN_4
//to behave as output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}
//loops consecutively
void loop(){
//get a key from keypad
key = keypad.getKey();
//check if a key is pressed
if (key != NO_KEY){
//call the function "set_leds"
set_leds(key); }
}
//this function configures the LEDs
//according to the key pressed
void set_leds(char x){
if(x=='0'){ //all LEDs are OFF
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}
else if(x=='1'){ //LED1 is ON
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}
else if(x=='2'){ //LED2 is ON
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}
else if(x=='3'){ //LED3 is ON
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}
else if(x=='4'){ //LED4 is ON
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
}
else if(x=='5'){ //LED5 is ON
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
}
else{ //all LEDs are ON
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
}
}
Step
3. Run
the simulation and check the correct operation of the circuit
Step 4. Suggested modifications and discussion:
- Adjust the code so that when “1” is pressed one LED turns on, when “2” is pressed two LEDs turn on ... when “5” is pressed five LEDs will turn on.
- Do we need to make any changes to the circuit?