Project tutorial
Controlling an Arduino from a Pi3 using I2C

Controlling an Arduino from a Pi3 using I2C © CC0

Demonstration of the I2C protocol. A simple example of turning the Arduino's internal LED on from a Raspberry Pi using I2C

  • 4,687 views
  • 1 comment
  • 14 respects

Components and supplies

About this project

Enable I2C on Raspberry Pi

You will only need to set this up once, assuming you have not done so already.

  • sudo raspi-config
  • Select option 5: Interfacing Options
  • Enable P5 I2C

Procedure

  • Remove all wires from Arduino
  • Connect the Arduino to the computer using USB
  • Upload the Arduino sketch code into the Arduino
  • Download the python code onto your Pi as i2c.py
  • Connect the Arduino to the Pi according to the schematic. The shematic is a little unclear, so note that the red wire connects to Pi physical pin 6, and the ground to pin 2.

Run the controller program from the python:

python3 ic2.py

This should cause the Arduino's internal LED, associated with pin 13, to light up. The program will also respond with the prompt: Press return to exit

Upon pressing return, the program will exit, and the LED will not be lit up.

Supplementary discussion

You can find out the Pi's pin descriptions and physical pins by typing gpio readall

The Pi apparently has two I2C devices: /dev/ic2-0 and /dev/ic2-1. This code uses the latter, as you do not need to be root. /dev/ic2-1 corresponds to bus = SMBus(1) in the python code. You can see the I2C devices available by typing ls /dev/*i2c* .

You need to select an I2C address for the Arduino. You can choose any byte you want for the address, just so long as it is not being used by another device in the chain. In this example, the Arduino is the only device being used. So it does not matter. I chose the value 0x8, which you can see as addr = 0x8 in the python code, and Wire.begin(0x8) in the Arduino code. Whatever value you choose, make sure you use the same in both sets of code.

The libraries assume a certain usage of pins. SDA (data) is on pin A4 on the Arduino, and pin 3 on the Pi. SCL (clock) is on pin A5 on the Arduino, and 5 on the Pi. It may be possible to "emulate" I2C on other pins, but I have not tried this.

There is a lot more to I2C, but this project is a simple proof of concept on getting I2C to work.

Date is written to I2C using bus.write_byte(). I chose the value 0x1 to mean "turn the LED on", and 0x0 to mean "turn it off again". The Arduino reads the byte using char c = Wire.read(), and sets the LED using this value via the digitalWrite() function.

References

Code

Arduino sketchC/C++
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

// 04-Feb-2018 mcarter adapted
#include <Wire.h>

const int ledPin = 13; // onboard LED
static_assert(LOW == 0, "Expecting LOW to be 0");

void setup() {
  Wire.begin(0x8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW); // turn it off
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    digitalWrite(ledPin, c);
  }
}
Python codePython
This is the controller code
from smbus import SMBus

addr = 0x8 # bus address
bus = SMBus(1) # indicates /dev/ic2-1
bus.write_byte(addr, 0x1) # switch it on
input("Press return to exit")
bus.write_byte(addr, 0x0) # switch it on

Schematics

i2c_bb_CpSIoWgNh0.png
I2c bb cpsiowgnh0

Comments

Similar projects you might like

Arduino I2C communication with Raspi 2 WIOT

Project tutorial by Christiano Faig

  • 35,430 views
  • 25 comments
  • 112 respects

Master Slave I2C Connection

by PIYUSH_K_SINGH

  • 3,733 views
  • 0 comments
  • 8 respects

Android Things Andy Robot Rasberry Pi3 And Arduino

Project showcase by Dwayne Hoang

  • 1,854 views
  • 0 comments
  • 7 respects

Arduino DTH22 Humidity Temperature With LCD I2C 16x2 Display

Project tutorial by Team GiftedMedia

  • 28,507 views
  • 5 comments
  • 35 respects
Add projectSign up / Login