1 8 TFT Display
1 8 TFT Display
1 8 TFT Display
https://learn.adafruit.com/1-8-tft-display
Overview 5
Pinouts 8
• EYESPI
EYESPI 10
• The EYESPI Connector and Cables
• Wiring Your EYESPI Display
• EYESPI Pins
Breakout Assembly 15
• Prepare the header strip:
• Add the breakout board:
• And Solder!
Displaying Bitmaps 22
• Breakout Wiring
• Example Sketch
Python Usage 40
• Turning on the Backlight
• Displaying an Image
• Drawing Shapes and Text
• Displaying System Information
Original V1 Shield 65
• Original V1.0 Shield
Graphics Library 72
Troubleshooting 73
Downloads 74
• Files & Datasheets
• Breakout Schematic
• Breakout Fabrication print
• Shield v2 Schematic & Fab Print
• Shield V1 Schematic & Fab Print
This tutorial is for our 1.8" diagonal TFT display. It comes packaged as a breakout or
as an Arduino shield. Both styles have a microSD interface for storing files and
images. These are both great ways to add a small, colorful and bright display to any
project. Since the display uses 4-wire SPI to communicate and has its own pixel-
addressable frame buffer, it requires little memory and only a few pins. This makes it
ideal for use with small microcontrollers.
This display breakout comes with an EYESPI connector! This 18-pin 0.5mm pitch FPC
connector has a flip-top connector for using a flex cable to hook up your display. It
The shield version plugs directly into an Arduino with no wiring required. The
breakout version can be used with every kind of microcontroller.
The 1.8" display has 128x160 color pixels. Unlike the low cost "Nokia 6110" and similar
LCD displays, which are CSTN type and thus have poor color and slow refresh, this
display is a true TFT! The TFT driver (ST7735R) can display full 18-bit color (262,144
shades!). And the LCD will always come with the same driver chip so there's no
worries that your code will not work from one to the other.
Both boards have the TFT soldered on (it uses a delicate flex-circuit connector) as
well as a ultra-low-dropout 3.3V regulator and a 3/5V level shifter so you can use it
with 3.3V or 5V power and logic. These also include a microSD card holder so you
can easily load full color bitmaps from a FAT16/FAT32 formatted microSD card. And on
the Shield version, we've added a nifty 5-way joystick navigation switch!
This lovely little display breakout is the best way to add a small, colorful and bright
display to any project. Since the display uses 4-wire SPI to communicate and has its
own pixel-addressable frame buffer, it can be used with every kind of microcontroller.
Even a very small one with low memory and few pins available!
The 1.8" display has 128x160 color pixels. Unlike the low cost "Nokia 6110" and similar
LCD displays, which are CSTN type and thus have poor color and slow refresh, this
display is a true TFT! The TFT driver (ST7735R) can display full 18-bit color (262,144
shades!). And the LCD will always come with the same driver chip so there's no
worries that your code will not work from one to the other.
The breakout has the TFT display soldered on (it uses a delicate flex-circuit
connector) as well as a ultra-low-dropout 3.3V regulator and a 3/5V level shifter so
you can use it with 3.3V or 5V power and logic. We also had a little space so we
placed a microSD card holder so you can easily load full color bitmaps from a FAT16/
FAT32 formatted microSD card. The microSD card is not included, but you can pick
one up here (http://adafru.it/102).
This display breakout also features an 18-pin "EYESPI" standard FPC connector () with
flip-top connector. You can use an 18-pin 0.5mm pitch FPC cable () to connect to all
the GPIO pins for when you want to skip the soldering.
EYESPI
This display comes with an EYESPI connector, which is an 18pin 0.5mm pitch
connector that allows you to use a flex cable to connect your display to your
microcontroller. For more details, visit the EYESPI page ().
This color display uses SPI to receive image data. That means you need at least 4
pins - clock, data in, tft cs and d/c. If you'd like to have SD card usage too, add
• Lite - this is the PWM input for the backlight control. Connect to 3-5VDC to turn
on the backlight. Connect to ground to turn it off. Or, you can PWM at any
frequency.
• MISO - this is the SPI Microcontroller In Serial Out pin, its used for the SD card. It
isn't used for the TFT display which is write-only
• SCLK - this is the SPI clock input pin
• MOSI - this is the SPI Microcontroller Out Serial In pin, it is used to send data
from the microcontroller to the SD card and/or TFT
• TFT_CS - this is the TFT SPI chip select pin
• Card CS - this is the SD card chip select, used if you want to read from the SD
card.
• D/C - this is the TFT SPI data or command selector pin
• RST - this is the TFT reset pin. Connect to ground to reset the TFT! Its best to
have this pin controlled by the library so the display is reset cleanly, but you can
also connect it to the Arduino Reset pin, which works for most cases.
• Vcc - this is the power pin, connect to 3-5VDC - it has reverse polarity protection
but try to wire it right!
• GND - this is the power and signal ground pin
For the level shifter we use the CD74HC4050 () which has a typical propagation delay
of ~10ns
This display now comes with an EYESPI connector. This connector allows you to
connect your display without soldering. There are EYESPI cables () available in
multiple lengths, which means you can find one to fit any project. This is especially
useful if your project requires the display to be freestanding, and not tied directly into
a breadboard. Inspired by the popularity of STEMMA QT, it provides plug-n-play for
displays!
The EYESPI connector is designed to work with 18-pin 0.5mm pitch flex cables.
Other flex cables, such as Raspberry Pi camera flex cables, will not work!
The following example shows how to connect the 1.8" TFT Display Breakout to a
Feather RP2040 using the EYESPI breakout board.
Connect the following Feather pins to the associated EYESPI breakout pins:
Finally, connect your display EYESPI connector to the breakout EYESPI connector usin
g an EYESPI cable. For details on using the EYESPI connector properly, visit Plugging
in an EYESPI Cable ().
EYESPI Pins
Though there are 18 pins available on the EYESPI connector, many displays do not
use all available pins. This display requires the following pins:
• Vin - This is the power pin. To power the board (and thus your display), connect
to the same power as the logic level of your microcontroller, e.g. for a 3V micro
like a Feather, use 3V, and for a 5V micro like an Arduino, use 5V.
• Lite - This is the PWM input for the backlight control. It is by default pulled high
(backlight on), however, you can PWM at any frequency or pull down to turn the
backlight off.
You can connect an EYESPI compatible display to the EYESPI breakout board using
an EYESPI cable. An EYESPI cable is an 18 pin flexible PCB (FPC). The FPC can only
be connected properly in one orientation, so be sure to follow the steps below to
ensure that your display and breakout are plugged in properly.
Breakout Assembly
If using an older version of the Arduino IDE (pre-1.8.10), also locate and install the Adaf
ruit_BusIO library (newer versions do this automatically when using the Arduino
Library Manager).
If this is all unfamiliar, we have a tutorial introducing Arduino library concepts and
installation ().
Now upload the sketch to your Arduino. You may need to press the Reset button to
reset the arduino and TFT. You should see a collection of graphical tests draw out on
the TFT.
Once uploaded, the Arduino should perform all the test display procedures! If you're
not seeing anything - first check if you have the backlight on, if the backlight is not lit
Changing Pins
Now that you have it working, there's a few things you can do to change around the
pins.
If you're using Hardware SPI, the CLOCK and MOSI pins are 'fixed' and cant be
changed. But you can change to software SPI, which is a bit slower, and that lets you
pick any pins you like. Find these lines:
Comment out option 1, and uncomment option 2. Then you can change the TFT_ pins
to whatever pins you'd like!
#define TFT_RST 9
to
#define TFT_RST -1
and connecting the RST line to the Arduino Reset pin. That way the Arduino will auto-
reset the TFT as well.
In this example, we'll show how to display a 128x160 pixel full color bitmap from a
microSD card.
We have an example sketch in the library showing how to display full color bitmap
images stored on an SD card. You'll need a microSD card such as this one (http://
adafru.it/102).
It's really easy to draw bitmaps. We have a library for it, Adafruit_ImageReader, which
can be installed through the Arduino Library Manager (Sketch→Include
Library→Manage Libraries…). Enter “imageread” in the search field and the library is
easy to spot:
Download parrot.bmp
If you want to later use your own image, use an image editing tool and crop your
image to no larger than 160 pixels high and 128 pixels wide. Save it as a 24-bit color B
MP file - it must be 24-bit color format to work, even if it was originally a 16-bit color
image - because of the way BMPs are stored and displayed!
Copy the parrot.bmp to the microSD card and insert it into the micro SD card holder
on your shield or breakout board.
Breakout Wiring
Shield users can skip directly to the "Example Sketch" section.
Wire up the TFT as described on the wiring & test page and add the two wires for
talking to the SD card. Connect CARD_CS (the unconnected pin in the middle) to
digital pin 4 (you can change this later to any pin you want). Connect MISO (second
from the right) to the Arduino's hardware SPI MISO pin. For Classic arduinos, this is
pin 12. For Mega's this is pin 50. You can't change the MISO pin, it's fixed in the chip
hardware.
Now upload the example sketch to the Arduino. It should display the parrot image. If
you have any problems, check the serial console for any messages such as not being
able to initialize the microSD card or not finding the image.
Adafruit_CircuitPython_ST7735R
First, make sure you are running the latest version of Adafruit CircuitPython () for your
board.
Next, you'll need to install the necessary libraries to use the hardware--carefully
follow the steps to find and install these libraries from Adafruit's CircuitPython library
bundle (). Our introduction guide has a great page on how to install the library
bundle () for both express and non-express boards.
• adafruit_st7735r
Before continuing make sure your board's lib folder or root filesystem has the adafruit
_st7735r file copied over.
Adafruit_CircuitPython_Display_Text
Go ahead and install this in the same manner as the driver library by copying the adaf
ruit_display_text folder over to the lib folder on your CircuitPython device.
"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
from adafruit_st7735r import ST7735R
spi = board.SPI()
tft_cs = board.D5
tft_dc = board.D6
display_bus = displayio.FourWire(
spi, command=tft_dc, chip_select=tft_cs, reset=board.D9
)
# Draw a label
text_group = displayio.Group(scale=2, x=11, y=64)
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
text_group.append(text_area) # Subgroup for text scaling
splash.append(text_group)
while True:
pass
Let's take a look at the sections of code one by one. We start by importing the board
so that we can initialize SPI , displayio , terminalio for the font, a label , and
the adafruit_st7735r driver.
import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_st7735r import ST7735R<div class="open_grepper_editor" title="Edit
& Save To Grepper"></div><div class="open_grepper_editor" title="Edit &
Save To Grepper"></div>
Next we release any previously used displays. This is important because if the
Feather is reset, the display pins are not automatically released and this makes them
available for use again.
Next, we set the SPI object to the board's SPI with the easy shortcut
function board.SPI() . By using this function, it finds the SPI module and initializes
using the default SPI parameters.
spi = board.SPI()
tft_cs = board.D5
tft_dc = board.D6<div class="open_grepper_editor" title="Edit & Save To
Grepper"></div><div class="open_grepper_editor" title="Edit & Save To
Grepper"></div>
In the next line, we set the display bus to FourWire which makes use of the SPI bus.
Finally, we initialize the driver with a width of 160 and a height of 128. If we stopped at
this point and ran the code, we would have a terminal that we could type at and have
the screen update. Because we want to use the display horizontally and the default
orientation is vertical, we rotate it 90 degrees. One other parameter that we provide
is bgr=True and the reason for this is that the color ordering of certain displays is
Blue, Green, Red rather than the usual Red, Green, Blue. It tell displayio the correct
color ordering for this particular display.
splash = displayio.Group(max_size=10)
display.show(splash)<div class="open_grepper_editor" title="Edit & Save To
Grepper"></div><div class="open_grepper_editor" title="Edit & Save To
Grepper"></div>
Next we create a Bitmap which is like a canvas that we can draw on. In this case we
are creating the Bitmap to be the same size as the screen, but only have one color.
The Bitmaps can currently handle up to 256 different colors. We create a Palette with
one color and set that color to 0x00FF00 which happens to be green. Colors are
With all those pieces in place, we create a TileGrid by passing the bitmap and palette
and draw it at (0, 0) which represents the display's upper left.
bg_sprite = displayio.TileGrid(color_bitmap,
pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)<div class="open_grepper_editor" title="Edit & Save To
Grepper"></div><div class="open_grepper_editor" title="Edit & Save To
Grepper"></div>
Next we will create a smaller purple square. The easiest way to do this is the create a
new bitmap that is a little smaller than the full screen with a single color and place it in
a specific location. In this case, we will create a bitmap that is 5 pixels smaller on each
side. The screen is 160x128, so we'll want to subtract 10 from each of those numbers.
We'll also want to place it at the position (5, 5) so that it ends up centered.
Next let's add a label that says "Hello World!" on top of that. We're going to use the
built-in Terminal Font and scale it up by a factor of two. To scale the label only, we will
make use of a subgroup, which we will then add to the main group.
Labels are centered vertically, so we'll place it at 64 for the Y coordinate, and around
11 pixels make it appear to be centered horizontally, but if you want to change the
text, change this to whatever looks good to you. Let's go with some yellow text, so
we'll pass it a value of 0xFFFF00 .
Finally, we place an infinite loop at the end so that the graphics screen remains in
place and isn't replaced by a terminal.
while True:
pass<div class="open_grepper_editor" title="Edit & Save To Grepper"></
div><div class="open_grepper_editor" title="Edit & Save To Grepper"></div>
We'll cover how to wire the display to your Raspberry Pi. First assemble your display.
Since there's dozens of Linux computers/boards you can use we will show wiring for
Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to
see whether your platform is supported ().
Note this is not a kernel driver that will let you have the console appear on the
TFT. However, this is handy when you can't install an fbtft driver, and want to use
the TFT purely from 'user Python' code!
These displays are set up to use the 8-bit data lines by default. We want to use them
for SPI. To do that, you'll need to either solder bridge some pads on the back or
These larger displays are set to use 8-bit data lines by default and may need to
be modified to use SPI.
1.8" Display
• GND connects to the Raspberry Pi's ground
• Vin connects to the Raspberry Pi's 3V pin
• RST connects to our Reset pin. We'll be using GPIO 24 but this can be changed
later.
• D/C connects to our SPI Chip Select pin. We'll be using GPIO 25, but this can be
changed later as well.
• CS connects to our SPI Chip Select pin. We'll be using CE0
• MOSI connects to SPI MOSI. On the Raspberry Pi, thats also MOSI
• CLK connects to SPI clock. On the Raspberry Pi, thats SLCK
• LITE connects to the Raspberry Pi's 3V pin. This can be used to separately
control the backlight.
SSD1351-based Displays
1.27" and 1.5" OLED Displays
• GND connects to the Raspberry Pi's ground
• Vin connects to the Raspberry Pi's 3V pin
• CLK connects to SPI clock. On the Raspberry Pi, thats SLCK
• MOSI connects to SPI MOSI. On the Raspberry Pi, thats also MOSI
• CS connects to our SPI Chip Select pin. We'll be using CE0
• RST connects to our Reset pin. We'll be using GPIO 24 but this can be changed
later.
• D/C connects to our SPI Chip Select pin. We'll be using GPIO 25, but this can be
changed later as well.
SSD1331-based Display
0.96" OLED Display
• MOSI connects to SPI MOSI. On the Raspberry Pi, thats also MOSI
• CLK connects to SPI clock. On the Raspberry Pi, thats SLCK
• D/C connects to our SPI Chip Select pin. We'll be using GPIO 25, but this can be
changed later.
• RST connects to our Reset pin. We'll be using GPIO 24 but this can be changed
later as well.
• CS connects to our SPI Chip Select pin. We'll be using CE0
• Vin connects to the Raspberry Pi's 3V pin
• GND connects to the Raspberry Pi's ground
Setup
You'll need to install the Adafruit_Blinka library that provides the CircuitPython
support in Python. This may also require enabling SPI on your platform and verifying
you are running Python 3. Since each platform is a little different, and Linux changes
often, please visit the CircuitPython on Linux guide to get your computer ready ()!
If you have previously installed the Kernel Driver with the PiTFT Easy Setup, you
will need to remove it first in order to run this example.
If your default Python is version 3 you may need to run 'pip' instead. Just make sure
you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
This package was previously calls ttf-dejavu, so if you are running an older version of
Raspberry Pi OS, it may be called that.
Pillow Library
We also need PIL, the Python Imaging Library, to allow graphics and using text with
custom fonts. There are several system libraries that PIL relies on, so installing via a
package manager is the easiest way to bring in everything:
If you installed the PIL through PIP, you may need to install some additional libraries:
Python Usage
If you have previously installed the Kernel Driver with the PiTFT Easy Setup, you
will need to remove it first in order to run this example.
Now that you have everything setup, we're going to look over three different
examples. For the first, we'll take a look at automatically scaling and cropping an
image and then centering it on the display.
Displaying an Image
Here's the full code to the example. We will go through it section by section to help
you better understand what is going on. Let's start by downloading an image of
Blinka. This image has enough border to allow resizing and cropping with a variety of
display sizes and rations to still look good.
Make sure you save it as blinka.jpg and place it in the same folder as your script.
Here's the code we'll be loading onto the Raspberry Pi. We'll go over the interesting
parts.
"""
Be sure to check the learn guides for more usage information.
This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
import digitalio
import board
from PIL import Image, ImageDraw
from adafruit_rgb_display import ili9341
from adafruit_rgb_display import st7789 # pylint: disable=unused-import
from adafruit_rgb_display import hx8357 # pylint: disable=unused-import
from adafruit_rgb_display import st7735 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1351 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1331 # pylint: disable=unused-import
# pylint: disable=line-too-long
# Create the display:
# disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789
# disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54"
ST7789
# disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53,
y_offset=40, # 1.14" ST7789
# disp = st7789.ST7789(spi, rotation=90, width=172, height=320, x_offset=34, #
1.47" ST7789
# disp = st7789.ST7789(spi, rotation=270, width=170, height=320, x_offset=35, #
1.9" ST7789
# disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
# disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R
# disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, #
1.44" ST7735R
# disp = st7735.ST7735R(spi, rotation=90, bgr=True, width=80, # 0.96" MiniTFT
Rev A ST7735R
# disp = st7735.ST7735R(spi, rotation=90, invert=True, width=80, # 0.96" MiniTFT
Rev B ST7735R
# x_offset=26, y_offset=1,
# disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351
# disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351
# disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331
disp = ili9341.ILI9341(
spi,
rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
# pylint: enable=line-too-long
image = Image.open("blinka.jpg")
# Display image.
disp.image(image)
So we start with our usual imports including a couple of Pillow modules and the
display drivers. That is followed by defining a few pins here. The reason we chose
these is because they allow you to use the same code with the PiTFT if you chose to
do so.
import digitalio
import board
from PIL import Image, ImageDraw
import adafruit_rgb_display.ili9341 as ili9341
import adafruit_rgb_display.st7789 as st7789
import adafruit_rgb_display.hx8357 as hx8357
import adafruit_rgb_display.st7735 as st7735
import adafruit_rgb_display.ssd1351 as ssd1351
import adafruit_rgb_display.ssd1331 as ssd1331
Next we'll set the baud rate from the default 24 MHz so that it works on a variety of
displays. The exception to this is the SSD1351 driver, which will automatically limit it to
16MHz even if you pass 24MHz. We'll set up out SPI bus and then initialize the display.
We wanted to make these examples work on as many displays as possible with very
few changes. The ILI9341 display is selected by default. For other displays, go ahead
and comment out these lines:
disp = ili9341.ILI9341(
spi,
rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
Next we read the current rotation setting of the display and if it is 90 or 270 degrees,
we need to swap the width and height for our calculations, otherwise we just grab the
width and height. We will create an image with our dimensions and use that to create
a draw object. The draw object will have all of our drawing functions.
Next we clear whatever is on the screen by drawing a black rectangle. This isn't
strictly necessary since it will be overwritten by the image, but it kind of sets the
stage.
image = Image.open("blinka.jpg")
Here's where it starts to get interesting. We want to scale the image so that it matches
either the width or height of the display, depending on which is smaller, so that we
have some of the image to chop off when we crop it. So we start by calculating the
width to height ration of both the display and the image. If the height is the closer of
the dimensions, we want to match the image height to the display height and let it be
a bit wider than the display. Otherwise, we want to do the opposite.
Once we've figured out how we're going to scale it, we pass in the new dimensions
and using a Bicubic rescaling method, we reassign the newly rescaled image back to
image . Pillow has quite a few different methods to choose from, but Bicubic does a
great job and is reasonably fast.
Next we want to figure the starting x and y points of the image where we want to
begin cropping it so that it ends up centered. We do that by using a standard
centering function, which is basically requesting the difference of the center of the
display and the center of the image. Just like with scaling, we replace the image
variable with the newly cropped image.
Finally, we take our image and display it. At this point, the image should have the
exact same dimensions at the display and fill it completely.
disp.image(image)
"""
This demo will draw a few rectangles onto the screen along with some text
on top of that.
This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import ili9341
from adafruit_rgb_display import st7789 # pylint: disable=unused-import
from adafruit_rgb_display import hx8357 # pylint: disable=unused-import
from adafruit_rgb_display import st7735 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1351 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1331 # pylint: disable=unused-import
# pylint: disable=line-too-long
# Create the display:
# disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789
# disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54"
ST7789
# disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53,
y_offset=40, # 1.14" ST7789
# disp = st7789.ST7789(spi, rotation=90, width=172, height=320, x_offset=34, #
1.47" ST7789
# disp = st7789.ST7789(spi, rotation=270, width=170, height=320, x_offset=35, #
1.9" ST7789
# disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
# disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R
# disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, #
1.44" ST7735R
# disp = st7735.ST7735R(spi, rotation=90, bgr=True, width=80, # 0.96" MiniTFT
Rev A ST7735R
# disp = st7735.ST7735R(spi, rotation=90, invert=True, width=80, # 0.96" MiniTFT
Rev B ST7735R
# x_offset=26, y_offset=1,
# disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351
# disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351
# disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331
disp = ili9341.ILI9341(
spi,
rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
# pylint: enable=line-too-long
# Display image.
disp.image(image)
Just like in the last example, we'll do our imports, but this time we're including the
ImageFont Pillow module because we'll be drawing some text this time.
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341
Next we'll define some parameters that we can tweak for various displays. The BORDE
R will be the size in pixels of the green border between the edge of the display and
the inner purple rectangle. The FONTSIZE will be the size of the font in points so that
we can adjust it easily for different displays.
BORDER = 20
FONTSIZE = 24
Next, just like in the previous example, we will set up the display, setup the rotation,
and create a draw object. If you have are using a different display than the ILI9341, go
ahead and adjust your initializer as explained in the previous example. After that, we
will setup the background with a green rectangle that takes up the full screen. To get
green, we pass in a tuple that has our Red, Green, and Blue color values in it in that
order which can be any integer from 0 to 255 .
Next we will draw an inner purple rectangle. This is the same color value as our
example in displayio quickstart, except the hexadecimal values have been converted
to decimal. We use the BORDER parameter to calculate the size and position that we
want to draw the rectangle.
Next we'll load a TTF font. The DejaVuSans.ttf font should come preloaded on
your Pi in the location in the code. We also make use of the FONTSIZE parameter
that we discussed earlier.
Now we draw the text Hello World onto the center of the display. You may recognize
the centering calculation was the same one we used to center crop the image in the
previous example. In this example though, we get the font size values using the gets
ize() function of the font object.
disp.image(image)
"""
This will show some Linux Statistics on the attached display. Be sure to adjust
to the display you have connected. Be sure to check the learn guides for more
usage information.
import time
import subprocess
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import ili9341
from adafruit_rgb_display import st7789 # pylint: disable=unused-import
from adafruit_rgb_display import hx8357 # pylint: disable=unused-import
from adafruit_rgb_display import st7735 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1351 # pylint: disable=unused-import
from adafruit_rgb_display import ssd1331 # pylint: disable=unused-import
# pylint: disable=line-too-long
# Create the display:
# disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789
# disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54"
ST7789
# disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53,
y_offset=40, # 1.14" ST7789
# disp = st7789.ST7789(spi, rotation=90, width=172, height=320, x_offset=34, #
1.47" ST7789
# disp = st7789.ST7789(spi, rotation=270, width=170, height=320, x_offset=35, #
1.9" ST7789
# disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357
# disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R
# disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, #
1.44" ST7735R
# disp = st7735.ST7735R(spi, rotation=90, bgr=True, width=80, # 0.96" MiniTFT
Rev A ST7735R
# disp = st7735.ST7735R(spi, rotation=90, invert=True, width=80, # 0.96" MiniTFT
Rev B ST7735R
# x_offset=26, y_offset=1,
# disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351
# disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351
# disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331
disp = ili9341.ILI9341(
spi,
rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
# pylint: enable=line-too-long
# Load a TTF font. Make sure the .ttf font file is in the
# same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24)
while True:
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Display image.
disp.image(image)
time.sleep(0.1)
Just like the last example, we'll start by importing everything we imported, but we're
adding two more imports. The first one is time so that we can add a small delay and
the other is subprocess so we can gather some system information.
import time
import subprocess
import digitalio
import board
Next, just like in the first two examples, we will set up the display, setup the rotation,
and create a draw object. If you have are using a different display than the ILI9341, go
ahead and adjust your initializer as explained in the previous example.
Just like in the first example, we're going to draw a black rectangle to fill up the
screen. After that, we're going to set up a couple of constants to help with positioning
text. The first is the padding and that will be the Y-position of the top-most text and
the other is x which is the X-Position and represents the left side of the text.
Now we get to the main loop and by using while True: , it will loop until Control+C
is pressed on the keyboard. The first item inside here, we clear the screen, but notice
that instead of giving it a tuple like before, we can just pass 0 and it will draw black.
Next, we run a few scripts using the subprocess function that get called to the
Operating System to get information. The in each command is passed through awk in
order to be formatted better for the display. By having the OS do the work, we don't
have to. These little scripts came from https://unix.stackexchange.com/
questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-
load
Now we display the information for the user. Here we use yet another way to pass
color information. We can pass it as a color string using the pound symbol, just like we
y = padding
draw.text((x, y), IP, font=font, fill="#FFFFFF")
y += font.getsize(IP)[1]
draw.text((x, y), CPU, font=font, fill="#FFFF00")
y += font.getsize(CPU)[1]
draw.text((x, y), MemUsage, font=font, fill="#00FF00")
y += font.getsize(MemUsage)[1]
draw.text((x, y), Disk, font=font, fill="#0000FF")
y += font.getsize(Disk)[1]
draw.text((x, y), Temp, font=font, fill="#FF00FF")
Finally, we write all the information out to the display using disp.image() . Since we
are looping, we tell Python to sleep for 0.1 seconds so that the CPU never gets too
busy.
disp.image(image)
time.sleep(.1)
SD Card
You can read more about installing libraries in our tutorial ().
Once you've gotten this far try pressing all the buttons on the board (except for
RESET) to activate the invert-blinking loop.
The graphics don't look identical to the below but you should still press all the
buttons as shown!
Displaying a Bitmap
If you have parrot.bmp () stored on the SD card you will get a nice parrot display once
the buttons have all been pressed
You could use a Grand Central which also has an M4 Processor. For this board, be
sure to check out our Introducing the Adafruit Grand Central M4 Express () guide.
If you need WiFi capabilities for your project, you could also use the Metro M4 Airlift
Lite. For this board, be sure to check out our Adafruit Metro M4 Express AirLift ()
guide.
Adafruit_CircuitPython_BusDevice
Adafruit_CircuitPython_seesaw/
releases
Adafruit_CircuitPython_ST7735R
First, make sure you are running the latest version of Adafruit CircuitPython () for your
board.
Next, you'll need to install the necessary libraries to use the hardware--carefully
follow the steps to find and install these libraries from Adafruit's CircuitPython library
bundle (). Our introduction guide has a great page on how to install the library
bundle () for both express and non-express boards.
• adafruit_st7735r
• adafruit_seesaw
• adafruit_bus_device
Before continuing make sure your board's lib folder or root filesystem has the adafruit
_st7735r, adafruit_seesaw and adafruit_bus_device files and folders copied over.
"""
This example will test out the display on the 1.8" TFT Shield
"""
import time
import board
import displayio
from adafruit_seesaw.tftshield18 import TFTShield18
from adafruit_st7735r import ST7735R
ss = TFTShield18()
spi = board.SPI()
tft_cs = board.D10
tft_dc = board.D8
ss.tft_reset()
display = ST7735R(display_bus, width=160, height=128, rotation=90, bgr=True)
ss.set_backlight(True)
while True:
buttons = ss.buttons
if buttons.right:
print("Button RIGHT!")
if buttons.down:
print("Button DOWN!")
if buttons.left:
print("Button LEFT!")
if buttons.up:
print("Button UP!")
if buttons.select:
print("Button SELECT!")
if buttons.a:
if buttons.b:
print("Button B!")
if buttons.c:
print("Button C!")
time.sleep(0.001)
Let's take a look at the sections of code one by one. We start by importing time , so
we can pause, the board so that we can initialize SPI, displayio , the tftshield1
8 seesaw library, and the adafruit_ili9341 driver.
import time
import board
import displayio
from adafruit_seesaw.tftshield18 import TFTShield18
from adafruit_st7735r import ST7735R
Next we release any previously used displays. This is important because if the Metro
is reset, the display pins are not automatically released and this makes them available
for use again.
displayio.release_displays()
We set up seesaw using the TFTShield18, which was written specifically for this shield
to make things very easy.
ss = TFTShield18()
Next, we set the SPI object to the board's SPI with the easy shortcut
function board.SPI() . By using this function, it finds the SPI module and initializes
using the default SPI parameters. Next we set the Chip Select and Data/Command
pins that will be used.
spi = board.SPI()
tft_cs = board.D10
tft_dc = board.D8
In the next line, we set the display bus to FourWire which makes use of the SPI bus.
Finally, we reset the display, initialize the driver with a width of 160 and a height of
128, and turn on the backlight. If we stopped at this point and ran the code, we would
have a terminal that we could type at and have the screen update.
ss.set_backlight(True)
Finally, we place an infinite loop at the end and constantly read the buttons. If a
button is detected as being pressed, a message specifies which one. Multiple buttons
can be pressed at the same time. We also provide an optional small delay to allow
you to adjust how quickly you want the buttons to read in case you want to debounce
the output.
while True:
buttons = ss.buttons
if buttons.right:
print("Button RIGHT!")
if buttons.down:
print("Button DOWN!")
if buttons.left:
print("Button LEFT!")
if buttons.up:
print("Button UP!")
if buttons.select:
print("Button SELECT!")
if buttons.a:
print("Button A!")
if buttons.b:
print("Button B!")
if buttons.c:
print("Button C!")
Now go ahead and run the code. Once it's running, try pushing a few buttons and see
what happens.
Original V1 Shield
Original V1.0 Shield
If your shield looks like this, you have the
original 1.8" TFT shield which does not
arduino_compatibles_lcds___displays_2013_05_03_IMG_1762-1024.jpg
have a helper seesaw chip
The shield uses the "Classic Arduino" SPI wiring and will perform best with Atmega
328-based Arduinos such as the Uno. It can work with other Arduinos but not very
well.
To use with the shield, modify the example code pin definitions as follows.
This is only required for the V1 shield, the V2 shield uses the hardware SPI port
so it's not necessary to use software SPI and in fact it won't work!
The Example code has 2 options for defining the display object. Uno, Duemilanove an
d other Atmega 328-based processors can use the "Option 1" version of the
constructor for best performance:
Mega and Leonardo users should use the "Option 2" version of the constructor for
compatibility:
The shield comes with all surface mount parts pre-soldered. All that remains is to
install the headers!
The 5-way joystick on the shield is great for implementing menu navigation or even
for use as a tiny game controller. To minimize the number of pins required, the joystick
uses a different resistor on each leg of the control to create a variable voltage divider
that can be monitored with a single analog pin. Each movement of the joystick control
connects a different resistor and results in a different voltage reading.
You can use this code as the input method for your menu system or game:
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
#define Neutral 0
#define Press 1
#define Up 2
#define Down 3
#define Right 4
#define Left 5
void loop()
{
int joy = CheckJoystick();
switch (joy)
{
case Left:
Serial.println("Left");
break;
case Right:
Serial.println("Right");
break;
case Up:
Serial.println("Up");
break;
case Down:
Serial.println("Down");
break;
case Press:
Serial.println("Press");
break;
}
}
Graphics Library
We've written a full graphics library specifically for this display which will get you up
and running quickly. The code is written in C/C++ for Arduino but is easy to port to any
microcontroller by rewritting the low level pin access functions.
The TFT LCD library is based off of the Adafruit GFX graphics core library. GFX has
many ready to go functions that should help you start out with your project. It's not
exhaustive and we'll try to update it if we find a really useful function. Right now it
supports pixels, lines, rectangles, circles, round-rects, triangles and printing text as
well as rotation.
Two libraries need to be downloaded and installed: first is the ST7735 library () (this
contains the low-level code specific to this device), and second is the Adafruit GFX
Library () (which handles graphics operations common to many displays we carry). You
can install these with the Arduino library manager.
Check out the GFX tutorial for detailed information about what is supported and how
to use it ()!
Troubleshooting
In Arduino, use delay() to add a few milliseconds before calling tft.begin(). Adjust
the amount of delay as needed to see how little you can get away with for your
specific setup.
Downloads
Files & Datasheets
• Adafruit GFX library ()
• Adafruit ST7735 library ()(See our detailed tutorial for installation assistance ())
• Fritzing object in the Adafruit library ()
• Datasheet for the display ()
• Datasheet for the display driver chip ().
• EagleCAD PCB files for TFT shield ()
• EagleCAD PCB files for TFT breakout ()
Breakout Schematic
For the level shifter we use the CD74HC4050 which has a typical propagation delay
of ~10ns