Raspberry Pi Pico W/MicroPython x 0.96" 80x160 ST7735 SPI IPS

This post show how to display on 0.96" 80x160 ST7735 SPI IPS, with Raspberry Pi Pico W/MicroPython, using boochow/MicroPython-ST7735 library. The original boochow/MicroPython-ST7735 target 128x160 ST7735 TFT LCD driver for MicroPython. This post show steps to install the library, and modify it to fix screen size, offset and color. Please note that the modification base on my own guessing and trying, not any official suggested approach.


Connection:

	ST7735	Raspberry Pi Pico W
	---------------------------
	BLK		3V3
	CS		GP15
	DC		GP14
	RES		GP13
	SDA		GP11
	SCL		GP10
	VCC		3V3
	GND		GND


Copy and edit ST7735.py, save to to MicroPython device, name it myST7735.py.


...

ScreenSize = (80, 160)

...

  def setStart(self, colstart, rowstart):
      self._offset[0] = colstart
      self._offset[1] = rowstart

  def initr( self ) :
    '''Initialize a red tab version.'''
    self._reset()
    self.setStart(26, 1)

...

Copy sysfont.py from GuyCarver MicroPython library, save in MicroPython device.
https://github.com/GuyCarver/MicroPython/blob/master/lib/sysfont.py

Exercise code:

mpyPicoW_graphicstest_80x160.py, modified from graphicstest.py.
from myST7735 import TFT
from sysfont import sysfont
from machine import SPI,Pin
import time
import math

#=== prepare ST7735 SPI 80x160
#spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
spi = SPI(1, baudrate=20000000, polarity=0, phase=0)
pin_DC = 14
pin_RESET = 13
pin_CS = 15
tft=TFT(spi, pin_DC, pin_RESET, pin_CS)
tft.initr()
tft.rgb(False)
tft.invertcolor(True)
#===

