Rohan Barnwal
Published © GPL3+

Turning a Simple Home Switchboard into a Smart Touch Control

Touchscreen smart switchboard with ESP32-S3: control 4 lights locally, clear ON/OFF UI no app no cloud instant response

BeginnerShowcase (no instructions)1 hour1,640
Turning a Simple Home Switchboard into a Smart Touch Control

Things used in this project

Hardware components

ESP32S3 Box 3
×1
Grove - 4-Channel SPDT Relay
Seeed Studio Grove - 4-Channel SPDT Relay
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connection

Code

Code

Arduino
#include <Arduino.h>

#define LGFX_ESP32_S3_BOX_V3
#include <LGFX_AUTODETECT.hpp>
#include <LovyanGFX.hpp>

static LGFX lcd;

// ===== PINS =====
#define PIN_L1 10
#define PIN_L2 14
#define PIN_L3 13
#define PIN_L4 21

bool l1=false, l2=false, l3=false, l4=false;

struct Btn { int x,y,w,h; const char* label; bool* state; int pin; };
Btn buttons[] = {
  {20, 60, 130, 60, "Light 1", &l1, PIN_L1},
  {170,60, 130, 60, "Light 2", &l2, PIN_L2},
  {20, 140,130, 60, "Light 3", &l3, PIN_L3},
  {170,140,130,60, "Light 4", &l4, PIN_L4},
};

void drawButton(Btn &b){
  lcd.fillRoundRect(b.x, b.y, b.w, b.h, 14, *b.state ? TFT_GREEN : TFT_DARKGREY);
  lcd.drawRoundRect(b.x, b.y, b.w, b.h, 14, TFT_WHITE);
  lcd.setFont(&fonts::FreeSansBold9pt7b);
  lcd.setTextColor(TFT_WHITE);
  lcd.setTextDatum(middle_center);
  lcd.drawString(b.label, b.x + b.w/2, b.y + b.h/2);
}

void drawUI(){
  lcd.fillScreen(TFT_BLACK);

  lcd.setFont(&fonts::FreeSansBold12pt7b);
  lcd.setTextColor(TFT_GREEN);
  lcd.setTextDatum(middle_center);
  lcd.drawString("ESP32-S3 Smart Lights", 160, 25);

  for(auto &b : buttons) drawButton(b);
}

void applyRelayState(int pin, bool isOn){
  // 🔁 Inverted logic for active-low relay:
  // ON  -> LOW
  // OFF -> HIGH
  digitalWrite(pin, isOn ? LOW : HIGH);
}

void toggle(int i){
  *buttons[i].state = !*buttons[i].state;
  applyRelayState(buttons[i].pin, *buttons[i].state);
  drawButton(buttons[i]);
}

void setup(){
  Serial.begin(115200);

  pinMode(PIN_L1, OUTPUT);
  pinMode(PIN_L2, OUTPUT);
  pinMode(PIN_L3, OUTPUT);
  pinMode(PIN_L4, OUTPUT);

  // 🔁 Initial state: OFF (active-low relay => HIGH)
  applyRelayState(PIN_L1, false);
  applyRelayState(PIN_L2, false);
  applyRelayState(PIN_L3, false);
  applyRelayState(PIN_L4, false);

  lcd.init();
  lcd.setBrightness(255);
  drawUI();

  Serial.println("✅ ESP32-S3 4 Lights Touch UI Ready (Active-Low Relay Logic)");
}

void loop(){
  uint16_t x, y;

  if(lcd.getTouch(&x, &y)){
    for(int i=0;i<4;i++){
      Btn &b = buttons[i];
      if(x > b.x && x < b.x + b.w && y > b.y && y < b.y + b.h){
        toggle(i);
        delay(250); // debounce
        break;
      }
    }
  }
}

Credits

Rohan Barnwal
43 projects • 38 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments