主页 Swift UNIX C Assembly Go Plan 9 Web MCU Research Non-Tech

Modify the default SDA and SCL pins of I2C of Raspi Pico using C/C++

2023-08-26 | MCU | #Words: 254

Pico has two I2C, namely two sets of SDA and SCL. You can see this from the names in the pin diagram. By default, only the first I2C (I2C0) is enabled. It means, even if you modify the pin, but not in I2C0, it still doesn’t work.

There are two ways to modify the SDA and SCL default pins of I2C correctly, and also there is a not recommended method (but still a useful method).

The most recommended is to set it in CMakeLists.txt, because this part controls all the code of project, which can avoid being replaced some settings for library in the SDK:

set(PICO_DEFAULT_I2C 1)
set(PICO_DEFAULT_I2C_SDA_PIN 18)
set(PICO_DEFAULT_I2C_SCL_PIN 19)

Another method is modifying in the source code, which is not recommended because it may be replaced by something. Place the following code after import library. But the replacement may be replaced cover again:

#define PICO_DEFAULT_I2C 1
#define PICO_DEFAULT_I2C_SDA_PIN 18
#define PICO_DEFAULT_I2C_SCL_PIN 19

The subsequent code will use this value instead of the default 4 and 5.

The default value is set in pico-sdk/blob/master/src/boards/include/boards/pico.h. The code part for setting the default value is as follows:

#ifndef PICO_DEFAULT_I2C_SDA_PIN
#define PICO_DEFAULT_I2C_SDA_PIN 4
#endif
#ifndef PICO_DEFAULT_I2C_SCL_PIN
#define PICO_DEFAULT_I2C_SCL_PIN 5
#endif

Of course, you can modify the default value directly. But it is not recommended, it will make a mess.

I hope these will help someone in need~