I2C - Raspberry Pi, Pico and an Arduino

Опубликовано: 04 Октябрь 2024
на канале: Penguin Tutor
14,479
223

In this video I show how you can communicate between various devices using I2C serial communications. The example shows a Raspberry Pi as the controller and a Raspberry Pi Pico and an Arduino as peripheral devices.


I2C is sometimes referred to as a Master, Slave relationship. This is now considered by many to be inappropriate as it is trivialising the suffering of slaves both historically and through modern day slavery affecting vulnerable people around the world. In addition the terminology isn't always correct.

I have therefore used the terms controller to indicate the computer that is controlling the I2C bus, and peripheral device to indicate the device that the controllers communicates with.

The main difference between SPI and I2C is that I2C needs only two wires (plus ground) whereas SPI needs 4 and an additional enable port on the controller for any additional devices. Some other differences are covered in more detail in the video above.
Pull-up resistors and voltage differences

I2C is implemented using open drain connections. This means that pull-up resistors are needed for both the SDA (data) and SCL (clock) lines. These are typically between about 2kΩ and 5kΩ.

If using different voltages, then it is important not to allow a 5V signal (eg. Arduino) to go to a device for 3.3V (eg. Raspberry Pi or Pico). This can be achieved by connecting only to the 3.3V supply, but I recommend instead using a 3.3V to 5V MOSFET logic level shift circuit for voltage protection.

Each peripheral must have a unique address. These are sometimes created in software and sometimes in hardware:
Software (used here allows for flexibility)
DIP switches / jumpers (limited address range, but good flexibility
Solder pads / tracks (difficult to change more than once
Fixed address (no flexibility - not recommended).
I2C from the Raspberry Pi to the Pico

The Raspberry Pi computer and Raspberry Pi Pico microcontroller both work at 3.3V. This makes connecting them together easy using:
SDA - Raspberry Pi GPIO 2 to Pico GP2
SCL - Raspberry Pi GPIO 3 to Pico GP3
Gnd - Raspberry Pi Gnd to Pico Gnd
I2C on the Raspberry Pi and the Arduino

Warning! The Raspberry Pi and Arduino work may work at different voltages.

The I2C ports on the Raspberry Pi are 3.3V only. They can be damaged by if a peripheral device pull-up resistor raises the bus to 5V. If connecting to a 5V device then a level shifter is recommended. The following can be used from a Raspberry Pi to an Arduino:
SDA - Raspberry Pi GPIO 2 to Arduino A4
SCL - Raspberry Pi GPIO 3 to Arduino A5
Gnd - Raspberry Pi Gnd to Pico Gnd
Software implementation of I2C


I had some difficulties with getting I2C working due to differences in the implementations of I2C. In particular the Python that I used on the Raspberry Pi is for SMBus (System Management Bus) which is based on I2C but not the same. I needed to use different methods to retrieve data from the Pico compared to the Arduino. In both cases I only transferred data to the peripherals 2 bytes at a time, and retrieve data 2 bytes from the Pico and 1 byte from the Arduino at a time.



For more details including the source code for the examples see:
http://www.penguintutor.com/electroni...


For more details of the logic-level shift circuit see:
http://www.penguintutor.com/electroni...
More details about SPI at:
http://www.penguintutor.com/electroni...


Chapters:
00:00 Introduction to I2C for Pico Arduino
01:43 What is I2C?
02:19 Comparison with UART and SPI
05:12 Which protocol is best?
07:27 ATmega328p
08:39 I2C connecting to multiple peripherals
10:48 Pull-up resistors
12:54 I2C peripheral addresses
14:51 I2C to the Raspberry Pi Pico
17:21 Raspberry Pi Pico C code
24:22 Raspberry Pi Python code (to Pico)
29:35 Testing the Pico
32:28 I2c to the Arduino
34:01 Arduino C code
40:35 Raspberry Pi Python code (to Arduino)
41:41 Testing the Arduino
42:30 Raspberry Pi, Pico and Arduino together
44:59 Summary