top of page

Tailwind series: Two way I2C communication between Raspberry-Pi and Arduino using logic level converter

by Quasortech Labs

RaspArduinoI2CHeaderImage.png

Article covers I2C communication between Raspberry-Pi 4 as controller and Arduino-Uno as target, using logic level converter.

Disclaimer

  1. 2021 revision of I2C specification changed "master/slave" to "controller/target" to align with I3C bus specification.

  2. This post does not dive deeper into logic level converter. We may cover how to test logic level converters in a future post.

  3. This article does not focus on multi-controller or multi-target setup.

Working sample

Before we continue, let's examine what our final outcome will resemble.

Click on this video to play.

Items needed

  • Arduino Uno.

  • Raspberry Pi 4.

  • Bidirectional logic level converter.

  • Breadboards and jumper wires.

Pre-requisites

This post assumes you already have,

1.     Arduino Editor installed on your laptop.​

2.    Your laptop connected to Arduino using the USB cable.

3.    Basic familiarity with Arduino programming using C++.

4.    Basic familiarity with Python programming on Raspberry-Pi.

Technical details

How I2C works?

This post does not delve deeply into the working of I2C specifications. There are abundant resources available on the internet for further information.

How logic level converter works?

Raspberry Pi's pin's output is 3.3 V while Arduino pin's output is 5 volts.

There will be no issues if Raspberry-pi (3.3 V) sends messages to Arduino (5 V). However, if Arduino has to send message to Raspberry-Pi (5 V to 3.3 V), then we risk damaging Raspberry-Pi.

This is where logic level converter comes to our rescue.

LogicLevelConverter (1).png

Logic level converter has a separate 5 Volts and 3 Volts logic levels, and helps convert messages across these two levels.

Schematic & Wiring diagrams

Schematic_IoT_2022-09-25 (1).png

This is how the final setup looks like:

IMG_6421_1.PNG

How-to steps

Step 1: Connecting Raspberry Pi (controller) to 3.3 volts side of logic level converter

IMG_6423_1.PNG

Connection point details:

Step 2: Connecting Arduino to 5 volts side of logic level controller

IMG_6423_2.PNG

Connection point details:

Step 3: Raspberry I2C code in Python

You will need SMBus library for this project.

SMBusLibraryForPython.png

Python code

Step 4: Arduino C++ I2C code

No additional libraries need to be downloaded. <Wire.h> comes standard as a part of Arduino install.

C++ code

Step 5: Testing the bidirectional message communication

Now the setup should be ready to communicate between Raspberry Pi and Arduino.

For a demonstration, please refer to the accompanying video located at the top of this page.

Closing note

I hope this post provides you with the fundamental details on I2C communication between Raspberry-Pi and Arduino. 

Thanks!

Music by QubeSounds from Pixabay

bottom of page