Arduino and SPI TFT LCDs

I love TFTs because one can make with them professional looking project displays without necessarily breaking the bank.

I am particularly fond of the SPI interface because it uses a minimum number of I/O pins. This means that since even a minimal Arduino (one based on an ATmega328) can drive a low-cost TFT with I/O left for other tasks, the cost may be kept down. Nowadays, it is realistic to implement a basic Arduino with a 2.2″ TFT for less than 10€. An ATmega328 with an Arduino bootloader goes for 1,50€ on Ebay, a 2.2″ SPI TFT goes for about 3,50€, so “vintage” character LCDs are definitely on their way out.

So, let’s get down to business. What does one need in order to get one of these displays to work?
Obviously, you need the TFT display itself. I don’t care where you buy it from – you may get it from Adafruit or SparkFun or iTead or any one of the “big name” shops or you may get it from Ebay (a.k.a. “China”). In my experience, it doesn’t really matter as long as you know what you are purchasing. For example, on Ebay when you search for 2.4″ SPI TFT LCD you will come across this:

$_57_crop_res

and this:

$_57-(1)_crop_res

They are essentially the same TFTs, but the first one is ~1€ cheaper than the second one. The difference is the PCB that is included. Do not underestimate this PCB. If you go for the plain TFT you will have to solder it to a suitable PCB like this one:

2014-11-25 00.42.57_resize

Sure, it is no herculean task, but the TFT + adapter will most likely cost more than a TFT pre-mounted on a PCB.

Rolling your own PCB is indeed an option, but IMHO it is not worth it, not unless you are planning to go into mass production. For 1 or 2 pieces just do yourself a favor and shell out the extra €. You won’t regret it.

But let’s backtrack just a bit. How does one select a TFT? Surely, one would think that size and resolution are the most important factors. I say sure, as long as you have the software part covered. In order to actually show stuff on a TFT you need an appropriate library. You should not take for granted that such a library indeed exists for that gorgeous hi-res IPS TFT that you found for 10€ on Ebay. Many sellers on Ebay just write the word “arduino” on the TFT’s description without giving it much serious thought. Plus you should expect zero (0) support from most Ebay sellers. Most of them can’t and won’t help you if you run into trouble with your code.

So, you should always do a little research. Google is your friend. A good start is Karlsen Henning’s UTFT library. Being billed as a Universal TFT Library it does indeed support a large number of TFT controllers. If your display’s controller is included in UTFT’s compatibility list, you are somewhat covered. I say somewhat because UTFT is not always the best choice since it has a pretty heavy footprint. It will consume the better part of an ATmega328’s flash memory capacity. Fortunately, there are other libraries out there. I will go into more detail later on.

So, you got a TFT and are faced with the task of hooking it up to the Arduino. Relax, it’s simple. You only need to connect 4 or 5 wires, plus power and GND. Let’s start with the basics.

1) Power (Vcc). Most displays need 3.3V to function. This is a requirement of the TFT panel itself as well as of the driver IC that is always part of the assembly (it is an embedded part – you can not really see it). But as you probably know, most Arduinos run on 5V. Display manufacturers that make products for Arduino of course know that and usually include an on-board regulator that takes 5V as input and gives the necessary 3.3V. In most cases there is a selector on the PCB (jumper, solder bridge, or something) that lets you configure the board for 5 or 3.3 volt operation. Look out for that.

2) LED power. This pin controls the backlight of the TFT panel. It consists of a number of LEDs, depending on the size of the LCD panel. Bigger panel means more LEDs and thus more power consumption. It is usually connected to GND or to 5V/3.3V. Some times a current limiting resistor is also necessary. Other times the resistor is built-in and so is a mosfet that allows you to adjust the LED backlight’s brightness by connecting it to a pin that supports PWM (some of the more expensive TFTs support this). In any case, read the manual. You may come across a Chinese TFT that you had to have but then noticed that it has sparse if any documentation. If this happens, play it safe by connecting the LED pin to GND through a resistor (a few hundred ohms is usually a good starting point). If it lights, it means that the polarity is OK. If it does not, try applying 5 or 3.3V to it (through the resistor). If it lights but is too dim, use a smaller resistor. Usually each LED draws about 10-15mA, so if you know how many LEDs your TFT uses you can estimate its power draw and thus select a proper resistor.

