switching relays with 3x4 keypad

hello everyone, i want to switch a relay (actually 4, but i cant even get one to work) by typing in a code on a keypad
-board: arduino uno
-relay board: HL-54 V1.0
-keypad: 3x4 matrix keypad

this is what i have ( code from arduinogetstarted that i modified):

#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
const int RELAY_PIN = 2;  // the Arduino pin, which connects to the IN pin of relay


char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password = "1234"; // change your password here
String input_password;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
  pinMode(RELAY_PIN, OUTPUT);
}

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

  if (key){
    Serial.println(key);

    if(key == '*') {
      input_password = ""; // clear input password
    } else if(key == '#') {
      if(password == input_password) {
        Serial.println("password is correct");
        digitalWrite(RELAY_PIN, HIGH);
        delay(500);
        digitalWrite(RELAY_PIN, LOW);
        delay(500);// DO YOUR WORK HERE
        
      } else {
        Serial.println("password is incorrect, try again");
      }

      input_password = ""; // clear input password
    } else {
      input_password += key; // append new character to input password string
    }
  }
}

the password works fine (it reacts as it should in the 'monitor', but the relay is being unpredictable (relay sometimes open and sometimes closed on reboot, switches relay on and off quickly after accepting code, in other cases relay stays closed or open indefinitely, arduino seems to power pin 1 when pressing buttons on keypad(?))
i tried adding delay, but no improvement :frowning:

i hope i've posted this the right way, i tried figuring it out myself but wasted several hours to no avail. In no way do i expect someone to just write me the code, i hope someone can point out what i'm doing wrong. This is my first project btw, i'm trying to learn.
thanks in advance!

How are the relays and the Arduino powered, exactly?

the arduino is connected to my laptop, relay is connected to the arduino;
-gnd to gnd,
-5V arduino to Vcc relay,
-pin 2 arduino to in1 relay board

thx

It may very well be a power supply problem. Replace the relay open/close with Serial.print() statements just telling you what the relay would do. If that works, it's a power problem. I don't see anything obviously wrong with the code.

try clearing the password right after it is successful

if(password == input_password) {
    input_password = "";

Hi,
Have you been able to write code to JUST turn the relays ON and OFF.
Have you got code to JUST read the keypad correctly?

Thanks.. Tom... :slight_smile:

@wvmarle
thanks for the tip!
i tried it:

 if(password == input_password) {
        Serial.println("password is correct");
        Serial.println("RELAY_PIN, HIGH");
        Serial.println("RELAY_PIN, LOW");
       
        
      } else {
        Serial.println("password is incorrect, try again");

but it gives me: (is there a better way to publish the monitor output btw?)
password is correct
RELAY_PIN, HIGH

so no "relay pin low" but a little square with a ">" in it. (hard to tell what the symbol is cause its so small, it looks different in the monitor itself)

jmanatee:
try clearing the password right after it is successful

if(password == input_password) {

input_password = "";

i think i already have that piece of code (unless i did it wrong of course )
it's on line 59

 } else {
        Serial.println("password is incorrect, try again");
      }

      input_password = ""; // clear input password
    } else {
      input_password += key; // append new character to input password string
    }

thanks

@tomgeorge
i started with code for the keypad i took from arduinogetstarted, and that works fine, it recognizes the input, and outputs "wrong password"/"correct password" as it should.
to test the relayboard i used code from the same source(see below). it makes the relay open/close indefinitly. also works fine

const int RELAY_PIN = 2;  // the Arduino pin, which connects to the IN pin of relay

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 2 as an output.
  pinMode(RELAY_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(RELAY_PIN, HIGH);
  delay(500);
  digitalWrite(RELAY_PIN, LOW);
  delay(500);
}

thanks

As most of your code is missing it's impossible to get a good idea of what's (supposed to be) going on.

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