Built-in project · Arduino Uno R3

Cap touch on an Arduino Uno

Draw on the 2.8 inch ILI9341 with its FT6206 capacitive touch panel, read with Adafruit’s library over I2C, and turn the film’s point round to match the screen. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $45. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • SPI for a display, I2C for a touch panel
  • Capacitive touch panels reporting pixel positions
  • Flipping coordinates with map()
  • Drawing shapes with the Adafruit GFX library
  • Why libraries hide long chip protocols
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The 2.8 inch screen is drawn over SPI: pin 13 is the clock, pin 11 the data, and pins 10, 9 and 8 are chip select, data/command and reset. The touch layer is a separate chip, the FT6206, and it talks over I2C on A4 and A5. Two different buses, so they never fight over a wire.

Once running, the sketch waits for a finger. When the panel reports a touch, the sketch reads the point, flips it to match the screen, and draws a small yellow dot there. Drag your finger and the dots trace a line. Each point is also printed to the serial monitor.

Parts list

3 parts, plus the jumper wires. They retail for about $45; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Touch TFT, 2.8 inch ILI9341 with FT6206 pin SCKArduino Uno R3 pin 13
Touch TFT, 2.8 inch ILI9341 with FT6206 pin MOSIArduino Uno R3 pin 11
Touch TFT, 2.8 inch ILI9341 with FT6206 pin CSArduino Uno R3 pin 10
Touch TFT, 2.8 inch ILI9341 with FT6206 pin DCArduino Uno R3 pin 9
Touch TFT, 2.8 inch ILI9341 with FT6206 pin RSTArduino Uno R3 pin 8
Touch TFT, 2.8 inch ILI9341 with FT6206 pin VCCArduino Uno R3 pin 5V
Touch TFT, 2.8 inch ILI9341 with FT6206 pin LEDArduino Uno R3 pin 5V
Touch TFT, 2.8 inch ILI9341 with FT6206 pin GNDArduino Uno R3 pin GND
Touch TFT, 2.8 inch ILI9341 with FT6206 pin SDAArduino Uno R3 pin A4
Touch TFT, 2.8 inch ILI9341 with FT6206 pin SCLArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Cap touch: draw on the 2.8 inch ILI9341 with its FT6206 capacitive touch
// panel, the Adafruit way.
//
//   display   CS -> 10   DC -> 9   RST -> 8   MOSI -> 11   SCK -> 13   LED -> 5 V
//   touch     SCL -> A5   SDA -> A4
//
// Press on the picture while it runs, and drag.
//
// The display is an SPI device and the touch controller an I2C one, so the
// two never share a wire. The FT6206 reports the point in pixels of the
// glass, but its film is laminated the other way up from the display's
// memory, so the first thing the sketch does with a point is turn it round:
// the two map() lines, straight from Adafruit's example.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>

Adafruit_ILI9341 tft(10, 9, 8);
Adafruit_FT6206 ctp;

void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
  if (!ctp.begin(40)) {
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1) {
    }
  }
  Serial.println("Capacitive touchscreen started");
}

void loop() {
  if (!ctp.touched()) return;
  TS_Point p = ctp.getPoint();
  // Flip it around to match the screen.
  p.x = map(p.x, 0, 240, 240, 0);
  p.y = map(p.y, 0, 320, 320, 0);
  tft.fillCircle(p.x, p.y, 3, ILI9341_YELLOW);
  Serial.print("touch ");
  Serial.print(p.x);
  Serial.print(" ");
  Serial.println(p.y);
  delay(50);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the serial port, starts the display with tft.begin() and fills it black. Then it starts the touch chip with ctp.begin(40). If that fails, it prints a message and stops forever in a while (1) loop, so you know the touch wiring is the problem.

void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
  if (!ctp.begin(40)) {
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1) {
    }
  }
  Serial.println("Capacitive touchscreen started");
}

loop sketch.ino

Does nothing until ctp.touched() is true. Then it gets the point and flips both axes with map(), because the touch film is mounted the other way up from the display memory. It draws a yellow circle of radius 3 there, prints the position, and waits 50 ms.

void loop() {
  if (!ctp.touched()) return;
  TS_Point p = ctp.getPoint();
  // Flip it around to match the screen.
  p.x = map(p.x, 0, 240, 240, 0);
  p.y = map(p.y, 0, 320, 320, 0);
  tft.fillCircle(p.x, p.y, 3, ILI9341_YELLOW);
  Serial.print("touch ");
  Serial.print(p.x);
  Serial.print(" ");
  Serial.println(p.y);
  delay(50);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The screen stays blank or white This project
    Check CS on pin 10, DC on pin 9 and RST on pin 8. The LED pin also needs 5V for the backlight.
  • The serial monitor says the touchscreen controller could not start This project
    Check SDA on A4 and SCL on A5, and that VCC and GND are connected.
  • Dots appear mirrored from where you touch This project
    The two map() lines flip the axes. Both are needed for this panel.
  • The screen stays white or blank. Touch TFT
    It is a 3.3 V display. Wire SPI properly: CS, DC, MOSI, SCK and MISO, with RESET and the backlight (LED) powered. Initialize the driver in code, for example with Adafruit_ILI9341.
  • The touch does not respond or is not found. Touch TFT
    The FT6206 touch controller uses I2C at address 0x38 and needs SDA and SCL with pull-ups. Call the touch library's begin before reading. Touch is separate from the display's SPI bus.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.