Blog > Projects > Control a Relay by Matrix Keyboard
Control a Relay by Matrix Keyboard

admin

2017-03-14 11:43:03

Projects

224 Views

0 Comments

Control Relay By Matrix Keyboard


Introduction

Today, in this experiment, we do a keyboard control relay opening and closing.Press the keys 1 to 8 to control 8 relays separately.Press the key 0 to turn off the 8-way relay.

Components

- 1 * Adeept 2560 Board

- 1 * 8 Channel Relay Module

- 1 *  4*4 Matrix Keyboard

- 8 * Male To Male Jumper Wires

- 8 * Male To Female Jumper Wires

Experimental Principle

In order to save the resources of the microcontroller port, we usually connect the buttons in a matrix in an actual project.

The following is the schematics of 4x4 matrix keyboard:



In this tutorial, we use the ‘Keypad’ function library. Before programming, please install the library.

Keypad



This is a 5V 8-channel replay interface board, be able to control various appliance and other equipments with large current. It can be controlled directly by Microcontroller(Arduino,8051,AVR,PIC,DSP,ARM,MSP430,TTL logic). 5V 8-Channel reply, interface board and each one needs 15-20mA driver current. Equipped with high-current relay, AC250V 10A; DC30V 10A. Equipped with screw holes for easy installation.

Experimental Procedures

Step 1: Build the circuit



Step 2: Program controlRelayByMatrixKeyboard.ino

/***********************************************************
File name: controlRelayByMatrixKeyboard.ino

Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/03/14 
***********************************************************/
#include 
 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
 
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

const int relayPin1 = 14;
const int relayPin2 = 15;
const int relayPin3 = 16; 
const int relayPin4 = 17; 
const int relayPin5 = 18; 
const int relayPin6 = 19; 
const int relayPin7 = 20; 
const int relayPin8 = 21; 
 
void setup(){
  Serial.begin(9600);//Open serial
  pinMode(relayPin1, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin2, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin3, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin4, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin5, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin6, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin7, OUTPUT); //initialize the relayPin as an output
  pinMode(relayPin8, OUTPUT); //initialize the relayPin as an output
}
   
void loop(){
  char customKey = customKeypad.getKey();//Read Key data   
  if (customKey){
    Serial.println(customKey);          //send the key data by serial port (UART)
  }
  switch(customKey){
    case '0':
          digitalWrite(relayPin1, LOW); //drive relay closure conduction
          digitalWrite(relayPin2, LOW); //drive relay closure conduction
          digitalWrite(relayPin3, LOW); //drive relay closure conduction
          digitalWrite(relayPin4, LOW); //drive relay closure conduction
          digitalWrite(relayPin5, LOW); //drive relay closure conduction
          digitalWrite(relayPin6, LOW); //drive relay closure conduction
          digitalWrite(relayPin7, LOW); //drive relay closure conduction
          digitalWrite(relayPin8, LOW); //drive relay closure conduction
          break;
    case '1':
          digitalWrite(relayPin1, HIGH); //drive relay closure conduction
          break;
    case '2':
          digitalWrite(relayPin2, HIGH); //drive relay closure conduction
          break;
    case '3':
          digitalWrite(relayPin3, HIGH); //drive relay closure conduction
          break;
    case '4':
          digitalWrite(relayPin4, HIGH); //drive relay closure conduction
          break;
    case '5':
          digitalWrite(relayPin5, HIGH); //drive relay closure conduction
          break;
    case '6':
          digitalWrite(relayPin6, HIGH); //drive relay closure conduction
          break;
    case '7':
          digitalWrite(relayPin7, HIGH); //drive relay closure conduction
          break;
    case '8':
          digitalWrite(relayPin8, HIGH); //drive relay closure conduction
          break;
    default: break;   
    }
}

Step 3: Compile and download the sketch to the 2560 board.




Last article : How to install and use Geany IDE in the Raspberry Pi or Ubuntu?
Next article : Indoor Environment Monitor In Kit -- Snake Game

COMMENTS

Identifying:

  Related Products
  Related Blogs
  • CONNECT WITH US 
Copyright © 2016 Adeept.com All Rights Reserved.