CircuitPython

Adafruit CircuitPython Module Install

To use the TFT display with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_RGB_Display module on your board.  Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

Bundle Install

For express boards that have extra flash storage, like the Feather/Metro M0 express and Circuit Playground express, you can easily install the necessary libraries with Adafruit's CircuitPython bundle.  This is an all-in-one package that includes the necessary libraries to use the ILI9341 display with CircuitPython.  To install the bundle follow the steps in your board's guide, like these steps for the Feather M0 express board.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_rgb_display
  • adafruit_bus_device
  • adafruit_register

If your board supports USB mass storage, like the M0-based boards, then simply drag the files to the board's file system. Note on boards without external SPI flash, like a Feather M0 or Trinket/Gemma M0, you might run into issues on Mac OSX with hidden files taking up too much space when drag and drop copying, see this page for a workaround.

If your board doesn't support USB mass storage, like the ESP8266, then use a tool like ampy to copy the file to the board. You can use the latest version of ampy and its new directory copy command to easily move module directories to the board.

Furthermore, CircuitPython for M0 boards after version 0.8.1 do not have the framebuf module built in to save flash space. So, please download and install the pure Python implementation of framebuf and copy it to lib folder of the board as well.

Before continuing make sure your board's root filesystem has the adafruit_rgb_displayadafruit_bus_device, and adafruit_register folders/modules copied over.

Usage

The following section will show how to control the LED backpack from the board's Python prompt / REPL.  You'll walk through how to control the TFT display and learn how to use the CircuitPython module built for the display.  As a reference be sure to see the micropython-adafruit-rgb-display module documentation too.

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

SPI Initialization

On CircuitPython the SPI bus must be initialized before the display can be used by your code.  Run the following code to import the necessary modules and initialize the SPI bus:

Download: file
import board
import busio
import digitalio

spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# For the ESP8266
cs = digitalio.DigitalInOut(board.GPIO0)
dc = digitalio.DigitalInOut(board.GPIO15)

# For the Feather M0s
#cs = digitalio.DigitalInOut(board.D9)
#dc = digitalio.DigitalInOut(board.D10)

These lines create the SPI bus interface and two digital inputs/outputs for the chip select and data/command lines connected to the display.  Notice that the pin numbers might be different depending on your board and how it's wired.  Read the comments above and pick the correct two cs and dc lines to run for your setup.

Display Initialization

Next to import the display library and initialize it run the following code:

Download: file
from adafruit_rgb_display import ili9341, color565
display = ili9341.ILI9341(spi, cs=cs, dc=dc)

When creating the display instance of the ILI9341 class you'll need to know which pins are connected to the display's CSDC, and optionally RST or reset line.  For the TFT FeatherWing see its guide for details on these pin connections.

The CS and DC parameters to the ILI9341 class initializer are required and should be a pin from the board module. In CircuitPython they are DigitalInOut objects instead of pins directly (like GPIO0) so that other types of GPIO can be used such as GPIO expanders.

There are a few optional keyword arguments you can specify too:

  • rst - This is a GPIO pin connected to the RST or reset line on the display.  The default for this is to not be specified and reset is not used.
  • width - The width of the display in pixels, the default is 240.
  • height - The height of the display in pixels, the default is 320.

Drawing

Once the display is initialized you're ready to perform basic fill and pixel drawing.  First to fill the display with a solid color use the fill function:

Download: file
display.fill(color565(255, 0, 0))

You should see the display fill entirely with a solid red color after running the command above.

Notice how the color565 function is called to get a color that's passed to the fill function.  This color565 function takes in the red, green, and blue color component values which should range from 0 (lowest intensity) to 255 (highest intensity).  Try filling the display with different color values!

To clear the display back to black, fill it with a zero color value:

Download: file
display.fill(0)

You can draw individual pixels with the pixel function.  For example to draw a white pixel at the origin position 0, 0:

Download: file
display.pixel(0, 0, color565(255, 255, 255))

Or to draw a pixel at the opposite corner at position 239, 319:

Download: file
display.pixel(239, 319, color565(255, 255, 255))

The pixel function takes the following parameters:

  • X positon of the pixel to draw.
  • Y position of the pixel to draw.
  • Color of the pixel.  Use the ili9341.color565 function to generate this color value from red, green, blue component values.

In addition to pixel drawing there's a filled rectangle drawing command called fill_rectangle.  For example to draw a blue box in one quadrant of the screen run:

Download: file
display.fill_rectangle(0, 0, 120, 170, color565(0, 0, 255))

The fill_rectangle function takes the following parameters:

  • X position of the rectangle upper left corner.
  • Y position of the rectangle upper left corner.
  • Width of the rectangle in pixels.
  • Height of the rectangle in pixels.
  • Color of the rectangle.  Again use the ili9341.color565 function to generate this value.

That's all there is to drawing on the ILI9341 display with CircuitPython!  Right now only basic fill, pixel, and filled rectangle drawing commands are supported.  However since this is a pixel-based display you can also draw text with the bitmap font library.  There's even a basic graphics library to draw lines and other shapes!

This guide was first published on Nov 16, 2016. It was last updated on Nov 16, 2016. This page (CircuitPython) was last updated on Feb 17, 2020.