#include <Keypad.h>
const int ROWS = 4; // Number of rows in the keypad
const int COLS = 4; // Number of columns in the keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; // Connect to R1, R2, R3, R4 on the keypad
byte colPins[COLS] = {2, 3, 4, 5}; // Connect to C1, C2, C3, C4 on the keypad
const int relayPin = 10; // Connect to the Signal (IN) pin of the relay module
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initialize the relay as OFF
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key); // Print the pressed key to the Serial Monitor
// Check if the pressed key is a specific key (you can customize this part)
if (key == 'A') {
digitalWrite(relayPin, HIGH); // Turn ON the relay
} else if (key == 'B') {
digitalWrite(relayPin, LOW); // Turn OFF the relay
}
}
}