• sales

    +86-0755-88291180

Raspberry Pi 1.14inch 1.3inch LCD - Python Setup User Guide

  • You can use this technique with any PiTFT, from the 240x135 mini PiTFT up to the 320x480. It isn't as fast as the kernel module support version but it'll work no matter what kernel/OS/version/etc and so is a lot less painful

Attaching

It's easy to use display breakouts with Python and the Adafruit CircuitPython RGB Display module.  This module allows you to easily write Python code to control the display.

Since the PiTFT comes preassembled, all you need to do is place it onto the GPIO pins.

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

Connect the display as shown below to your Raspberry Pi.

  • 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!
  • You can only use this technique with Linux/computer devices that have hardware SPI support, and not all single board computers have an SPI device so check before continuing

For the 1.14":

For the 1.3":

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 already installed the kernel module, you will need to remove it by editing your /boot/config.txt file before proceeding.

Python Installation of RGB Display Library

Once that's done, from your command line run the following commands:

  • sudo pip3 install adafruit-circuitpython-rgb-display
  • sudo pip3 install --upgrade --force-reinstall spidev

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!

If that complains about pip3 not being installed, then run this first to install it:

  • sudo apt-get install python3-pip

DejaVu TTF Font

Raspberry Pi usually comes with the DejaVu font already installed, but in case it didn't, you can run the following to install it:

  • sudo apt-get install ttf-dejavu

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:

  • sudo apt-get install python3-pil

NumPy Library

A recent improvement of the RGB_Display library makes use of NumPy for some additional speed. This can be installed with the following command:

  • sudo apt-get install python3-numpy

That's it. You should be ready to go.

Quickstart Button Test

This button test demo will test to make sure you have everything setup correctly. Go ahead and save the file to your Raspberry Pi in your home directory as rgb_display_minipitfttest.py.

  1. import digitalio
  2. import board
  3.  
  4. from adafruit_rgb_display.rgb import color565
  5. import adafruit_rgb_display.st7789 as st7789
  6.  
  7. # Configuration for CS and DC pins for Raspberry Pi
  8. cs_pin = digitalio.DigitalInOut(board.CE0)
  9. dc_pin = digitalio.DigitalInOut(board.D25)
  10. reset_pin = None
  11. BAUDRATE = 64000000 # The pi can be very fast!
  12. # Create the ST7789 display:
  13. display = st7789.ST7789(
  14. board.SPI(),
  15. cs=cs_pin,
  16. dc=dc_pin,
  17. rst=reset_pin,
  18. baudrate=BAUDRATE,
  19. width=135,
  20. height=240,
  21. x_offset=53,
  22. y_offset=40,
  23. )
  24.  
  25. backlight = digitalio.DigitalInOut(board.D22)
  26. backlight.switch_to_output()
  27. backlight.value = True
  28. buttonA = digitalio.DigitalInOut(board.D23)
  29. buttonB = digitalio.DigitalInOut(board.D24)
  30. buttonA.switch_to_input()
  31. buttonB.switch_to_input()
  32.  
  33. # Main loop:
  34. while True:
  35. if buttonA.value and buttonB.value:
  36. backlight.value = False # turn off backlight
  37. else:
  38. backlight.value = True # turn on backlight
  39. if buttonB.value and not buttonA.value: # just button A pressed
  40. display.fill(color565(255, 0, 0)) # red
  41. if buttonA.value and not buttonB.value: # just button B pressed
  42. display.fill(color565(0, 0, 255)) # blue
  43. if not buttonA.value and not buttonB.value: # none pressed
  44. display.fill(color565(0, 255, 0)) # green

Go ahead and run it with this command:

sudo python3 rgb_display_minipitfttest.py

Once it is running, push the buttons. The top button should make the display light up Red, the bottom Blue, and pressing both at the same time should make it Green.