arduino-experiments

Overview

In this tutorial, we will use a keypad to control a relay via an Arduino. This project demonstrates how to create a secure access control system or automate devices using simple inputs.

Components Required

No Ads Available.

Circuit Diagram

Code


#include <Keypad.h>

// Define keypad configuration
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[rows] = {9, 8, 7, 6};
byte colPins[cols] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

// Relay pin
int relayPin = 10;

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.println(key);
    if (key == 'A') {
      digitalWrite(relayPin, HIGH); // Turn on relay
    } else if (key == 'B') {
      digitalWrite(relayPin, LOW); // Turn off relay
    }
  }
}
            

Steps

  1. Connect the keypad to the Arduino using the pins defined in the code.
  2. Connect the relay module to pin 10 of the Arduino.
  3. Upload the code to the Arduino and open the Serial Monitor to view keypad inputs.
  4. Press 'A' on the keypad to turn on the relay and 'B' to turn it off.

Contact Us

If you have any questions or inquiries, feel free to reach out to us at Microautomation.no@icloud.com .

Follow our Socials for the newest updates!