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

Arduino Mega 2560 Controlled TFT Touchscreen Interface with 4-Channel Relay Automation

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

Circuit Documentation

Summary

This circuit integrates an Arduino Mega 2560 microcontroller with an LCD TFT screen (ILI9488 HD), a 4-channel relay module, and a 5V power supply (battery). The Arduino Mega 2560 controls the LCD display to show interactive buttons and manages the state of the relays based on user input on the touchscreen. The relays can be toggled on and off to control external devices or circuits. The 5V battery provides power to the entire circuit.

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

Documented Code

#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 (x > BUTTON_X && x < BUTTON_X + BUTTON_W &&
          y > BUTTON_Y + i * BUTTON_SPACING_Y &&
          y < BUTTON_Y + i * BUTTON_SPACING_Y + BUTTON_H) {
        // Toggle relay state
        relayState[i] = !relayState[i];
        digitalWrite(RELAY1 + i, relayState[i] ? HIGH : LOW);
        // Redraw button
        drawButton(i);
      }
    }
  }
}

void drawButtons() {
  for (int i = 0; i < 4; i++) {
    drawButton(i);
  }
}

void drawButton(int i) {
  tft.fillRect(BUTTON_X, BUTTON_Y + i * BUTTON_SPACING_Y, BUTTON_W, BUTTON_H,
               relayState[i] ? ILI9341_GREEN : ILI9341_RED);
  tft.drawRect(BUTTON_X, BUTTON_Y + i * BUTTON_SPACING_Y, BUTTON_W, BUTTON_H,
               ILI9341_WHITE);
  tft.setCursor(BUTTON_X + 10, BUTTON_Y + i * BUTTON_SPACING_Y + 20);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.print("Relay ");
  tft.print(i + 1);
}

The code provided is an Arduino sketch that initializes the ILI9488 TFT LCD display and the 4-channel relay module. It sets up a simple graphical user interface with buttons on the display that, when pressed, toggle the state of the corresponding relay. The touch screen is used to detect button presses, and the relay states are updated accordingly. The code includes functions for drawing the buttons on the display and handling the touch events.