In this project, we will use the TCRT5000 infrared (IR) reflective sensor (labeled HW-006 v1.3 on the board). Instead of classic buttons, we will use a hand wave to toggle the display output.
Connecting components to the Arduino Nano ESP32:
The code below allows switching between the text "STATUS: OK" and the warning "DETECTED!" when the sensor detects proximity.
// Project: Touchless Switch (TCRT5000 + TFT 2.0 SPI)
// Author: Borut Bukovnik - vadnica.org
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
// Pins for TFT display
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
#define IR_PIN 4 // Pin for IR sensor
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
void setup() {
tft.init(240, 320);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
pinMode(IR_PIN, INPUT); // Set sensor as input
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 20);
tft.print("Proximity Sensor Active");
}
void loop() {
// Read state (LOW means proximity detected)
int state = digitalRead(IR_PIN);
tft.setCursor(50, 110);
tft.setTextSize(4);
if (state == LOW) {
// Obstacle detected
tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
tft.print(" DETECTED! ");
} else {
// Path clear
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
tft.print(" STATUS: OK ");
}
delay(100); // Fast responsiveness
}