Arduino KEYPAD LCD SHIELD RELAY CONTROL.

Arduino KEYPAD LCD SHIELD RELAY CONTROL.

Hello world! today i wrote a simple code to control 4 relays with a keypad LCD shield for arduino. You can add any device and the code is easy to modify. I used 1 lamp as a example device. The video displays dutch text on the lcd screen but the code i uploaded is english, “apparaat” means device.

Schematic:
arduino keypad shield code, Arduino KEYPAD LCD SHIELD

Just wire it up like the schematic, upload the code and it should work.
If things don’t work you probably don’t have the library LiquidCrystal.h installed.

Parts list:
Arduino mega 2560, I used the RobotDyn Mega 2560
4 x 1 channel relay KY-019
LCD shield with keypad. <- i bought a bunch of those. shipping fast Jumper wires set MM/FF/MF Power cables + lamp sockets.(optional)

Code:

/*
Arduino KEYPAD LCD SHIELD
Author:Danny van den Brande, Arduinosensors.nl. BlueCore Tech.
Hello world! Today i made a small example on how to use a LCD keypad.
You can add anything to it. In this example we switch on and off relays.
Good luck!
 */
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int relay1 = 30;
int relay2 = 31;
int relay3 = 32;
int relay4 = 33;

int lcdkeys;
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("BlueCore Tech");
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
}
void loop() {
  Serial.println(lcdkeys);// look in the serial monitor and press buttons to see values.
  lcdkeys=analogRead(0);
  if (lcdkeys<50) {  //if value less then 50 
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("DEVICE 1");
  lcd.setCursor(0, 1);
  lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, HIGH);
  }
  else if(lcdkeys<195) { //if value less then 195 and so on. 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 2");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    
  }
  else if(lcdkeys<380) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 3");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
  }
  else if(lcdkeys<650) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 4");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, LOW);
  }
  else if(lcdkeys<1023) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("BLUECORE TECH");
    lcd.setCursor(0, 1);
    lcd.print("OFF");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);  
  }
}
Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

2 thoughts on “Arduino KEYPAD LCD SHIELD RELAY CONTROL.”

  1. Having trouble with this. Can’t choose relay 4. When choosen, lcd prints “Device 1” So i get D1,D2,D3,D1 when i go trough the buttons. This is on a UNO board however.
    Help appriciated

Leave a Reply

Your email address will not be published. Required fields are marked *