I2C Connection

日本語のページ

It is very common to connect various sensors and other devices using I2C.
It has the advantage of being relatively easy to use, as you only need to connect four lines: power (VCC/GND), data (SDA), and clock (SCL).
This page explains how to connect with I2C.

Pins used for I2C connection

Depending on the microcontroller, the pins used for I2C connection (SDA/SCL) may be fixed, or you may be able to freely set the pin assignment.

Microcontrollers with fixed pins

The SDA/SCL pins are fixed on the following microcontrollers.

MicrocontrollerSDA pinSCL pinPin position
ESP32 (unbranded)GPIO21GPIO22See GPIO21/22 in this diagram
Raspberry PiGPIO2GPIO3See GPIO2/3 in this diagram
Raspberry Pi Pico (not W)GP4GP5See GP4/5 in this diagram

If you are using a microcontroller with fixed SDA/SCL pins and an I2C-connected module, use a block that does not specify a pin number during initialization.
For example, in the case of the temperature/pressure sensor BMP280, use the following block.

Initialize

Microcontrollers with free pin assignments

In the following microcontrollers, you can freely change the pins assigned to SDA/SCL.

  • ESP32-S3
  • ESP32-C3
  • Raspberry Pi Pico W

When using I2C-connected modules with these microcontrollers, use a block that specifies the pin number during initialization.

For example, in the case of the temperature/pressure sensor BMP280, use the following block.

Specify the SCL / SDA pin number and initialize