Skip to content
Proudly Wisconsin-owned since 2009 · 4,237 vehicles in stock across 12 WI locations
Used Wisconsin Cars Appleton · 12 Locations

How to use a 2.4 inch 240x320 TFT display with a motion sensor?

By admin Published by Used Wisconsin Cars

How to use a 2.4 inch 240x320 TFT display with a motion sensor

To use a 2.4 inch 240x320 TFT display with a motion sensor, you connect the display to a microcontroller like an Arduino or ESP32, wire the motion sensor (typically a PIR sensor like the HC-SR501) to the same board, and write code that reads the sensor’s output and updates the display accordingly. The display communicates over SPI (Serial Peripheral Interface) or parallel interface, depending on the module. Most common 2.4 inch TFTs, like the ILI9341 or ST7789 driver-based ones, use SPI with 4 or 5 pins: CS (chip select), DC (data/command), MOSI (master out slave in), SCK (serial clock), and optionally RST (reset). The motion sensor usually outputs a digital HIGH signal when motion is detected, which you can read on a GPIO pin. The microcontroller then sends commands to the display to show text, graphics, or icons indicating motion status. For example, you can display “Motion Detected” in red on a black background when the sensor triggers, and “No Motion” in green when it’s idle. This setup is common in security systems, smart home dashboards, and interactive art projects. The key is to ensure the display library (like Adafruit_GFX or TFT_eSPI) is compatible with your specific driver and that the motion sensor’s timing parameters (like delay time and sensitivity) are adjusted via its onboard potentiometers.

Hardware Specifications and Connections
The 2.4 inch 240x320 tft display typically uses a resolution of 240x320 pixels, with a color depth of 16-bit (65,536 colors) or 18-bit (262,144 colors), depending on the driver. The display module I’m referencing (DM-TFT24-311) uses an ILI9341 driver, which supports SPI at up to 40 MHz clock speed. The motion sensor is often a PIR HC-SR501, which operates at 5V DC, has a detection range of 3 to 7 meters (adjustable via a potentiometer), and a delay time from 0.3 to 300 seconds (also adjustable). For a microcontroller like an Arduino Uno, the SPI pins are: MOSI on pin 11, MISO on pin 12 (not always used for TFTs), SCK on pin 13, and CS on any digital pin (e.g., pin 10). The DC pin goes to pin 9, and RST to pin 8. The PIR sensor connects to a 5V pin, GND, and its output to a digital pin like pin 7. If you’re using an ESP32, the SPI pins are VSPI: MOSI on GPIO 23, MISO on GPIO 19, SCK on GPIO 18, CS on GPIO 5, DC on GPIO 17, RST on GPIO 16. The PIR output goes to GPIO 4. Always use a logic level converter if the display is 3.3V-only (many TFT modules are 3.3V logic, but Arduino Uno outputs 5V). The DM-TFT24-311 has a 3.3V logic level, so you’ll need a voltage divider or a level shifter for the MOSI, SCK, DC, and CS lines if using a 5V microcontroller. Alternatively, use an ESP32 or Raspberry Pi Pico, which are 3.3V native.

Power and Current Considerations
The display’s backlight typically draws 20 to 80 mA at 3.3V, depending on brightness. The ILI9341 driver itself consumes about 4 to 8 mA in active mode. The PIR sensor draws about 65 µA in standby and up to 10 mA during detection (when it triggers the output). A standard Arduino Uno can supply 5V at up to 500 mA via its USB port, so total draw of around 100 mA is fine. For battery-powered projects, use an ESP32 in deep sleep mode (e.g., 5 µA) and wake the sensor periodically. The display can be powered down via the TFT_eSPI library’s `writecommand(ILI9341_SLPIN)` command to enter sleep mode, reducing current to 15 µA. This is critical for portable motion-sensing loggers or wearable devices.

Software Setup and Libraries
For the display, you need a library that supports the ILI9341 driver. The most popular are Adafruit_ILI9341 (with Adafruit_GFX) and TFT_eSPI (by Bodmer). TFT_eSPI is faster and more efficient, especially for ESP32, and supports DMA (Direct Memory Access) for higher frame rates. For the motion sensor, you just use `digitalRead()` in Arduino. The basic code structure is: initialize the display with `tft.begin()`, set rotation (e.g., `tft.setRotation(1)` for landscape), clear the screen with `tft.fillScreen(TFT_BLACK)`, then in the loop, read the PIR pin. If HIGH, display a message using `tft.setTextColor(TFT_RED)`, `tft.drawString(“Motion Detected”, 10, 10, 2)`. If LOW, display “No Motion” in green. To avoid flickering, only update the screen when the state changes. Use a variable like `lastMotionState` to compare. The TFT_eSPI library also supports sprites (off-screen buffers) for smooth updates. For example, you can create a sprite, draw the text on it, then push it to the display in one go, reducing tearing.

