Arduino Mega Relays Controlled By Push Buttons

Today we will be taking Arduino projects we have built in the past and combining them together. The Arduino Mega will be used for the main board. The Arduino will be connected to a ULN2003 that controls 3 LEDs and 3 relays. An I2C 2×16 LCD display will indicate which relay is selected. 2 normally open push buttons will be used to change the relay setting.

Advertisements
Advertisements

The LCD screen is attach to SDA, SDL and power. The sketch is pretty basic so it is easy to understand.

/*Arduino Mega Relay Control
  https://MacarrLabs.com/
  https://KM4NMP.com/
  Author: Matthew Carr, Macarr Labs LLC 6/1/19
  Arduino Mega Sketch
*/
//Libraries 
#include <Wire.h&gt;  
#include <LiquidCrystal_I2C.h&gt;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

const int UpButtonPin = 22;  //set up button pin
int UpButtonPinState = 0; //up button pin state
const int DownButtonPin = 24;  //set down button pin
int DownButtonPinState = 0;
const int OutputA = 52;  //set output a pin
const int OutputB = 50;  //set output b pin
const int OutputC = 48;  //set output c pin
int RelayNumber = 1;
  
void setup() {
  lcd.begin(16,2); //Start LCD
  lcd.backlight(); //Turn LCD Backlight on
  
  pinMode(UpButtonPin, INPUT);  //initialize the UpButtonPin as an input
  pinMode(DownButtonPin, INPUT);  //initialize the DownButtonPin as an input
  pinMode(OutputA, OUTPUT);  //initialize the Output A pin as an output
  pinMode(OutputB, OUTPUT);  //initialize the Output B pin as an output
  pinMode(OutputC, OUTPUT);  //initialize the Output C pin as an output


 lcd.setCursor(0,0);
 lcd.print(" MACARRLABS.COM");
 lcd.setCursor(0,1);
 lcd.print("   KM4NMP.COM");
 delay(3000);
 lcd.clear(); 
}

void loop() {
   //check button states
  UpButtonPinState = digitalRead(UpButtonPin);
  DownButtonPinState = digitalRead(DownButtonPin);
  
  if(UpButtonPinState == HIGH) {
    RelayNumber = RelayNumber + 1;
  }
  if(DownButtonPinState == HIGH) {
    RelayNumber = RelayNumber - 1;
  }
  if(RelayNumber &gt; 3) {  //put a max of 3 for relay number
    RelayNumber = 3;
  }
  if(RelayNumber < 1) {  //put a min of 1 for relay number
    RelayNumber = 1;
  }
  
  //check relay number and activate output according to number
if(RelayNumber == 1) {
  digitalWrite(OutputA, HIGH);
  lcd.setCursor(0,0);
  lcd.print("Relay 1");
  }
  else{
    digitalWrite(OutputA,LOW);
  }

if(RelayNumber == 2) {
  digitalWrite(OutputB, HIGH);
  lcd.setCursor(0,0);
  lcd.print("Relay 2");
  }
  else{
    digitalWrite(OutputB,LOW);
  }
if(RelayNumber == 3) {
  digitalWrite(OutputC, HIGH);
  lcd.setCursor(0,0);
  lcd.print("Relay 3");
  }
  else{
    digitalWrite(OutputC,LOW);
  }

 delay(150);
  
}
 


Next week our project will be based off of this Arduino project. We are going to tie this into some Amateur Radio equipment. Until then build this setup and test it out.

Advertisements

Links

https://km4nmp.com/2019/05/26/uln2003-and-arduino-programing-and-testing/

https://km4nmp.com/2019/05/25/connecting-the-uln2003-to-the-arduino-mega/

https://km4nmp.com/2019/05/11/lm7805-regulated-5-volts/

Latest Post

Advertisements

2 thoughts on “Arduino Mega Relays Controlled By Push Buttons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close