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.


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
  Author: Matthew Carr, Macarr Labs LLC 6/1/19
  Arduino Mega Sketch
#include <Wire.h>  
#include <LiquidCrystal_I2C.h>

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.print(" MACARRLABS.COM");
 lcd.print("   KM4NMP.COM");

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 > 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.print("Relay 1");

if(RelayNumber == 2) {
  digitalWrite(OutputB, HIGH);
  lcd.print("Relay 2");
if(RelayNumber == 3) {
  digitalWrite(OutputC, HIGH);
  lcd.print("Relay 3");


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.



Latest Post

Leave a Reply

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

You are commenting using your 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