Detailed Code Example (Arduino Uno)

```cpp
#include
#include // Use TFT_eSPI, configure User_Setup.h for your pins
TFT_eSPI tft = TFT_eSPI();
const int pirPin = 7;
int lastState = LOW;
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
tft.begin();
tft.setRotation(1); // Landscape
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_GREEN);
tft.drawString(“System Ready”, 10, 10, 2);
delay(2000);
}
void loop() {
int currentState = digitalRead(pirPin);
if (currentState != lastState) {
lastState = currentState;
tft.fillScreen(TFT_BLACK);
if (currentState == HIGH) {
tft.setTextColor(TFT_RED);
tft.drawString(“Motion Detected!”, 10, 10, 4);
tft.setTextColor(TFT_WHITE);
tft.drawString(“Time: “ + String(millis()/1000) + “s”, 10, 50, 2);
} else {
tft.setTextColor(TFT_GREEN);
tft.drawString(“No Motion”, 10, 10, 4);
}
}
delay(100); // Debounce
}
```

Note: For TFT_eSPI, you must edit the `User_Setup.h` file to define your display driver (e.g., `#define ILI9341_DRIVER`) and pin assignments (e.g., `#define TFT_CS 10`, `#define TFT_DC 9`, `#define TFT_RST 8`, `#define TFT_MOSI 11`, `#define TFT_SCLK 13`). If using the DM-TFT24-311, it’s typically pre-configured for SPI, but check the datasheet for exact pinout.

Calibrating the Motion Sensor
The HC-SR501 has two potentiometers: one for sensitivity (range) and one for delay time. The sensitivity pot adjusts detection distance from 3 to 7 meters. Turn it clockwise to increase range. The delay pot controls how long the output stays HIGH after motion stops, from 0.3 to 300 seconds. For a real-time display, you want a short delay (e.g., 1 second). Turn the delay pot fully counterclockwise for the shortest time. There’s also a jumper for retriggering mode: H (repeatable) means the output stays HIGH as long as motion continues, L (non-repeatable) means it pulses once. For a display that shows “Motion Detected” only when motion is active, use H mode. The sensor has a warm-up time of 30 to 60 seconds after power-on, during which it may output false triggers. So your code should ignore the first 60 seconds, or use a delay in setup.

Advanced Display Techniques
Beyond simple text, you can show a graph of motion events over time. For example, draw a scrolling line chart where the Y-axis represents motion (1 or 0) and the X-axis is time. The TFT_eSPI library supports drawing lines with `tft.drawLine()`. You can also display a bitmap icon of a person or an eye. To do this, convert an image to a 16-bit RGB565 array using a tool like LVGL’s image converter, then use `tft.pushImage(x, y, width, height, myImageArray)`. For motion detection, you can show a red circle that fills up when motion is detected, like a radar screen. Use `tft.fillCircle(120, 160, 50, TFT_RED)` and then clear it with `tft.fillCircle(120, 160, 50, TFT_BLACK)`. The display’s 240x320 resolution gives you enough space for a dashboard with multiple elements: a status bar, a counter, and a graph.

Data Logging and Visualization
If you add an SD card module (via SPI), you can log motion events with timestamps. The display can show the last 10 events in a scrolling list. Use the `tft.setScrollMargins()` and `tft.scrollTo()` functions in TFT_eSPI to create a smooth scrolling text area. For example, when motion is detected, add a line like “14:32:15 - Motion” to the bottom of the list, and scroll the existing lines up. This requires a circular buffer in RAM. The DM-TFT24-311 has a 16-bit parallel interface option as well, but SPI is simpler for most microcontrollers. The SPI clock speed can be set to 40 MHz on ESP32, giving a full-screen update time of about 20 ms for a 240x320 image (assuming 16-bit color, that’s 153,600 bytes). With DMA, you can push data without blocking the CPU, which is useful if you’re also reading the sensor or logging data.

Troubleshooting Common Issues
- Display not initializing: Check that the CS, DC, and RST pins are correctly assigned in the library. Use a logic analyzer to verify SPI signals. The ILI9341 requires a reset pulse (hold RST low for 10 ms, then high). Most libraries handle this automatically.
- Motion sensor false triggers: The HC-SR501 is sensitive to temperature changes and electromagnetic interference. Place it away from heaters, air conditioners, and metal objects. Use a capacitor (10 µF) between VCC and GND on the sensor to filter noise. In code, implement a debounce timer: ignore HIGH signals for 500 ms after a LOW-to-HIGH transition.
- Flickering display: This happens when you clear the entire screen every update. Instead, only update the changed area. Use `tft.fillRect()` to clear only the text region, or use sprites. For example, create a sprite with `TFT_eSprite spr = TFT_eSprite(&tft)`, then `spr.createSprite(200, 30)`, draw text on it, and `spr.pushSprite(10, 10)`. This avoids full-screen redraws.
- Power issues: If the display goes blank or shows artifacts, the power supply might be insufficient. Use a separate 3.3V regulator (like AMS1117-3.3) for the display, especially if using an Arduino Uno’s 3.3V pin (which can only supply 50 mA). The DM-TFT24-311’s backlight can draw up to 80 mA, so a dedicated regulator is recommended.