3) Signalling. This is the nice part about using SPI: you only need 4 or 5 wires.
CLK (or SCLK / SCK): This is the clock input pin.
MOSI (or SDI / SDA): This is the Master Out Slave In pin. The actual raw data that is sent to the TFT passes through this wire.
CS (or TFT_CS or LCD-CS): This is the chip select pin.
D/C (or A0 or RS): This is the Data or Command selector pin.

We also have the Reset pin. Some times you can get away with connecting it to the Arduino’s reset pin, but it is better to connect it to a normal pin in order to have better control over it.

A special note here: Signalling is usually done at 3.3V unless the TFT’s manufacturer has implemented some kind of level shifting on board the PCB. This level shifting may be done by an IC (best case), or a bunch of transistors and resistors (fair enough..) or just 1.2K resistors (a bit of a kludge, but it usually works). It is important to be careful not to send 5V into a TFT that only supports 3.3V logic because in that case you will most likely damage the TFT.

At this point you need to take a break from the hardware and consider the software, since your choice of library will dictate the particulars of the next step, which is the connection of the signal wires to the Arduino.

Your main choices are two: the UTFT library and the Adafruit GFX library.

Each library has its strengths and weaknesses.

UTFT
Pros:

  • Very nice text support, especially with the add-on UTFT_DLB. Any TrueType font can be converted into a UTFT font of any size with minimum effort.
  • It is indeed universal. You only need to change one parameter in your code to support a different TFT. One library to rule them all, etc.

Cons:

  • Memory consumption. Nice fonts come at a price. No big deal if you have a MEGA or DUE, but makes things pretty cramped in an ATmega328.

Adafruit GFX
Pros:

  • Relatively small footprint.

Cons:

  • Ugly (blocky) fonts if you scale them to a non-native size. This is being fixed by 3rd party code that now supports a small number of proportional fonts but is nowhere near as versatile as UTFT’s code.

You really should become familiar with both of them since different projects will steer you towards one or the other.

Depending on your choice of library, you may need to use the hardware SPI pins for CLK and MOSI or you may be free to use any pins you like. It really just depends on the library.

Each of the libraries uses a slightly different notation for the signal pins. I will try to sum things up in this table:

[table “” not found /]

You may notice that most libraries say that you can just connect the TFT Reset pin to the Arduino Reset Pin. If you do that, you should put 0 as the reset pin.

Good luck!

TFT HiFiDuino v2.01 + video

As is usually the case, a few bugs crept into the v2 release. So, here is v2.01: TFT_HiFiDuino_v2.xx (10517 downloads ) (Note: As always, the code on this page may not be the current one, i.e. there may be a newer version available. The latest version is always up at the project’s official page.)

Also, here is a video of the code in action:

TFT HiFiDuino v2!

It’s official: Version 2 of the TFT HiFiDuino controller is complete!

There is a number of changes, thus the new version:

  • New minimal display mode as default. Goes into full display when changes are to be made to parameters.
  • Full graphics support in the minimal display.
  • New proportional fonts (TrueType).
  • New IR code. Now supports a much larger range of remote manufacturers.
  • Support of MCP23008 IC to control misc devices.
  • New option to set 0db as default (power-on) volume for connection to a preamp.

The code is (and will remain) compatible with my current shield. (Hint: shield v2 is also coming up!)

New requirements:

Plus the good old UTFT library.

I am including the necessary fonts and bitmaps in the ZIP. The fonts should go into your UTFT & UTFT_DLB directories, usually found in the Windows user’s Documents folders (for example, here: c:\Users\<user name>\Documents\Arduino\libraries\UTFT_DLB\).

The bitmaps should go into your sketch’s folder.

I have included in several places in the code SerialUSB output for debugging purposes. It is commented out in this release for performance purposes. However, it is very easy to re-enable for either debugging or viewing of the IR codes sent to the Arduino. You may use these IR codes to customize the code to support your remote by changing the relevant #define statements in the Remote control definitions section.

