Arduino Mega 2560 Controlled 4-Channel Relay with Touchscreen Interface
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit involves an Arduino Mega 2560 microcontroller, an ILI9488 TFT LCD screen, a 4-channel relay module, and a 5V battery. The Arduino Mega 2560 controls the LCD screen and the relay module. The LCD screen displays four buttons, each controlling one of the four relays. Pressing a button toggles the corresponding relay on or off.
Component List
Arduino Mega 2560
- Description: A microcontroller board based on the ATmega2560.
- Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D21/SCL, D20/SDA, D19/RX1, D18/TX1, D17 PWM/RX2, D16 PWM/TX2, D15/RX3, D14/TX3, D0 RX0, D1 TX0, D2 PWM, D3 PWM, D4 PWM, D5 PWM, D6 PWM, D7 PWM, D8 PWM, D9 PWM, D10 PWM, D11 PWM, D12 PWM, D13 PWM, AREF, SDA, SCL, D52, D50, D48, D46, D44, D42, D40, D38, D36, D34, D32, D30, D28, D26, D24, D22, D53, D51, D49, D47, D45, D43, D41, D39, D37, D35, D33, D31, D29, D27, D25, D23.
LCD TFT Screen ILI9488 HD
- Description: A 3.5-inch TFT LCD display module with a resolution of 480x320 pixels.
- Pins: PEN, TDO, TDI, VCC (3.3~5V), GND, CS, RST, D/C, SDI(MOSI), SCK, BL, SDO(MISO), TCS, TCK.
4-Channel Relay Module
- Description: A module with four relays, each capable of switching up to 10A at 250VAC or 30VDC.
- Pins: N.O. 4, COM 4, N.C. 4, N.O. 3, COM 3, N.C. 3, N.O. 2, COM 2, N.C. 2, N.O. 1, COM 1, N.C. 1, Pin 13, Pin 14, Pin 15, Pin 16, Pin 17, Pin 18, Pin 19, Pin 20, Pin 21, Pin 22, Pin 23, Pin 24, VCC+, VCC- (GND), IN 1, IN 2, IN 3, IN 4.
5V Battery
- Description: A power source providing 5V.
- Pins: Positive, Negative.
Wiring Details
Arduino Mega 2560
- 5V: Connected to VCC+ of the 4-channel relay module, VCC (3.3~5V) of the LCD TFT screen, and the positive terminal of the 5V battery.
- GND: Connected to VCC- (GND) of the 4-channel relay module, GND of the LCD TFT screen, and the negative terminal of the 5V battery.
- D2 PWM: Connected to IN 1 of the 4-channel relay module.
- D3 PWM: Connected to IN 2 of the 4-channel relay module.
- D4 PWM: Connected to IN 3 of the 4-channel relay module.
- D5 PWM: Connected to IN 4 of the 4-channel relay module.
- D7 PWM: Connected to BL of the LCD TFT screen.
- D8 PWM: Connected to D/C of the LCD TFT screen.
- D9 PWM: Connected to RST of the LCD TFT screen.
- D10 PWM: Connected to CS of the LCD TFT screen.
- D52: Connected to SCK of the LCD TFT screen.
- D50: Connected to SDO(MISO) of the LCD TFT screen.
- D51: Connected to SDI(MOSI) of the LCD TFT screen.
LCD TFT Screen ILI9488 HD
- VCC (3.3~5V): Connected to 5V of the Arduino Mega 2560 and the positive terminal of the 5V battery.
- GND: Connected to GND of the Arduino Mega 2560 and the negative terminal of the 5V battery.
- BL: Connected to D7 PWM of the Arduino Mega 2560.
- D/C: Connected to D8 PWM of the Arduino Mega 2560.
- RST: Connected to D9 PWM of the Arduino Mega 2560.
- CS: Connected to D10 PWM of the Arduino Mega 2560.
- SCK: Connected to D52 of the Arduino Mega 2560.
- SDO(MISO): Connected to D50 of the Arduino Mega 2560.
- SDI(MOSI): Connected to D51 of the Arduino Mega 2560.
4-Channel Relay Module
- VCC+: Connected to 5V of the Arduino Mega 2560 and the positive terminal of the 5V battery.
- VCC- (GND): Connected to GND of the Arduino Mega 2560 and the negative terminal of the 5V battery.
- IN 1: Connected to D2 PWM of the Arduino Mega 2560.
- IN 2: Connected to D3 PWM of the Arduino Mega 2560.
- IN 3: Connected to D4 PWM of the Arduino Mega 2560.
- IN 4: Connected to D5 PWM of the Arduino Mega 2560.
5V Battery
- Positive: Connected to 5V of the Arduino Mega 2560, VCC+ of the 4-channel relay module, and VCC (3.3~5V) of the LCD TFT screen.
- Negative: Connected to GND of the Arduino Mega 2560, VCC- (GND) of the 4-channel relay module, and GND of the LCD TFT screen.
Code Documentation
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
/*
* This Arduino Sketch sets up an ILI9488 TFT LCD display, a Mega 2560 board,
* and a 4-channel relay module. The code displays four buttons on the LCD,
* each controlling one of the four relays. Pressing a button toggles the
* corresponding relay on or off.
*/
// Pin definitions
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_MOSI 51
#define TFT_CLK 52
#define TFT_MISO 50
#define TFT_BL 7
#define RELAY1 2
#define RELAY2 3
#define RELAY3 4
#define RELAY4 5
// Touch screen pins
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
// Touch screen calibration values
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 940
#define TS_MAXY 920
// Create display and touch screen objects
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// Button coordinates
#define BUTTON_X 60
#define BUTTON_Y 40
#define BUTTON_W 120
#define BUTTON_H 60
#define BUTTON_SPACING_X 60
#define BUTTON_SPACING_Y 80
// Button states
bool relayState[4] = {false, false, false, false};
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the display
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
// Initialize relay pins
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Draw initial buttons
drawButtons();
}
void loop() {
// Check for touch
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
// Map touch coordinates
int x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
int y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
// Check which button was pressed
for (int i = 0; i < 4; i++) {
if (