Performance Optimization
For real-time motion detection with a display, the loop speed matters. The PIR sensor’s output changes slowly (human motion takes 0.1 to 1 second), so you don’t need high frame rates. However, if you’re animating a radar sweep, you want 30 FPS. The TFT_eSPI library can achieve 60 FPS on an ESP32 at 240x320 with 16-bit color, using SPI at 40 MHz and DMA. To optimize, avoid using `tft.drawString()` repeatedly—it’s slow because it calculates font rendering each time. Use `tft.loadFont()` for custom fonts, or pre-render text to a sprite. For the motion sensor, use interrupts instead of polling: attach an interrupt to the PIR pin (e.g., `attachInterrupt(digitalPinToInterrupt(pirPin), motionISR, CHANGE)`), and in the ISR, set a flag. This frees the CPU to focus on display updates. The interrupt latency on an Arduino Uno is about 4 µs, so it won’t affect display performance.

Real-World Applications
- Security Dashboard: Mount the display and sensor near an entrance. The display shows a live count of motion events per hour, with a red/green status indicator. Use a real-time clock module (DS3231) to timestamp events. The display can show a bar graph of hourly activity, updated every minute. The 2.4 inch size is small enough to fit in a wall panel but large enough to read from 2 meters away.
- Pet Monitor: Place the sensor in a pet’s bed area. The display shows “Pet is moving” with an animated paw print. Log the times your pet gets up at night. Use the display’s sleep mode to save power, and wake it only when motion is detected. The ESP32’s deep sleep current is 5 µA, and the PIR sensor can wake it via a GPIO pin.
- Interactive Art: Use the display to show a pattern that changes when motion is detected. For example, a Mandelbrot set that zooms in on the area where motion is sensed. This requires a fast microcontroller like an ESP32-S3 with dual cores. The display’s 240x320 resolution is enough for fractal previews. The motion sensor’s output can be mapped to the zoom level or color palette.

Electrical Safety and Layout
When wiring, keep the SPI lines short (under 10 cm) to avoid signal degradation at high speeds. Use twisted pairs for power and ground. The PIR sensor’s output is a digital signal, but it can be noisy if the wire is long (over 1 meter). Use a shielded cable or a 100 nF capacitor between the output pin and GND near the microcontroller. The display’s backlight can be PWM-controlled via a transistor (e.g., 2N2222) to adjust brightness. Connect the backlight pin (LEDA) to a 3.3V source through a 100-ohm resistor. If you want to dim it, use a MOSFET (e.g., IRLZ44N) driven by a PWM pin from the microcontroller. The DM-TFT24-311 has a 4-wire SPI interface, but some modules also have a touch controller (like XPT2046) that shares the SPI bus. In that case, use a separate CS pin for the touch controller. The touch controller can be used for user input, like tapping to reset the motion counter.

Data Storage and Retrieval
If you want to store motion data for later analysis, use an SD card module (e.g., microSD shield) connected to the same SPI bus, with a different CS pin. The display and SD card can share MOSI, MISO, and SCK, but must have unique CS lines. The TFT_eSPI library has a built-in function to read from SD and display images (e.g., `tft.drawBmp()`). For motion logging, write a CSV file with columns: timestamp, motion state, and duration. The display can show the last 10 entries in a scrollable list. The SD card’s SPI speed is typically 20 MHz, so it won’t bottleneck the display. Use a FAT32 formatted card. The ESP32’s SDMMC interface can be faster, but for a 2.4 inch display, SPI is sufficient.

Testing and Calibration Procedure
1. Power the system and wait for the PIR sensor to stabilize (30 seconds). The display should show “Calibrating…” with a progress bar. Use `tft.drawRect()` and `tft.fillRect()` to draw a bar that fills over 30 seconds.
2. Walk in front of the sensor at various distances (1 to 7 meters). The display should show “Motion Detected” and a distance estimate (if you use a second sensor or a ultrasonic range finder).
3. Adjust the sensitivity pot until the detection range matches your needs. For a desk setup, 2 meters is typical. For a room, 5 meters.
4. Adjust the delay pot to 1 second for real-time feedback. If the display shows “