top of page
Search

How to Connect a Touch Screen to an Arduino

  • admin983369
  • Oct 6
  • 4 min read

capacitive touch screen

Integrating a touch screen with an Arduino can transform your project, allowing for sophisticated user interfaces with buttons, sliders, and graphics. The most common and beginner-friendly type for Arduino is the Resistive Touch Screen, often integrated with a TFT LCD display.


This guide will focus on connecting a popular and widely available module: the ILI9341 TFT LCD with a Resistive Touch Screen.


What You Will Need

  1. An Arduino Board: (Uno, Mega, Nano, etc.)

  2. TFT LCD with Touch Screen: ILI9341 driver-based screen (e.g., a 2.8" or 3.2" module).

  3. Breadboard and Jumper Wires: A lot of them!

  4. Library: You will need two key Arduino libraries:

    • Adafruit_ILI9341: For controlling the display.

    • Adafruit_STMPE610: For reading the touch input.

    • (These can be installed via the Arduino IDE's Library Manager).


Step 1: Understanding the Connections

These displays have two separate controllers:

  1. The LCD Controller (ILI9341): Handles the pixels on the screen.

  2. The Touch Controller (STMPE610): Measures the touch position.

They communicate via SPI (Serial Peripheral Interface), a fast serial protocol. This allows us to use only a few digital pins.


Pinout of a Typical ILI9341 Module:

  • VCC: 5V or 3.3V (Check your module's datasheet!)

  • GND: Ground

  • CS (TFT): Chip Select for the Display

  • RST: Reset for the Display

  • DC (or D/C): Data/Command selector for the Display

  • MOSI: Master-Out-Slave-In (Data line from Arduino to screen)

  • SCK: Serial Clock

  • MISO: Master-In-Slave-Out (Data line from screen to Arduino) - Crucial for Touch!

  • CS (Touch): Chip Select for the Touch Controller


Step 2: Wiring the Circuit

The following table shows a standard wiring for an Arduino Uno. The SPI pins (MOSI, MISO, SCK) are fixed on the Uno, but you can choose other pins for CS, DC, and RST.

ILI9341 Module Pin

Arduino Uno Pin

Description

VCC

5V

Power

GND

GND

Ground

CS (TFT)

Digital Pin 10

Display Chip Select

RST

Digital Pin 9

Display Reset

DC (D/C)

Digital Pin 8

Display Data/Command

MOSI

Digital Pin 11 (ICSP-4)

SPI Data Line

SCK

Digital Pin 13 (ICSP-3)

SPI Clock

MISO

Digital Pin 12 (ICSP-1)

SPI Data Line (for touch)

CS (Touch)

Digital Pin 7

Touch Chip Select

Important Notes:

  • Arduino Mega: Use Digital Pin 51 (MOSI), 50 (MISO), and 52 (SCK).

  • 3.3V Logic: Some modules are 3.3V logic only. If yours is, you must power it from the Arduino's 3.3V pin and use a logic level converter for the data lines to avoid damage. Most common modules are 5V tolerant, but always check.


Step 3: Installing the Libraries

  1. Open the Arduino IDE.

  2. Go to Sketch > Include Library > Manage Libraries....

  3. Search for "Adafruit ILI9341" and install it.

  4. Search for "Adafruit STMPE610" and install it.

  5. The library manager will also install other required dependencies like Adafruit_GFX.


Step 4: The Code - A Basic Touch Point Reader

This simple sketch will display a welcome message and print the raw (X, Y) touch coordinates to the Serial Monitor when you press the screen.

cpp

// Include the necessary libraries

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_ILI9341.h>

#include <Adafruit_STMPE610.h>


// Define the pins we used for wiring

#define TFT_CS   10

#define TFT_DC    8

#define TFT_RST   9

#define STMPE_CS  7


// Initialize the display and touch screen objects

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);


void setup() {

  Serial.begin(9600);


  // Initialize the display

  tft.begin();

  tft.setRotation(3); // Rotates the display (0-3). Experiment with this.


  // Initialize the touch screen

  if (!ts.begin()) {

    Serial.println("STMPE610 not found!");

    while (1); // Halt if touch controller isn't detected

  }


  Serial.println("Touch Screen Test");


  // Display a background

  tft.fillScreen(ILI9341_BLACK);

  tft.setTextColor(ILI9341_WHITE);

  tft.setTextSize(2);

  tft.setCursor(20, 20);

  tft.println("Touch the Screen!");

}


void loop() {

  // Check if the screen is being touched

  if (ts.touched()) {

    // Read the raw touch data

    TS_Point p = ts.getPoint();


    // Print the raw coordinates to Serial Monitor

    Serial.print("X = "); Serial.print(p.x);

    Serial.print("\tY = "); Serial.print(p.y);

    Serial.print("\tPressure = "); Serial.println(p.z);


    // Draw a small circle where the touch was detected

    // Note: Raw coordinates need to be mapped to screen pixels.

    // This is a simple, direct mapping. You may need to adjust it.

    int screenX = map(p.x, 150, 3800, 0, tft.width());  // Adjust min/max X based on your readings

    int screenY = map(p.y, 150, 3900, 0, tft.height()); // Adjust min/max Y based on your readings


    if (screenX >= 0 && screenX < tft.width() && screenY >= 0 && screenY < tft.height()) {

      tft.fillCircle(screenX, screenY, 5, ILI9341_RED);

    }


    // A small delay to debounce the touch

    delay(100);

  }

}


Step 5: Calibration and Mapping

The most crucial part is mapping the raw touch values to actual screen pixels.

  1. Upload the code and open the Serial Monitor.

  2. Touch the four corners of the screen and note the raw X and Y values.

  3. In the map() function in the code, replace the values (150, 3800) and (150, 3900) with the minimum and maximum raw values you read from your specific screen.

A more robust solution is to create a proper calibration function, but adjusting the map() function is a good start.


Troubleshooting Common Issues

  • "STMPE610 not found!": Double-check your wiring, especially for the touch CS pin. Ensure the STMPE_CS define in the code matches your physical connection.

  • Blank White Screen: Check the display's RST pin connection and ensure the TFT_CS and TFT_DC pins are correct.

  • Garbage on Screen: This is often a speed issue. Ensure you are using the hardware SPI pins (11,12,13 on Uno) and not other digital pins for MOSI/MISO/SCK.

  • Incorrect Touch Coordinates: You must calibrate the map() function for your specific screen. Raw values vary between modules.

By following these steps, you can successfully connect a touch screen to your Arduino and begin creating interactive projects. The next step is to use these coordinates to detect "buttons" on the screen by checking if the touch point is within a specific rectangular area.


 
 
bottom of page