Recently wanted to know the default pins for I2C & SPI when using Micropython.
The Raspberry Pi Pico documentation lists GPIO 4 & 5 for I2C0 and GPIO 16, 17, 18 & 19 for SPI0.
Came across the Python code below and the default pins are different.
from machine import I2C, SPI
print(SPI(0))
print(SPI(1))
print(I2C(0))
print(I2C(1))
Hi All,
Just had a quick read.
Surely the default pins for I2C and the like are a function of the device in use (RPi, Arduino etc) and not the programming software or the slave devices.
I canāt see what Python or Micropython would have to do with it.
Maybe I am wrong. I think you can nominate some pins for this function but that is not really ādefaultā I donāt think.
Cheers Bob
Yes, there are a few pins on the Pico thatāre designed specifically to be the default pins for SPI/I2C. And in most Micropython builds it is configured so that when you donāt specify pins to use for an SPI/I2C bus pins 8 and 9 for data and clock automatically get used.
However, as Python is an interpreted language rather than compiled into OP codes for some specific hardware like C for a PIC chip, you can actually modify the UF2 firmware package generated to use different pins as the defaults instead without needing to specify it in your script:
You can nominate pins as in the list I gave like this
myi2c = I2C(id=1, scl=11 sda=10)
which will also take the I2C library defined frequency of 4000000.
I think that you might be allowed to do something like this (not tried it though)
myi2c = I2C(id=1, scl=11, sda=26)
i.e. āsplitā the pins that are usually paired ( the side-by-side pins )
You still have to use the appropriate i2c hardware controller so you cannot do this
myi2c = I2C(id=0, scl=15,sda=14)
as those pins ābelongā to the i2c1 controller internally
There is also a SoftI2C library defined that lets you pick any GPIO pins ā¦
(and I think that there is code somewhere that can be run within the PIO subsystem to create an i2c bus protocol too)
Hi Murray
Thanks for that Will tuck this info away in the old grey thing until I decide to try my hand at a Pico. One of these days I will do it. Have got a couple but havenāt tried in earnest yet.
Cheers Bob
The RP2040 is an amazing micro and very flexible in pin assignments. Each GPIO pin has multiple functions determined by the software.
The micropython library I was using on a Pico defined the Serial Peripheral Interface as SPI(0). I wondered what pins it was using so I could get the wiring right.
That was when I discovered the difference between the Raspberry Pi documentation and what I thought the Micropython defaults were. The python code I listed above is an easy and simple way of determining the pins when not defined in the library.
Thought it may be of use to someone else and is a āgotchaā to check it your code does not work. Could just be a simple wiring error.
For reference re the Micropython defaults in the i2c and SPI libraries.
Section 3.6 gives the i2c generic usage, and also details the Micropython ādefaultsā for i2c.
See Table 2 on page 16
Similarly Section 3.7 gives the generic SPI usage, and also the Micropython ādefaultsā for SPI
See Table 3 on page 16
If you really want to dig into the RP2040 microprocessor on the Pico boards try thisā¦
It is VERY detailed, is 639 pages long (i,e, 20+ pages on i2c configuration and protocol management), and brain-hurting , so it is way easier to take the setup provided by the authors of the Micropython libraries.
Murray
3 Likes
And you can get our latest projects and tips straight away by following us on: