Use Keypad to turn LED's on and off

Hey Guys I am new to arduino. I was wondering if you could help me. I want to have a keypad that when pressed will turn off and on an LED.
e.g.
If I press 1 then # I want it to turn LED1 on
If I press 1 then * I want it to turn LED1 off

Wiring Diagram:

Code:

#include <Wire.h>
#include <Keypad.h>
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
int LED4 = 12;
int LED5 = 13;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup()   /****** SETUP: RUNS ONCE ******/
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600); 
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop()  
{
 char key = keypad.getKey();
   
    
  	if (key == '1' && key == '#'){
    Serial.println(key);
      digitalWrite(LED1,HIGH);
  }    

    if (key == '2' && key == '#'){
    Serial.println(key);
      digitalWrite(LED2,HIGH);
    } 
     
     if (key == '3' && key == '#'){
    Serial.println(key);
      digitalWrite(LED3,HIGH);
     }
  
    if (key == '4' && key == '#'){
    Serial.println(key);
      digitalWrite(LED4,HIGH);
    }
  
    if (key == '5' && key == '#'){
    Serial.println(key);
      digitalWrite(LED5,HIGH);
    }
  
  
  
    if (key == '1' && key == '*'){
    Serial.println(key);
      digitalWrite(LED1,LOW);
  }    

    if (key == '2' && key == '*'){
    Serial.println(key);
      digitalWrite(LED2,LOW);
    } 
     
     if (key == '3' && key == '*'){
    Serial.println(key);
      digitalWrite(LED3,LOW);
     }
  
    if (key == '4' && key == '*'){
    Serial.println(key);
      digitalWrite(LED4,LOW);
    }
  
    if (key == '5' && key == '*'){
    Serial.println(key);
      digitalWrite(LED5,LOW);
    }
  }

Thanks for your help

LEDs must have a series current limiting resistor.

Add a 470 ohm resistor to each LED.


Please edit your post, place your code using the </> code button in the posting menu
or use CTRL E.


This:

... can never be right, since a variable can never have two values at the same time. If it's 3 for example as shown, it can't also be a #.

You probably need to think about moving the value of key into another variable called previouskey or something, then check for previouskey and key to have the respective values.

But that said, it's a few years since I used a keypad, and I can't recall exactly how they work. (And all my Arduino sketches are on an old laptop which I don't have easy access to.)

Hello
I think to have an array for the variable key should help.

Here is a start, you can finish the sketch by adding the missing cases.

#include <Wire.h>
#include <Keypad.h>

const byte LED1 = 9;
const byte LED2 = 10;
const byte LED3 = 11;
const byte LED4 = 12;
const byte LED5 = 13;

byte myLEDis = 255;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//******************************************************************************
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);

} //END of setup()


//******************************************************************************
void loop()
{
  char key = keypad.getKey();

  switch (key)
  {
    //******************
    case '1':
      myLEDis = LED1;
      break;

    //******************
    case '#':
      if (myLEDis != 255)
      {
        digitalWrite(myLEDis, HIGH);
      }
      break;



  } //END of switch/case

} //END of loop()
1 Like

Hello
Great KISS solution, I like it. :+1:

Thankyou @LarryD for your help,

Here is the finished code for all 5 LED's:

#include <Wire.h>
#include <Keypad.h>

const byte LED1 = 9;
const byte LED2 = 10;
const byte LED3 = 11;
const byte LED4 = 12;
const byte LED5 = 13;

byte myLEDis = 255;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//******************************************************************************
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);

} //END of setup()


//******************************************************************************
void loop()
{
  char key = keypad.getKey();

  switch (key)
  {
    //******************
    case '1':
      myLEDis = LED1;
      break;
    
    case '2':
      myLEDis = LED2;
      break;
    
    case '3':
      myLEDis = LED3;
      break;
    case '4':
      myLEDis = LED4;
      break;
    
	case '5':
      myLEDis = LED5;
      break;
    //******************
    case '#':
      if (myLEDis != 255)
      {
        digitalWrite(myLEDis, HIGH);
      }
      break;
    
    case '*':
      if (myLEDis != 255)
      {
        digitalWrite(myLEDis, LOW);
      }
      break;



  } //END of switch/case

} //END of loop()

Just one more question.
What would I have to do to get 0 to turn off and on all the LED's?

const byte allLEDs[] = {LED1, LED2, LED3, LED4, LED5};

. . . 

    //******************
    case '0':
      for (byte x = 0; x < sizeof(allLEDs); x++)
      {
        digitalWrite(allLEDs[x], LOW);
      }
      break;

Test

How would you add 4 (i.e. LED6, LED7, LED8, LED9) more LEDs ?

Regarding

#include <Wire.h>

Wire.begin();

Just thought I'd mention that the Wire library is not needed for what's being done here.

#include <Keypad.h>

const byte LED1 = 9;
const byte LED2 = 10;
const byte LED3 = 11;
const byte LED4 = 12;
const byte LED5 = 13;

byte myLEDis = 255;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// -- | -- | -- | -- | -- | -- | -- | -- | -- 
void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);

} //END of setup()

// -- | -- | -- | -- | -- | -- | -- | -- | -- 
void loop()
{
  char key = keypad.getKey();

  switch (key)
  {
    //******************
    case '1':
      myLEDis = LED1;
      break;

    //******************
    case '#':
      if (myLEDis != 255)
      {
        digitalWrite(myLEDis, HIGH);
      }
      break;

  } //END of switch/case

} //END of loop()

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