def testlines(color):
    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((0,0),(x, tft.size()[1] - 1), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((0,0),(tft.size()[0] - 1, y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((tft.size()[0] - 1, 0), (x, tft.size()[1] - 1), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((tft.size()[0] - 1, 0), (0, y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((0, tft.size()[1] - 1), (x, 0), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((0, tft.size()[1] - 1), (tft.size()[0] - 1,y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((tft.size()[0] - 1, tft.size()[1] - 1), (x, 0), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((tft.size()[0] - 1, tft.size()[1] - 1), (0, y), color)

def testfastlines(color1, color2):
    tft.fill(TFT.BLACK)
    for y in range(0, tft.size()[1], 5):
        tft.hline((0,y), tft.size()[0], color1)
    for x in range(0, tft.size()[0], 5):
        tft.vline((x,0), tft.size()[1], color2)

def testdrawrects(color):
    tft.fill(TFT.BLACK);
    for x in range(0,tft.size()[0],6):
        tft.rect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color)

def testfillrects(color1, color2):
    tft.fill(TFT.BLACK);
    for x in range(tft.size()[0],0,-6):
        tft.fillrect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color1)
        tft.rect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color2)


def testfillcircles(radius, color):
    for x in range(radius, tft.size()[0], radius * 2):
        for y in range(radius, tft.size()[1], radius * 2):
            tft.fillcircle((x, y), radius, color)

def testdrawcircles(radius, color):
    for x in range(0, tft.size()[0] + radius, radius * 2):
        for y in range(0, tft.size()[1] + radius, radius * 2):
            tft.circle((x, y), radius, color)

def testtriangles():
    tft.fill(TFT.BLACK);
    color = 0xF800
    w = tft.size()[0] // 2
    x = tft.size()[1] - 1
    y = 0
    z = tft.size()[0]
    for t in range(0, 15):
        tft.line((w, y), (y, x), color)
        tft.line((y, x), (z, x), color)
        tft.line((z, x), (w, y), color)
        x -= 4
        y += 4
        z -= 4
        color += 100

def testroundrects():
    tft.fill(TFT.BLACK);
    color = 100
    for t in range(5):
        x = 0
        y = 0
        w = tft.size()[0] - 2
        h = tft.size()[1] - 2
        for i in range(17):
            tft.rect((x, y), (w, h), color)
            x += 2
            y += 3
            w -= 4
            h -= 6
            color += 1100
        color += 100

def tftprinttest():
    tft.fill(TFT.BLACK);
    v = 30
    tft.text((0, v), "Hello World!", TFT.RED, sysfont, 1, nowrap=True)
    v += sysfont["Height"]
    tft.text((0, v), "Hello World!", TFT.YELLOW, sysfont, 2, nowrap=True)
    v += sysfont["Height"] * 2
    tft.text((0, v), "Hello World!", TFT.GREEN, sysfont, 3, nowrap=True)
    v += sysfont["Height"] * 3
    tft.text((0, v), str(1234.567), TFT.BLUE, sysfont, 4, nowrap=True)
    time.sleep_ms(1500)
    tft.fill(TFT.BLACK);
    v = 0
    tft.text((0, v), "Hello World!", TFT.RED, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), str(math.pi), TFT.GREEN, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " Want pi?", TFT.GREEN, sysfont)
    v += sysfont["Height"] * 2
    tft.text((0, v), hex(8675309), TFT.GREEN, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " Print HEX!", TFT.GREEN, sysfont)
    v += sysfont["Height"] * 2
    tft.text((0, v), "Sketch has been", TFT.WHITE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), "running for: ", TFT.WHITE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), str(time.ticks_ms() / 1000), TFT.PURPLE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " seconds.", TFT.WHITE, sysfont)

def test_main():
    tft.fill(TFT.BLACK)
    tft.text((0, 0), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", TFT.WHITE, sysfont, 1)
    time.sleep_ms(1000)

    tftprinttest()
    time.sleep_ms(4000)

    testlines(TFT.YELLOW)
    time.sleep_ms(500)

    testfastlines(TFT.RED, TFT.BLUE)
    time.sleep_ms(500)

    testdrawrects(TFT.GREEN)
    time.sleep_ms(500)

    testfillrects(TFT.YELLOW, TFT.PURPLE)
    time.sleep_ms(500)

    tft.fill(TFT.BLACK)
    testfillcircles(10, TFT.BLUE)
    testdrawcircles(10, TFT.WHITE)
    time.sleep_ms(500)

    testroundrects()
    time.sleep_ms(500)

    testtriangles()
    time.sleep_ms(500)

test_main()

tft.rotation(1)
tft.setStart(1, 26)
test_main()

mpyPicoW_ST7735_color.py, to verify color.
"""
Raspberry Pi Pico W/MicroPython
display on 0.96" 80x160 (RGB) IPS with ST7735S SPI Driver
- Color Test
"""
from myST7735 import TFT
from sysfont import sysfont
from machine import SPI
import time
import math
import sys
import os
#=== prepare ST7735 TFT
#spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
spi = SPI(1, baudrate=20000000, polarity=0, phase=0)
pin_DC = 14
pin_RESET = 13
pin_CS = 15
#tft=TFT(spi,16,17,18)
tft=TFT(spi, pin_DC, pin_RESET, pin_CS)
tft.initr()
tft.rgb(False)
tft.invertcolor(True)
#===

def test_msg():
    tft.rotation(1)
    tft.setStart(1, 26)
    
    tft_width = tft.size()[0]
    tft_height = tft.size()[1]
    print("TFT size =", tft_width, tft_height)
    
    tft.fill(TFT.WHITE)
    
    msg = "Color test on 0.96\" 80x160 ST7735 SPI TFT"
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.BLACK)
    tft.text((10, 10), msg, TFT.WHITE, sysfont, 2, nowrap=False)
    time.sleep(3)
    
    msg = sys.implementation[0] + " " + os.uname()[3]
    #msg = sys.implementation[0], os.uname()[3], "\nrun on", os.uname()[4]
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.BLACK)
    tft.text((10, 10), msg, TFT.WHITE, sysfont, 2, nowrap=False)
    time.sleep(3)
    
    msg = "run on " + os.uname()[4]
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.BLACK)
    tft.text((10, 10), msg, TFT.WHITE, sysfont, 2, nowrap=False)
    time.sleep(3)

def test_color():
    tft.rotation(0)
    tft.setStart(26, 1)
    
    tft_width = tft.size()[0]
    tft_height = tft.size()[1]
    print("TFT size =", tft_width, tft_height)
    
    tft.fill(TFT.WHITE)
    time.sleep(0.5)
    
    print("BLACK")
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.BLACK)
    tft.text((10, 10), "BLACK", TFT.WHITE, sysfont, 2, nowrap=False)
    time.sleep(1)
    
    print("RED")
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.RED)
    tft.text((10, 10), "RED", TFT.WHITE, sysfont, 2, nowrap=True)
    time.sleep(1)
    
    print("GREEN")
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.GREEN)
    tft.text((10, 10), "GREEN", TFT.WHITE, sysfont, 2, nowrap=True)
    time.sleep(1)
    
    
    print("BLUE")
    tft.fillrect([1,1], [tft_width-2, tft_height-2], TFT.BLUE)
    tft.text((10, 10), "BLUE", TFT.WHITE, sysfont, 2, nowrap=True)
    time.sleep(1)
    

while True:
    test_msg()
    test_color()


Related:
80x160 ST7735S SPI TFT with Raspberry Pi Pico W/CircuitPython 8 Beta 5

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator