Raspberry Pi and Arduino Connected Using I2C - Oscar Liang
Raspberry Pi and Arduino Connected Using I2C - Oscar Liang
These are the images showing where the I2C pins are on
the Raspberry Pi and Arduino.
$ cat /etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't
blacklist spi-bcm2708
#blacklist i2c-bcm2708
i2c-dev
pi@raspberrypi ~ $ ll /dev/i2c*
crw-rw---T 1 root i2c 89, 0 May 25 11:56 /dev/i2c-0
crw-rw---T 1 root i2c 89, 1 May 25 11:56 /dev/i2c-1
pi@raspberrypi ~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Install Python-SMBus
This program has only been tested with Arduino IDE 1.0.
[sourcecode language=”cpp”]
#include <Wire.h>
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
Serial.println(“Ready!”);
}
void loop() {
delay(100);
}
while(Wire.available()) {
number = Wire.read();
Serial.print(“data received: “);
Serial.println(number);
if (number == 1){
if (state == 0){
digitalWrite(13, HIGH); // set the LED on
state = 1;
}
else{
digitalWrite(13, LOW); // set the LED off
state = 0;
}
}
}
}
[/sourcecode]
[sourcecode language=”python”]
import smbus
import time
# for RPI version 1, use “bus = smbus.SMBus(0)”
bus = smbus.SMBus(1)
def writeNumber(value):
bus.write_byte(address, value)
# bus.write_byte_data(address, 0, value)
return -1
def readNumber():
number = bus.read_byte(address)
# number = bus.read_byte_data(address, 1)
return number
while True:
var = input(“Enter 1 – 9: “)
if not var:
continue
writeNumber(var)
print “RPI: Hi Arduino, I sent you “, var
# sleep one second
time.sleep(1)
number = readNumber()
print “Arduino: Hey RPI, I received a digit “, number
print
[/sourcecode]
pi@raspberrypi ~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
That’s the end of this article, for results, please see video
on top. From here, you can add sensors to the Arduino, to
send data back to the Raspberry. Or have servos and
motors on the Arduino that can be controlled from the
Raspberry Pi. It’s just Fun.
Updates: 07/07/2013
Someone messaged me asking how to use logic level
converter for i2c connection between Raspberry Pi an d
Arduino. I happen to have a spare Logic Level converter,
so I gave it a go.