Arduino Mega 2560-Based Relay Control System with LCD Display and MP3 Player
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed using an Arduino Mega 2560 microcontroller, multiple KY-004 Key Switch Modules, LCD displays with I2C interface, an 8-channel relay module, a serial MP3 player, and a toggle switch. The circuit is programmed to control relays based on button inputs and display information on the LCDs. The MP3 player is used to play audio sequences.
Component List
Arduino Mega 2560
- Description: A microcontroller board based on the ATmega2560.
- Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL.
KY-004 Key Switch Module
- Description: A simple key switch module.
- Pins: S, Middle, -.
LCD Display with I2C Interface (lcdi2c)
- Description: A 16x2 character LCD display with I2C interface.
- Pins: GND, VCC, SDA, SCL.
8 Channel Relay JD-VCC
- Description: A relay module with 8 channels.
- Pins: JD-VCC, VCC, GND, In1-In8, IN1_NC, IN1_COM, IN1_NO, IN2_NC, IN2_COM, IN2_NO, IN3_NC, IN3_COM, IN3_NO, IN4_NC, IN4_COM, IN4_NO, IN5_NC, IN5_COM, IN5_NO, IN6_NC, IN6_COM, IN6_NO, IN7_NC, IN7_COM, IN7_NO, IN8_NC, IN8_COM, IN8_NO.
Serial MP3 Player
- Description: A module for playing MP3 files.
- Pins: RX, TX, VCC, GND.
Toggle Switch SPST
- Description: A single-pole single-throw toggle switch.
- Pins: L1, COM.
Wiring Details
Arduino Mega 2560
- 5V: Connected to VCC of all LCD displays and the Serial MP3 player.
- GND: Connected to GND of all LCD displays, Serial MP3 player, and KY-004 Key Switch Modules.
- D19/RX1: Connected to TX of the Serial MP3 player.
- D18/TX1: Connected to RX of the Serial MP3 player.
- D3 PWM: Connected to S of a KY-004 Key Switch Module.
- D5 PWM: Connected to L1 of the Toggle Switch SPST.
- D6 PWM: Connected to In8 of the 8 Channel Relay JD-VCC.
- D7 PWM: Connected to In7 of the 8 Channel Relay JD-VCC.
- D8 PWM: Connected to In6 of the 8 Channel Relay JD-VCC.
- D9 PWM: Connected to In5 of the 8 Channel Relay JD-VCC.
- D10 PWM: Connected to In4 of the 8 Channel Relay JD-VCC.
- D11 PWM: Connected to In3 of the 8 Channel Relay JD-VCC.
- D12 PWM: Connected to In2 of the 8 Channel Relay JD-VCC.
- D13 PWM: Connected to In1 of the 8 Channel Relay JD-VCC.
- SDA: Connected to SDA of all LCD displays.
- SCL: Connected to SCL of all LCD displays.
- D22-D53: Connected to S of various KY-004 Key Switch Modules.
KY-004 Key Switch Module
- S: Connected to various digital pins (D22-D53) of the Arduino Mega 2560.
- Middle: Connected to 5V of the Arduino Mega 2560.
- -: Connected to GND of the Arduino Mega 2560.
LCD Display with I2C Interface (lcdi2c)
- GND: Connected to GND of the Arduino Mega 2560.
- VCC: Connected to 5V of the Arduino Mega 2560.
- SDA: Connected to SDA of the Arduino Mega 2560.
- SCL: Connected to SCL of the Arduino Mega 2560.
8 Channel Relay JD-VCC
- In1-In8: Connected to D6-D13 of the Arduino Mega 2560.
- GND: Connected to GND of the Arduino Mega 2560.
- VCC: Connected to 5V of the Arduino Mega 2560.
Serial MP3 Player
- RX: Connected to D18/TX1 of the Arduino Mega 2560.
- TX: Connected to D19/RX1 of the Arduino Mega 2560.
- VCC: Connected to 5V of the Arduino Mega 2560.
- GND: Connected to GND of the Arduino Mega 2560.
Toggle Switch SPST
- L1: Connected to D5 PWM of the Arduino Mega 2560.
- COM: Connected to GND of the Arduino Mega 2560.
Documented Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include "SerialMP3Player.h"
#include <Chrono.h>
#define TX 19
#define RX 18
Chrono stopwatch; // Create a Chrono object
float z = 0.0;
SerialMP3Player mp3(RX,TX);
const int switchPin = 5; // Pin connected to the flick switch
int switchState = 0;
int lastSwitchState = HIGH;
bool mode = false; // false = OFF mode, true = ON mode
// Variables for managing time-based tasks in ON mode
unsigned long previousMillis = 0; // Store the last time the function was called
const long interval = 100; // Increment every 1 seconds (100ms)
const long debounceDelay = 250; // debounce time; increase if necessary
const int relay1 = 6;
const int relay2 = 7;
const int relay3 = 8;
const int relay4 = 9;
const int relay5 = 10;
const int relay6 = 11;
const int relay7 = 12;
const int relay8 = 13;
LiquidCrystal_I2C lcd1(0x27, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
LiquidCrystal_I2C lcd3(0x25, 16, 2);
LiquidCrystal_I2C lcd4(0x23, 16, 2);
// Pin definitions for buttons
const int button1_inc = 23; // Button for increasing a
const int button2_dec = 22; // Button for decreasing a
const int button3_inc = 25; // Button for increasing b
const int button4_dec = 24; // Button for decreasing b
const int button5_inc = 27; // Button for increasing c
const int button6_dec = 26; // Button for decreasing c
const int button7_inc = 29; // Button for increasing d
const int button8_dec = 28; // Button for decreasing d
const int button9_inc = 31; // Button for increasing e
const int button10_dec = 30; // Button for decreasing e
const int button11_inc = 33; // Button for increasing f
const int button12_dec = 32; // Button for decreasing f
const int button13_inc = 35; // Button for increasing g
const int button14_dec = 34; // Button for decreasing g
const int button15_inc = 37; // Button for increasing h
const int button16_dec = 36; // Button for decreasing h
const int button17_inc = 39; // Button for increasing i
const int button18_dec = 38; // Button for decreasing i
const int button19_inc = 41; // Button for increasing j
const int button20_dec = 40; // Button for decreasing j
const int button21_inc = 43; // Button for increasing k
const int button22_dec = 42; // Button for decreasing k
const int button23_inc = 45; // Button for increasing l
const int button24_dec = 44; // Button for decreasing l
const int button25_inc = 47; // Button for increasing m
const int button26_dec = 46; // Button for decreasing m
const int button27_inc = 49; // Button for increasing n
const int button28_dec = 48; // Button for decreasing n
const int button29_inc = 51; // Button for increasing o
const int button30_dec = 50; // Button for decreasing o
const int button31_inc = 53; // Button for increasing p
const int button32_dec = 52; // Button for decreasing p
const int button33