# Which are the SPI pins on Tiny 2040

Hello community, I’m new here!

I’m trying to understand which pins on the Tiny 2040 are equivalent to Pi Pico. Previously I was able to initialize SPI on the PI Pico with sck=Pin(10) and mosi=Pin(11), this works. According to the pins diagram SCK and MOSI are GP2 and GP0, right? So I modified my code this way:

``````#spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
#          sck=Pin(10), mosi=Pin(11), miso=None)
spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
sck=Pin(2), mosi=Pin(0), miso=None)
``````

But when I run that code Thonny shows this error:

``````Traceback (most recent call last):
File "<stdin>", line 9, in <module>
``````

There is a pinout diagram on the shop page.

Thanks! That pinout diagram is helpful. MOSI is TX, facepalm! So it seems these are the corresponding pins (GPxx):

Pi Pico: GP10 = SPI1 SCK —> Tiny 2040: GP2 = SPI0 SCK
Pi Pico: GP11 = SPI1 TX -----> Tiny 2040: GP3 = SPI0 TX

And the code is now:

``````#spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
#          sck=Pin(10), mosi=Pin(11), miso=None)
spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
sck=Pin(2), mosi=Pin(3), miso=None)
``````

But it still throws a “bad SCK pin” error. I believe the problem is with the Pin() constructor, it tries to map GP2 to a Pi Pico board pin, which is different from the Tiny 2040 board pin. Maybe the only way around is finding firmware for the Tiny 2040, or modify it to map the right pins.

Any ideas where could I find the code?

Try using the pin numbers listed in grey.

Did you get this working? I am having troubles with spi too.

Carlos

I have a Tiny 2040, but no easy way to hook up that display, or I’d have a go at getting it to work. I have several Pimoroni SPI LCD Breakouts but none have a male header soldered on. Well, the ones that do are already soldered to a Proto board and wired up to a Pi Zero.
I also have the PICO Breakout Garden Base, but can’t plug a Tiny into it as it has a different pinout to a stock PICO.
I will be ordering some of the Breakout garden Extenders though to get around this problem in the future. Might be a while until I get them though.
I will be following this to see if anybody figures it out.

I did finally get the Adafruit 1.54" 240x240 Wide Angle TFT LCD Display working by wiring according to:

``````TFT                                 Tiny
---------------------------------------
V+                                  V+
GND                                 GND
Clock                               GP2
MOSI                                GP3
TC                                  RX - GP1
RT                                  TX - GP0
DC                                  SCL - GP7
BL                                  SDA - GP6
``````

Hope this helps

Would you be willing to post the code that you got to work with the Adafruit 1.54" 240x240 Wide Angle ?

Or, if anyone has a working set of C or Micropython code that would drive a SPI MAX7219 based chain of 4 8x8 displays?

This code is from the original Overview | Kitty Toe Bean Keypad with Color TFT | Adafruit Learning System, with modifications for setting up the SPI interface. The appears to be a bug in CircuitPython, it is being worked on. It works fine when all the code for the TFT is commented out. It works fine when all the Keyboard stuff is commented out but give an error when both are configured:

``````224
25
Traceback (most recent call last):
File "code.py", line 124, in <module>
File "adafruit_hid/keyboard.py", line 120, in send
File "adafruit_hid/keyboard.py", line 90, in press
TypeError: function takes 1 positional arguments but 2 were given

----------------------------------------------------------------------------------
import board
import busio
import displayio
import digitalio
import usb_hid
import usb_midi

#  if you want to use this as an HID keyboard, set keyboard_mode to True
#  otherwise, set it to False
keyboard_mode = True
#  if you want to use this as a MIDI keyboard, set midi_mode to True
#  otherwise, set it to False
midi_mode = False

