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 for .
2. This post does not dive deeper into logic level converter. I'm planning to release another post in the future on how to test logic level converters.
3. This article does not focus on multi-controller or multi-target setup.
Working sample
Before we proceed further, let us take a look at what our end result would look like.
Items Needed
- Arduino Uno
- Raspberry Pi 4
- Bidirectional logic level converter
- Breadboards and jumper wires.
Pre-requisites
This post assumes you already have,
Arduino Editor installed on your laptop.
Your laptop connected to Arduino using the USB cable.
Basic familiarity with Arduino programming using C++.
Basic familiarity with Python programming on Raspberry-Pi.
Technical details
How I2C works?
This post does not go deeper into the working of I2C specifications. There are numerous materials available on the internet.
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.
Logic level converter has a separate 5 Volts and 3 Volts logic levels, and helps convert messages across these two levels.
Schematic & Wiring diagrams
This is how the final setup looks like:
How-to steps
Step 1: Connecting Raspberry Pi (controller) to 3.3 volts side of logic level converter
Raspberry Pi pin | Logic level converter pin (low level 3.3 V) | Remarks | Wire color |
1 | LV | 3.3 V | red |
39 | GND (on 3.3v level) | 3.3v Ground | black |
3 (I2C1 SDA) | LV1 | Controller's serial data | yellow |
5 (I2C1 CLK) | LV2 | Controller's clock | blue |
Step 2: Connecting Arduino to 5 volts side of logic level controller
Arduino Uno | Logic level converter pin (High level 5 v) | Remarks | Wire color |
5 V | HV | 5 V | red |
GND | GND (on 5 v level) | 5 V ground | black |
A4 | HV1 | target's serial data | yellow |
A5 | HV2 | Target's clock | blue |
Step 3: Raspberry I2C code in Python
You will need SMBus library for this project.
~~~~~~~~ Python Code
Step 4: Arduino C++ I2C code
There are no new libraries 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. Please refer the last section of the video in this page to see how to test it.
Closing note
Hope this post gives you the basic details for setting up I2C communication between Raspberry-Pi and Arduino. Wishing you all the very best for your IoT projects.
Thanks,
(3)
Comments