Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Controlled 4-Channel Relay with Touchscreen Interface

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Controlled 4-Channel Relay with Touchscreen Interface

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

LCD TFT Screen ILI9488 HD

4-Channel Relay Module

5V Battery

Wiring Details

Arduino Mega 2560

LCD TFT Screen ILI9488 HD

4-Channel Relay Module

5V Battery

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 (