

While there are many ways described on how to interface with Arduino over Serial port, I found very few on anything else.
Both serial and ICSP communication require logic level converters and I have none of those...

But not to be too sad, I2C does not! And it lets you talk to more than one device on the I2C network!
So I went out searching and stumbled upon https://github.com/binerry/RaspberryPi/ ... 2c-arduino which is a sample implementation showing a working I2C communication between RPi and Arduino. But... how can you stop at that?

So after some editing and whatnot, I ended up with a binary for the Pi and a small library for the Arduino that lets you do just about anything you want with them.
Here is the usage for the binary:
Code: Select all
Controll your Arduino through I2C
Usage: i2c-arduino ADDRESS COMMAND [ARG]...
Returns: -1 on error or integer value of the result
Commands:
read <pin> Sets pin mode to INPUT and returns digitalRead(pin)
pread <pin> Sets pin mode to INPUT_PULLUP and returns digitalRead(pin)
aread <pin> Returns analogRead(pin)
write <pin> <value> Sets pin mode to OUTPUT and executes digitalWrite(pin, value). Returns the set value
awrite <pin> <value> Sets pin mode to OUTPUT and executes analogWrite(pin, value). Returns the set value
<custom_command> Execute custom command declared in the arduino sketch. Separate the command from the arguments with forward slash (ex: P13/1)
Yes you can define your own commands to send to the arduinos and get an integer result

Here is the example sketch included:
Code: Select all
#include "Commander.h"
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
Commander i2c = Commander::init(0x04);//I2C Address
//custom i2cCommander Command Functions
int i2cP13(String command){
int pos = command.indexOf("/");
if(pos != -1){
int value = command.substring(pos + 1).toInt();
i2c.dw(13, value);
return value;
}
return pos;
}
int i2cLCD(String command){
if(Commander::countSlashes(command) < 3){
return -1;
}
String args = command.substring(command.indexOf("/") + 1);
int charPos = args.substring(0, args.indexOf("/")).toInt();
args = args.substring(args.indexOf("/") + 1);
int line = args.substring(0, args.indexOf("/")).toInt();
args = args.substring(args.indexOf("/") + 1);
lcd.setCursor(charPos,line);
lcd.print(args);
return 0;
}
void setup(){
//init LCD
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Raspberry Pi I2C");
lcd.setCursor(0,1);
lcd.print(" Arduino Bridge ");
//add custom commands to be called from I2C bridge
Command_t i2cCustomCmd;//simple control of pin 13 demo
i2cCustomCmd.execCommand = "P13";//name of the command to listen for
i2cCustomCmd.callback = i2cP13;//function to be called when the command is received
i2c.addCommand(i2cCustomCmd);//add it to the i2c commander
Command_t i2cLCDcmd;
i2cLCDcmd.execCommand = "LCD";//print stuff to the LCD
i2cLCDcmd.callback = i2cLCD;
i2c.addCommand(i2cLCDcmd);
}
void loop() {
}

Code: Select all
sudo ./i2c-arduino 4 P13/1 //set pin 13 to HIGH
sudo ./i2c-arduino 4 P13/0 //set pin 13 to LOW
sudo ./i2c-arduino 4 LCD/0/0/"I2C is AWESOME!!!" // print "I2C is AWESOME!!!" from first character on the first line of the screen
And at the end of the day you can control and monitor 127 Arduinos attached on the I2C bus

