8x8 KeyPad Controlling 16 Relays - Paid Help :)

Hello All,

I'm looking to pay for someone to write me a program to upload to my Arduino Mega. Basically I want an 4x4 Keypad to operate 2x 8 Relay Boards. Each Keypad press will either turn a relay on or off. Probably fairly simple to someone who knows coding.

So in short 16 keys on the keypad each one linked to a relay, press and the relay comes on, press again and the relay goes off.

Many thanks in advance :slight_smile:

Mike


The picture shows a 4 x 4 keypad. You talk about an 8 x 8 keypad.

You talk about each key controlling one relay, but 4 x 4 is 16 (and 8 x 8 is 64) and there aren't 16 (or 64) relays on that board.

Real requirements are going to be essential.

@paulS, I think it's just a mistake in the writing.
a 4x4 keypad like the one in the picture would be able to control 2 of those 8x relay boards, one relay per key :slight_smile:

My bad :slight_smile: Edited. Yes i mean 4x4

Thanks

Mike

Your 4 x 4 keypad will need 8 pins. Your relay boards will need 8 pins each. That’s 24 pins. Which Arduino do you have?

What you want to do seems really simple, if you have enough pins. The keypad library handles all the interactions with the keypad. A 16 element bool array, to keep track of whether the corresponding relay pin is such that the relay is on, and a 16 element byte array with the pin numbers, and a very few lines of code are all you need.

What part(s) are you have problems with?

Hi Paul, i have an arduino mega.

Im completely new to programming and will attempt to learn it over time. But to jump start this project i was happy to pay to get it going if that makes sense.

Thanks

Mike

A Mega will work great for what you’re doing.

See PM.

This pretty much is beginner level code, a great project to start learning with.

The keypad library makes reading the keypad dead easy.

After that it's indeed a matter of reading that keypad output and switching the appropriate relay.

I dont have a keypad to test it so try it out and let me know if it works. free of charge sir… its possible that it may not work. Im pretty new so it was a learning experience if it doesnt work post back so we can work out th bugs and we can both learn something.

[code]
 #include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte colPins[COLS] = { 6, 7, 8 ,9}; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.


Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );// Create the Keypad

#define relaypin1 36   
#define relaypin2 37
#define relaypin3 38
#define relaypin4 39
#define relaypin5 40
#define relaypin6 41
#define relaypin7 42
#define relaypin8 43
#define relaypin9 44
#define relaypin10 45
#define relaypin11 46
#define relaypin12 47
#define relaypin13 48
#define relaypin14 49
#define relaypin15 50
#define relaypin16 51

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin


void setup()
{
   pinMode(relaypin1,OUTPUT);
   pinMode(relaypin2,OUTPUT);
   pinMode(relaypin3,OUTPUT);
   pinMode(relaypin4,OUTPUT);
   pinMode(relaypin5,OUTPUT);
   pinMode(relaypin6,OUTPUT);
   pinMode(relaypin7,OUTPUT);
   pinMode(relaypin8,OUTPUT);
   pinMode(relaypin9,OUTPUT);
   pinMode(relaypin10,OUTPUT);
   pinMode(relaypin11,OUTPUT);
   pinMode(relaypin12,OUTPUT);
   pinMode(relaypin13,OUTPUT);
   pinMode(relaypin14,OUTPUT);
   pinMode(relaypin15,OUTPUT);
   pinMode(relaypin16,OUTPUT);
  
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '1':
               {
  reading = digitalRead(relaypin1);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin1, state);

  previous = reading;
}
        
      case '4':
              {
  reading = digitalRead(relaypin4);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin4, state);

  previous = reading;
}
        
      case '2':
                {
  reading = digitalRead(relaypin2);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin2, state);

  previous = reading;
}
        
      case '5':
            {
  reading = digitalRead(relaypin5);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin5, state);

  previous = reading;
}
        
      case '3':
             {
  reading = digitalRead(relaypin3);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin3, state);

  previous = reading;
}
        
      case '6':
              {
  reading = digitalRead(relaypin6);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin6, state);

  previous = reading;
}
        
      case 'A':
             {
  reading = digitalRead(relaypin11);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin11, state);

  previous = reading;
}
        
      case 'B':
             {
  reading = digitalRead(relaypin12);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin12, state);

  previous = reading;
}
        
      case '0':
            {
  reading = digitalRead(relaypin10);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin10, state);

  previous = reading;
}
    
      case '8':
            {
  reading = digitalRead(relaypin8);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin8, state);

  previous = reading;
}
        
      case '*':
             {
  reading = digitalRead(relaypin16);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin16, state);

  previous = reading;
}
  
      case '7':
             {
  reading = digitalRead(relaypin7);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin7, state);

  previous = reading;
}
     
      case '#':
              {
  reading = digitalRead(relaypin15);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin15, state);

  previous = reading;
}
     
      case '9':
        {
  reading = digitalRead(relaypin9);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin9, state);

  previous = reading;
}
      
      case 'D':
                   {
  reading = digitalRead(relaypin14);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin14, state);

  previous = reading;
}

       case 'C':
              {
  reading = digitalRead(relaypin13);
   { if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      }

  digitalWrite(relaypin13, state);

  previous = reading;
}
        
      default:
      Serial.println(key);
    }
  }
}

[/code]

Hi Tater -

Already done and sent. I’ll check with the OP to see if he has any issues with me posting the code so you can see how it was done.

Pat.

patduino:
Hi Tater -

Already done and sent. I'll check with the OP to see if he has any issues with me posting the code so you can see how it was done.

Pat.

That would be awesome! Thanks!

Did this ever get sorted to work?

I too have a 4x4 and 16 relay board (not an 8). Still trying to learn the basics and thought this might give me a start,
Thanks

Markcbz:
Did this ever get sorted to work?

I too have a 4x4 and 16 relay board (not an 8). Still trying to learn the basics and thought this might give me a start,
Thanks

it’s pretty simple… I would do something like this

#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

const byte rowPins[ROWS] = { 7, 6, 5, 4 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
const byte colPins[COLS] = { 11, 10, 9, 8}; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.

// plug the relays to those pins#
const byte relayPins[] = {  // depicting the maping to the keypad
  36, 37, 38, 39,           // 1  2  3  A
  40, 41, 42, 43,           // 4  5  6  B
  44, 45, 46, 47,           // 7  8  9  C
  48, 49, 50, 51            // *  0  #  D
};

char keys[ROWS][COLS] = { // Define the Keymap, we don't care about symbols, it's an index (+1) in the relay arrray
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12},
  {13, 14, 15, 16}
};

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

void setup()
{
  for (byte i = 0; i < 16; i++)   {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);
  }
}

void loop()
{
  byte key = keypad.getKey();
  if (key) digitalWrite(relayPins[key - 1], !digitalRead(relayPins[key - 1]));
}

typed here, fully untested :slight_smile:

relays are power hungry, so they need their own power supply, join ground… and depending on your relays, I would not turn them all at once as each channel usually need 15 to 20mA each.

I don’t remember from the top of my head but I think you should not go above 200mA in total for all pins and various ports have individual limits… → 320mA might (will likely) overwhelm the capacity of the Arduino Mega

so there is probably a need for a proper design of the commands and choosing pins to the relays that are not from the same ports but scattered amongst the ports , may be with transistors.