#  change keyboard shortcuts here
#  defaults are shortcuts for save, cut, copy & paste
#  comment out ctrl depending on windows or macOS
if keyboard_mode:
keyboard = Keyboard(usb_hid.devices)
#  modifier for windows
ctrl = Keycode.CONTROL
#  modifier for macOS
#  ctrl = Keycode.COMMAND
key0 = Keycode.S
key1 = Keycode.X
key2 = Keycode.C
key3 = Keycode.V
shortcuts = [key0, key1, key2, key3]

#  change MIDI note numbers here
if midi_mode:
midi_notes = [60, 61, 62, 63]

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

#  spi display setup
#spi = board.SPI()
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3)
tft_cs = board.GP1
tft_dc = board.GP7

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

#  display setup
display = ST7789(display_bus, width=240, height=240, rowstart=80)

#  bitmap setup
bitmap = displayio.OnDiskBitmap(open("/parrot-240-sheet.bmp", "rb"))

# Create a TileGrid to hold the bitmap
width=1, height=1,
tile_height=240, tile_width=240,
default_tile=0,
x=0, y=0)

# Create a Group to hold the TileGrid
group = displayio.Group()

# Add the TileGrid to the Group
group.append(parrot0_grid)

# Add the Group to the Display
display.show(group)

#  digital pins for the buttons
key_pins = [board.A0, board.A1, board.A2, board.A3]

#  array for buttons
keys = []

#  setup buttons as inputs
for key in key_pins:
key_pin = digitalio.DigitalInOut(key)
key_pin.direction = digitalio.Direction.INPUT
key_pin.pull = digitalio.Pull.UP
keys.append(key_pin)

p = 0 #  variable for tilegrid index
a = 0 #  variable for tile position

#  states for buttons
key0_pressed = False
key1_pressed = False
key2_pressed = False
key3_pressed = False

#  array for button states
key_states = [key0_pressed, key1_pressed, key2_pressed, key3_pressed]

while True:
#  default tile grid position
parrot0_grid[a] = p

#  iterate through 4 buttons
for i in range(4):
inputs = keys[i]
#  if button is pressed...
if not inputs.value and key_states[i] is False:
#  tile grid advances by 1 frame
p += 1
#  update button state
key_states[i] = True
#  if a midi keyboard...
if midi_mode:
#  send NoteOn for corresponding MIDI note
midi.send(NoteOn(midi_notes[i], 120))
#  if an HID keyboard...
if keyboard_mode:
#  send keyboard output for corresponding keycode
#  the default includes a modifier along with the keycode
print(ctrl)
print(shortcuts[i])
keyboard.send(ctrl, shortcuts[i])
print(keyboard.send(ctrl, shortcuts[i]))
#  if the tile grid's index is at 9...
if p > 9:
#  reset the index to 0
p = 0
#  if the button is released...
if inputs.value and key_states[i] is True:
#  update button state
key_states[i] = False
#  if a midi keyboard...
if midi_mode:
#  send NoteOff for corresponding MIDI note
midi.send(NoteOff(midi_notes[i], 120))

``````

Again, this is for CircuitPython setup, not MicroPython

Appreciated! Thanks!

The bug appears to be in version 7 of circuitpython. Use the latest 6.x version

I was able to load CP 6.x and it worked fine

The CircuitPython bug has been identified and fixed

# OnDiskBitmap improvements #5017

I am a a bit late with a response, but this may help someone else seeing the same error.

The issue is with the software wanting to use hardware SPI on the RP2040. The first argument to SPI() is the hardware SPI identifier. The code is calling as “SPI(1, …” indicating hardware for SPI1 is to be utilized. Looking at the pin diagram, only SPI0 is available on the selected IO pins. Changing the call to “SPI(0, …” will resolve the bad SCK pin exception.

Try

``````spi = SPI(0, baudrate=2000000, polarity=0, phase=0,
sck=Pin(2), mosi=Pin(3), miso=None)
``````

Enjoy!
Bill

hello folks, anyone able to connect the ST7789 (240x240, without CS pin) display to the Tiny 2040 using MicroPython yet? I used a working Pico example and changed the pins, nothing worked.