How to use I2C Pins in Raspberry Pi Pico | I2C Scanner Code
Use I2C Pins in Raspberry Pi Pico. SDA & SCL pin are GPIO8 & GPIO9 connected to I2C Slave. I2C Scanner Code Example for Raspberry Pi Pico
- Suitable forB.Tech/Diploma RPi Pico students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
Overview
In this tutorial, we will learn how to use
I2C Pins in Raspberry Pi Pico
& go through the
I2C Scanner Code
. The Raspberry Pi Pico is built using an
RP2040
microcontroller
. The board exposes
multi-function GPIO pins from a total of
36 GPIO pins
. The
10 GPIO Pins
are not exposed, hence they can’t be used. Out of the
26 usable GPIO pins
, there are
2 pairs
of I2C Pins which will be discussed in this post.
We will see what is
I2C Communication protocol
and how does it work. We will also learn in detail about the I2C Pin of Raspberry Pi Pico. We will take
I2C Scanner Code
as an
example
and check how the I2C Address of a few I2C enabled sensors & modules. Before that, you can check our Raspberry Pi Pico
Getting Started Tutorial
to learn more about the module.
What is I2C Communication Protocol
I2C
(Inter-Integrated Circuit) is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Like UART communication, I2C only uses two wires to transmit data between devices.
The two wires are named
Serial Clock Line (SCL)
and
Serial Data Line (SDA)
. The data to be transferred is sent through the SDA wire and is synchronized with the
clock signal
from SCL. All the devices/ICs on the
I2C network
are connected to the same SCL and SDA lines as shown above.
The devices connected to the
I2C bus
are either masters or slaves. At any instant of time, only a single master stays active on the I2C bus. It controls the
SCL clock line
and decides what operation is to be done on the
SDA data line
All the devices that respond to instructions from this master device are slaves. For differentiating between
multiple slave devices
connected to the same I2C bus, each slave device is physically assigned a permanent
7-bit address
When a master device wants to transfer data to or from a
slave device
, it specifies this particular slave device address on the
SDA line
and then proceeds with the transfer. So Effective communication takes place between the master device and a particular slave device. All the other slave devices don’t respond unless their
address
is specified by the master device on the SDA line.
I2C Pins in Raspberry Pi Pico
The microcontroller RP2040 chip has two I2C controllers. You can access both I2C controllers through GPIO pins of Raspberry Pi Pico. The following table shows the connection of GPIO pins with both I2C controllers.
I2C Controller
GPIO Pins
I2C0 – SDA
GP0/GP4/GP8/GP12/GP16/GP20
I2C0 – SCL
GP1/GP5/GP9/GP13/GP17/GP21
I2C1 – SDA
GP2/GP6/GP10/GP14/GP18/GP26
I2C1 – SCL
GP3/GP7/GP11/GP15/GP19/GP27
Each connection of the controller can be configured through multiple
GPIO pins
as shown in the figure. But before using an I2C controller, you should configure in
software
which GPIO pins you want to use with a specific I2C controller.
Features of Raspberry Pi Pico I2C Pins
The Raspberry Pi Pico has
RP2040 Chip
which supports the following features:
1. Device can work in
Master
or
Slave Mode
with a default salve address =
0x055
2. I2C Pins have 3 speed modes: Standard (
0 to 100 Kb/s
), Fast(
<= 400 Kb/s
) & Fast Plus mode (
<= 1000 Kb/s
)
3. It can both transmit and Receive
Buffers
4. It can also be used in
interrupt
and
DMA
mode
How to use I2C Pins of Raspberry Pi Pico with I2C Sensors or Modules?
Now let us learn how we can use the
I2C Pin
of
Raspberry Pi Pico
with any other I2C Based Sensors or Modules. In this case, we can use Raspberry Pi Pico as a
Mater Device
& other external sensors or modules as a
Slave Device
Here is a circuit where we have attached
different I2C Devices to the Pico Board. The 3 devices are
BME680
Sensor,
MPU6050
Sensor & 0.96″ OLED Display.
In this example, we will connect the
SDA & SCL
pin of
MPU6050
BME680
&
OLED Display
to the common I2C line of Raspberry Pi Pico. Since there are
multiple I2C Pins
, we will use only a single pair of I2C pins of Raspberry Pi Pico. We will use
GPIO8
as
SDA0
&
GPIO9
as
SCL0
Raspberry Pi Pico I2C Scanner Code
Now let us see the
Raspberry Pi Pico I2C Scanner Code
. The code is written in MicroPython. You can either use
Thonny IDE
or
uPyCraft IDE
to connect the Raspberry Pi Pico to your computer.
The following code will scan the I2C Address of all the Sensors connected to the I2C pin of Raspberry Pi Pico. Copy the code and then Download & Run.
import
machine
sda
=
machine
Pin
(
)
scl
=
machine
Pin
(
)
i2c
=
machine
I2C
(
sda
=
sda
scl
=
scl
freq
=
400000
)
(
'Scan i2c bus...'
)
devices
=
i2c
scan
(
)
if
len
(
devices
)
==
:
(
"No i2c device !"
)
else
:
(
'i2c devices found:'
len
(
devices
)
)
for
device
in
devices
:
(
"Decimal address: "
device
" | Hexa address: "
hex
(
device
)
)
Once you run the code, the Micropython will try scanning the
I2C devices
connected to Pico Board. You can connect up to
127 I2C Slave devices
. The device will scan the address and will show it in the Shell Window.
1. The
I2C Address
of
OLED Display
is
which in Hexadecimal is
0x3C
2. The
I2C Address
of
MPU6050
is
which in Hexadecimal is
0x68
3. The
I2C Address
of
BME680
is
which in Hexadecimal is
0x77
Video Tutorial & Guide
Raspberry Pi Pico Complete Guide| Pinout+Features+ADC+I2C+OLED+Internal Temperature Sensor+DHT11
Watch this video on YouTube
In this tutorial, we will learn how to use
I2C Pins in Raspberry Pi Pico
& go through the
I2C Scanner Code
. The Raspberry Pi Pico is built using an
RP2040
microcontroller
. The board exposes
multi-function GPIO pins from a total of
36 GPIO pins
. The
10 GPIO Pins
are not exposed, hence they can’t be used. Out of the
26 usable GPIO pins
, there are
2 pairs
of I2C Pins which will be discussed in this post.
We will see what is
I2C Communication protocol
and how does it work. We will also learn in detail about the I2C Pin of Raspberry Pi Pico. We will take
I2C Scanner Code
as an
example
and check how the I2C Address of a few I2C enabled sensors & modules. Before that, you can check our Raspberry Pi Pico
Getting Started Tutorial
to learn more about the module.
What is I2C Communication Protocol
I2C
(Inter-Integrated Circuit) is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Like UART communication, I2C only uses two wires to transmit data between devices.
The two wires are named
Serial Clock Line (SCL)
and
Serial Data Line (SDA)
. The data to be transferred is sent through the SDA wire and is synchronized with the
clock signal
from SCL. All the devices/ICs on the
I2C network
are connected to the same SCL and SDA lines as shown above.
The devices connected to the
I2C bus
are either masters or slaves. At any instant of time, only a single master stays active on the I2C bus. It controls the
SCL clock line
and decides what operation is to be done on the
SDA data line
All the devices that respond to instructions from this master device are slaves. For differentiating between
multiple slave devices
connected to the same I2C bus, each slave device is physically assigned a permanent
7-bit address
When a master device wants to transfer data to or from a
slave device
, it specifies this particular slave device address on the
SDA line
and then proceeds with the transfer. So Effective communication takes place between the master device and a particular slave device. All the other slave devices don’t respond unless their
address
is specified by the master device on the SDA line.
I2C Pins in Raspberry Pi Pico
The microcontroller RP2040 chip has two I2C controllers. You can access both I2C controllers through GPIO pins of Raspberry Pi Pico. The following table shows the connection of GPIO pins with both I2C controllers.
I2C Controller
GPIO Pins
I2C0 – SDA
GP0/GP4/GP8/GP12/GP16/GP20
I2C0 – SCL
GP1/GP5/GP9/GP13/GP17/GP21
I2C1 – SDA
GP2/GP6/GP10/GP14/GP18/GP26
I2C1 – SCL
GP3/GP7/GP11/GP15/GP19/GP27
Each connection of the controller can be configured through multiple
GPIO pins
as shown in the figure. But before using an I2C controller, you should configure in
software
which GPIO pins you want to use with a specific I2C controller.
Features of Raspberry Pi Pico I2C Pins
The Raspberry Pi Pico has
RP2040 Chip
which supports the following features:
1. Device can work in
Master
or
Slave Mode
with a default salve address =
0x055
2. I2C Pins have 3 speed modes: Standard (
0 to 100 Kb/s
), Fast(
<= 400 Kb/s
) & Fast Plus mode (
<= 1000 Kb/s
)
3. It can both transmit and Receive
Buffers
4. It can also be used in
interrupt
and
DMA
mode
How to use I2C Pins of Raspberry Pi Pico with I2C Sensors or Modules?
Now let us learn how we can use the
I2C Pin
of
Raspberry Pi Pico
with any other I2C Based Sensors or Modules. In this case, we can use Raspberry Pi Pico as a
Mater Device
& other external sensors or modules as a
Slave Device
Here is a circuit where we have attached
different I2C Devices to the Pico Board. The 3 devices are
BME680
Sensor,
MPU6050
Sensor & 0.96″ OLED Display.
In this example, we will connect the
SDA & SCL
pin of
MPU6050
BME680
&
OLED Display
to the common I2C line of Raspberry Pi Pico. Since there are
multiple I2C Pins
, we will use only a single pair of I2C pins of Raspberry Pi Pico. We will use
GPIO8
as
SDA0
&
GPIO9
as
SCL0
Raspberry Pi Pico I2C Scanner Code
Now let us see the
Raspberry Pi Pico I2C Scanner Code
. The code is written in MicroPython. You can either use
Thonny IDE
or
uPyCraft IDE
to connect the Raspberry Pi Pico to your computer.
The following code will scan the I2C Address of all the Sensors connected to the I2C pin of Raspberry Pi Pico. Copy the code and then Download & Run.
import
machine
sda
=
machine
Pin
(
)
scl
=
machine
Pin
(
)
i2c
=
machine
I2C
(
sda
=
sda
scl
=
scl
freq
=
400000
)
(
'Scan i2c bus...'
)
devices
=
i2c
scan
(
)
if
len
(
devices
)
==
:
(
"No i2c device !"
)
else
:
(
'i2c devices found:'
len
(
devices
)
)
for
device
in
devices
:
(
"Decimal address: "
device
" | Hexa address: "
hex
(
device
)
)
Once you run the code, the Micropython will try scanning the
I2C devices
connected to Pico Board. You can connect up to
127 I2C Slave devices
. The device will scan the address and will show it in the Shell Window.
1. The
I2C Address
of
OLED Display
is
which in Hexadecimal is
0x3C
2. The
I2C Address
of
MPU6050
is
which in Hexadecimal is
0x68
3. The
I2C Address
of
BME680
is
which in Hexadecimal is
0x77
Video Tutorial & Guide
Raspberry Pi Pico Complete Guide| Pinout+Features+ADC+I2C+OLED+Internal Temperature Sensor+DHT11
Watch this video on YouTube
Technologies used
Hardware components
- Raspberry Pi
- DHT11
- OLED
- MPU6050
Software & tools
- Thonny
- MicroPython
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.