Micropython Default I2C & SPI pins

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))

Results:

SPI(0, baudrate=992063, polarity=0, phase=0, bits=8, sck=6, mosi=7, miso=4)
SPI(1, baudrate=992063, polarity=0, phase=0, bits=8, sck=10, mosi=11, miso=8)
I2C(0, freq=399361, scl=9, sda=8)
I2C(1, freq=399361, scl=7, sda=6)

Useful if you need to know.
Unsure what Circuit Python defaults are, I don’t use it.

Regards
Jim

1 Like

Hi @James46717

I would go with the defaults you have found for the i2c pins. (PiicoDev default is i2c0, sda(8), scl(9))

Note that you can select any of the designated pins for the appropriate i2c bus from the sets below

i2c0   sda  gp8(default),   gp0,gp4,gp12,gp16,gp20
       scl  gp9(default),   gp1,gp5,gp13,gp17,gp21
i2c1   sda  gp6(default),   gp2,gp10,gp14,gp18,gp26
       scl  gp7(default),   gp3,gp11,gp15,gp19,gp27

Murray

2 Likes

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

2 Likes

Hey 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:

2 Likes

Hi Bob

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)

Murray

1 Like

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

Hi @James46717 et al,

And as ‘final’ comments, I would say that the SPI interface has the same general hardware configuration

  • 2 hardware controllers
  • a set of possible groups of related pins
  • a Micropython predefined ‘defaults’ in the general SPI machine library

Overall the same as the I2C hardware / default pins / library setup.

Murray

2 Likes

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.

Thanks for all the responses.
Cheers
Jim

1 Like

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 :exploding_head:, so it is way easier to take the setup provided by the authors of the Micropython libraries.

Murray

3 Likes