# Touchscreen control for relays

**URL:** <https://forum.arduino.cc/t/touchscreen-control-for-relays/1461685>\
**Category:** General Guidance\
**Created:** [October 1, 2026, 3:52am UTC](https://forum.arduino.cc/t/touchscreen-control-for-relays/1461685 "2026-10-01T03:52:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arduinorocks15](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@arduinorocks15](https://forum.arduino.cc/u/arduinorocks15)\
**Post date:** [October 1, 2026, 3:52am UTC](https://forum.arduino.cc/t/touchscreen-control-for-relays/1461685/1 "2026-10-01T03:52:13Z")

</div>

Hi, I am building a light bar controller for my truck. I have the code working mostly the way I want. The relay functions when the set area of the touch screen is pressed, but there is no color response on the screen. I am looking to have it just show the color for about half a second then set the screen back to black so it keeps it from being blinding at night. Any help would be much appreciated. Thanks in advance!

Here is the complete code:

```arduino
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>
#include "TouchScreen.h"

#if defined( __SAM3X8E__ )
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

int Relay1 = 22;
int Relay2 = 23;
int Relay3 = 24;
int Relay4 = 25;
int i = 0;

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define BOXSIZE 40
#define PENRADIUS 3

bool relay1State[4] = {false};
int touch = 0;

void setup() {

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);

  Serial.begin(9600);
  tft.reset();
  tft.begin(0x8357);
  tft.setTextColor(RED);
  tft.setTextSize(3);
  tft.setRotation(2);
  tft.fillScreen(BLACK);
  tft.fillRect(0, 0, 120, 120, BLACK);
  tft.fillRect(120, 0, 120, 120, BLACK);

}

void loop() {

  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
    p = ts.getPoint();
    if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
    if (p.x > 0 && p.x < 120) {
      if (p.y > 0 && p.y < 120) {
        touch = 1;
      }
    }
    if (p.x > 120 && p.x < 240) {
      if (p.y > 0 && p.y < 120) { 
        touch = 2;
      }
    }

// Causes the first relay to change state when the set area on the screen is pressed
  if (touch == 1) {
    relay1State[i] = !relay1State[i];
    digitalWrite(Relay1 + i, relay1State[i] ? HIGH : LOW);
    touch = 0;
    tft.fillRect(0, 0, 120, 120, GREEN);
    delay(500);

  }
else {
  tft.fillRect(0, 0, 120, 120, BLACK);

}
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [October 1, 2026, 4:29am UTC](https://forum.arduino.cc/t/touchscreen-control-for-relays/1461685/2 "2026-10-01T04:29:20Z")

</div>

> [@arduinorocks15](#):
>
> ```arduino
> pinMode(XM, OUTPUT);
> pinMode(YP, OUTPUT);
> 
> ```

These should be with the other `pinMode()` commands in `setup()`

---

<div class="post-metadata">

**Author:** ![arduinorocks15](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@arduinorocks15](https://forum.arduino.cc/u/arduinorocks15)\
**Post date:** [October 1, 2026, 4:33am UTC](https://forum.arduino.cc/t/touchscreen-control-for-relays/1461685/3 "2026-10-01T04:33:43Z")

</div>

Thanks.