Due to code size and performance requirements I’m afraid that from v2 onwards TFT HiFiDuino will only be compatible with the Arduino Due. Sorry, it’s the price to pay for nice graphics. Thankfully, it’s a pretty low price. 😛

You may download it here: TFT_HiFiDuino_v2.xx (10517 downloads ) (Note: As always, the code on this page may not be the current one, i.e. there may be a newer version available. The latest version is always up at the project’s official page.)

I will soon update the code’s official page to v2.

IMG_8600_resize

TFT Hifiduino Code v0.95

Update: TFT Hifiduino Code v0.95 available!

Download from here: B3_arduino_code_v0_95 (Note: As always, the code on this page may not be the current one, i.e. there may be a newer version available. The latest version is always up at the project’s official page.)

Here is a partial changelog:

– Changed rotary encoder code. Should work for MEGA as well as Due (no longer using interrupts).

– Added remote control codes for virtually all of the functionality that is offered by the encoder so now all of the parameters can be set via remote.

Bugs:

– Selection bar is still buggy. Haven’t gotten around to sorting it out.

Missing functionality:

– Disply backlight handling. It is next in line to be sorted.

– Sleep mode. Much research is needed, so won’t be implemented soon.

Also, I’m working on a simple MEGA / Due shield that should simplify things a lot. It is a work in progress, so I’m not ready to publish schematics or anything, but you can have a look at the current version’s 3D model:

Render_01

3.2″ TFT pinout & connection to Arduino MEGA (or Due)

UPDATE (30/11/2013): Here is an easier way: https://www.dimdim.gr/2013/11/3-2%E2%80%B3-tft-connection-to-arduino-due-update/
Still, keep in mind that the info posted below still applies and is probably useful..

I’ve been asked what is the correct way to wire a 3.2″ TFT to an Arduino MEGA (or Due) in order to make it work with the UTFT library.

The answer of course depends on the exact model of the TFT that we have on hand. The below instructions apply to a generic 3.2″ TFT with wide aspect ratio and resolution of 240 x 400 that I got off of Ebay.

This is its pinout according to the manufacturer:

3.2-tft-pinout-pdf

This is nice, but I want to use a standard 40-pin ribbon cable which I have left over from an old computer, and its conductor numbering is a little different. At first I thought I’d try to make sense of it as I went but it didn’t take long for me to realize that it would actually save me time if I made a “conversion table”. So I came up with what you see here:

3.2-tft-pinout-xlsWhat we have here is the actual conductor number in the grey background (counting the conductors in the ribbon cable from left to right) and then above and below them the corresponding signal lines according to the above pinout. Above and below the signal lines I have noted the actual Arduino pins that correspond to the signals.

For example, pin 2 (the second pin on the flex cable looking at it from the left) corresponds to the DB0 signal which should be connected to the D37 pin on the Arduino MEGA (or Due). Note that the connections are made according to UTFT’s documentation and are applicable specifically to UTFT.

So we have to connect signals D0 through to D15 to the necessary digital pins. Then we also have to connect pins RS, WR, CS and REST to whichever pins we like (we must declare these pins in our sketch, see UTFT documentation). Pin 11 is RD and it must be pulled high, which means connecting it to +3.3V.  Pin 37 is the backlight illumination which means it must also be connected to +3.3V. This leaves pin 1 which must be connected to ground and pin 3 which must be connected to Vcc which in our case is 5V.

Note that I have not really gotten around to using the touchscreen capabilities or the SD reader, so I have not connected them to my Arduinos. It shouldn’t be difficult though.

Now, there is one more thing that I should point out and it is very important. The Arduino MEGA is using 5V logic while the TFT is expecting 3.3V logic. This means that if you connect the D0-D15 and RS, WR, CS, REST lines directly to the MEGA you will most likely damage the TFT. You need to connect a 10K resistor in series with each and every one of the lines. That will bring the voltage down to acceptable levels. Do not forget to do this!

In case of the Due the resistors are not necessary since it uses 3.3V logic so it is directly compatible.

Here is a little video I made of the 3.2″ TFT running a UTFT demo sketch:

Good luck!