ov150’s diary

言語理解凹処理速度凸のレアケース アラフォーで初めて発達障害わかったよね

raspberry pi picoとst7735rのspi接続(circuitpython)

久しぶりの電子工作勉強

しばらくご無沙汰してしまっていた電子工作の勉強を再開しました。
いまいち理解できずにいたcircuitpythonを使うととても便利であることが分かったので
最近はcircuitpythonを使っています。

出来上がり図
raspberry pi pico wにcircuitpythonをインストール

まずはcircuitpythonをダウンロード。
raspberry pi pico w用のドライバがあります(pico用とpico w用は別)。
circuitpython.org

おなじみのpico w本体のbootselボタンを押しながらPCに接続し、ダウンロードした.uf2ファイルをpico wにドラッグします。

circuitpythonエディタの入っているPCにcircuitpython bundleをダウンロード

上の工程で使ったcircuitpythonのバージョンに合わせたbundleをお使いのPCにダウンロードして解凍しておきます。
circuitpython.org
これでほぼ準備完了。

pico wとst7735rを接続

以下のように接続

st7735r pico w
CS GP5
DC GP6
RST GP9
SDA GP3
SCK GP2
VCC 3.3V
GND GND
pico wのcircuitpythonドライブ、libフォルダに必要なライブラリをコピー

とりあえずサンプルコードを動かしてみて、エラーの出たライブラリを、
PCにダウンロードしたbundleフォルダの中のlibフォルダから探して、
pico wのlibフォルダにコピー。

コード

以下のコード(adafruitのサンプルコードのピン指定を変更しています)で動く。
※import busioしてspiを指定しています。pico wはspiがたくさんあるので
元々の「board.spi」だと動きません。
※私はpico wを使いましたが、普通のpicoでも同様に動くと思います。

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
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
import busio

# Release any resources currently in use for the displays
displayio.release_displays()

spi = busio.SPI(clock=board.GP2, MOSI=board.GP3) 
tft_cs = board.GP5
tft_dc = board.GP6

display_bus = displayio.FourWire(
    spi, command=tft_dc, chip_select=tft_cs, reset=board.GP9
)

display = ST7735R(display_bus, width=160, height=128, rotation=90, bgr=True)

# Make the display context
splash = displayio.Group()
display.show(splash)

color_bitmap = displayio.Bitmap(160, 128, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00  # Bright Green

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(150, 118, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0xAA0088  # Purple
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=5, y=5)
splash.append(inner_sprite)

# 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