#ST7735R example, object has no attribute 'SPI'
1 messages · Page 1 of 1 (latest)
Example code is attached. Copied straight from the ST7735R library provided by Adafruit.
It seems there's something wrong with how it's defining SPI in the board file.
I cannot get this to run in Thonny, PyCharm, or VS Code.
I've downloaded the latest Thonny 4.0.2, which now seems to install plugins and packages properly. I manually copied over the Adafruit_display_text and st7735r drivers to the Pico clone. It keeps throwing this error:
Traceback (most recent call last):
File "<stdin>", line 18, in <module>
AttributeError: 'module' object has no attribute 'SPI'
It happens right at line 16:
spi = board.SPI()
Some boards have built in SPI interface. Others need you to define it like:
import busio
spi = busio.SPI(board.SCK, MISO=board.MISO)
What board are you using?
It's a Waveshare RP2040-LCD-0.96 board, which is a Pico clone.
import board
import busio
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4)
Okay, that seems to be exactly what I'm looking for! I have dozens of tabs open in my browser hunting for info on this stuff, and you're the first one to mention this. It seems to be exactly what I'm running into.
Thanks! I'll give this a try.
That page explains that the Pico does not have preset interfaces for I2C, SPI and UART. Because everything is GPIO.
I noticed that. Very interesting. It also makes sense, as the Pico has multiple pins that can act as SPI, I2C, or UART pins.
So you have to choose which ones you need.
A little odd that the example code doesn't follow the one on the page you listed...
With great power, comes great responsibility.
Most Adafruit boards have a built-in SPI and I2C
I wonder if the spi = board.SPI() could accept the variables you mentioned and pass them to the SPI code in the board.py...
board.py mentions it checks for local variables.
if "SCLK" in locals() and "MOSI" in locals() and "MISO" in locals():
def SPI():
"""The singleton SPI interface"""
import busio
return busio.SPI(SCLK, MOSI, MISO)
But if I supply the pins in the main code to the board.SPI() function, does it pass it down into board.py's SPI function to check and assign the pins?
(If you can't tell, I'm VERY green at this...)
Are you defining your own board?
No, I'm not.
board is built in to CP for each different type
I see board.py checks for the board type automatically, so it can grab pin assignments and such.
I'm just confused because the SPI code in the provided example with the ST7735R library doesn't match the SPI example on the Adafruit page you shared.
Because most boards have a predefined SPI. If you do:
import board
dir(board)
``` in REPL, you will see the predefined pins.
The example uses board.SPI() and the example you shared uses busio.SPI(XYZ).
(Sub XYZ for the pin assignments.)
You can replace the line in the example with the line I mentioned.
Everything else should work.
Great, I'll give that a shot. Thanks for being patient while I wrap my head around this.
But I recall that you have a board with a built in TFT. So, you may need to double check the data sheet to make sure what pins that is connected to.