#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Define pins for the display
#define TFT_CS 15
#define TFT_DC 14
#define TFT_RST 13
#define TFT_BLK 12 // Backlight control pin
// Define pin for the motion detector
#define MOTION_PIN 4
// Initialize ILI9341 display
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setup() {
// Initialize the display
display.begin();
// Turn on the backlight
pinMode(TFT_BLK, OUTPUT);
digitalWrite(TFT_BLK, HIGH);
// Clear the screen with black color
display.fillScreen(ILI9341_BLACK);
// Initialize the motion detector pin
pinMode(MOTION_PIN, INPUT);
// Display initial message
display.setCursor(10, 10);
display.setTextColor(ILI9341_WHITE);
display.setTextSize(2);
display.print("Waiting for motion...");
}
void loop() {
// Check for motion
if (digitalRead(MOTION_PIN) == HIGH) {
// Clear the screen
display.fillScreen(ILI9341_BLACK);
// Display motion detected message
display.setCursor(10, 10);
display.setTextColor(ILI9341_RED);
display.setTextSize(2);
display.print("Motion Detected!");
// Wait for a while to avoid multiple triggers
delay(2000);
// Clear the screen again
display.fillScreen(ILI9341_BLACK);
// Display waiting message
display.setCursor(10, 10);
display.setTextColor(ILI9341_WHITE);
display.setTextSize(2);
display.print("Waiting for motion...");
}
}
Loading
pi-pico-w
pi-pico-w