Passing serial data over I2C between two Arduino’s

Below you will find some arduino code for passing serial data over I2C between two Arduino’s. Parts of the following code were salvaged from various places around the internet.


This can also be used for:

1) General communication between two Arduino’s over I2C

2) Using a second Arduino to expand the peripherals, for example: add a a second serial port device, add a second arduino for dedicated monitoring of certain interfaces

I used this to communicate between a Diavolino@ 5V with a Arduino Pro @ 3.3V using the Sparkfun level converter TX lines. My attempts at trying to run this without the level converter using pullup registers tied to 3.3V did not work. I will reattempt that once I get a scope for debug.

I2C Master code:

#include <Wire.h>
// SCL - analog pin 5
// SDA - analog pin 4

int led = 13;
int debug = 0;

void setup() {
  pinMode(led, OUTPUT);     
  Wire.begin(); //join I2C as master
  Serial.begin(9600); //setup serial for input and output
  digitalWrite(led, HIGH);
  Serial.println("I2C MASTER 3.3V");
  delay(5000); // the master should be ready after the slave

void loop() {
  if (Serial.available()) {
    char inChar = (char); 
    if (debug) {
  else {
    if (debug) Serial.println("!serial");

  Wire.requestFrom(4, 1); // request 1 byte from slave device #4
  if (debug) Serial.println("waitOnReq");
  char d =;
  if (debug) {
    Serial.print(" reqData:");
  else { // ignore special character ?
    if (d != '?') Serial.print(d);

  if (debug) digitalWrite(led, HIGH);
  if (debug) delay(200);

I2C Slave code:

#include <Wire.h>

// SCL - analog pin 5
// SDA - analog pin 4
int led = 13;
int debug = 0;
void setup() {
    pinMode(led, OUTPUT);     
    Wire.begin(4);                // join i2c bus with address #4
    Wire.onReceive(receiveEvent); // register event
    Wire.onRequest(requestEvent); // register event
    Serial.begin(9600);           // start serial for input and output
    digitalWrite(led, HIGH);
    Serial.println("I2C SLAVE 5V");
    delay(2000); // the slave should become ready first

void loop() {

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
    if (debug) {
        char c =;
        if (debug) {
        else {

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
    if (debug) Serial.println("reqEvent");
    if (debug) digitalWrite(led, LOW);
    if (Serial.available()) {
        char inChar = '0'; 
        inChar = (char); 
        if (debug) {
    } else { //send some dummy data if no data is available from serial
        if (debug) Serial.println("!sent:");

My RaspberryPi Config

Setup for first boot

I bought an 8GB micro SDHC card which came with a USB card reader and an SD adapter. I used the windows disk image writer for setting it up with the latest raspbian wheezy image.

Then I powered the card using a usb cable and used a ethernet cable to connect it to my router. I then setup my router to assign The RPi a reserved easy to remember IP address.

$ ssh pi@

On first login its best to setup the following config using your preferences.

pi@raspberrypi ~ $ sudo raspi-config

At this point I was using ~1.5GB of the card.

Please note that the following tidbits have been scraped from various parts of the internet and I did not really have to figure out any of this myself.

Here is a good index page from the embedded linux RPi wiki for quickly looking up information.


Installing Packages

Next I upgraded all the installed packages, this takes a while to finish.

$ sudo apt-get update ; sudo apt-get upgrade

The upgrade pushes the card upto ~1.6GB.

General packages

$ sudo apt-get install vim ctags tcsh screen tightvncserver python-setuptools git

Automatically Adjust Clock speeds – Save Power

$ sudo vi /boot/config.txt


Install the cpufrequtils package, which allows control over the CPU speed based on demand and reboot:

$ sudo apt-get install cpufrequtils
$ sudo cpufreq-set -g ondemand
$ cpufreq-info
  current CPU frequency is 100.0 MHz.

GPIO packages

$ sudo apt-get install python-dev
$ sudo apt-get install python-rpi.gpio

Packages for OS X screen and file sharing

$ sudo apt-get install netatalk avahi-daemon
$ sudo update-rc.d avahi-daemon defaults

Miscallaneous setup

OS X File and Screen sharing

$ sudo vim /etc/avahi/services/afpd.service
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
 <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 <name replace-wildcards="yes">%h</name>

$ sudo vim /etc/avahi/services/rfb.service
<?xml version="1.0" standalone='no'?>
 <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 <name replace-wildcards="yes">%h</name>

Note that rfb port 5901 implies :1 display.

$ sudo /etc/init.d/avahi-daemon restart


sudo vim /etc/modules

and add these two lines to the end of the file:


Next for python support:

sudo apt-get install python-smbus
sudo apt-get install i2c-tools

And finally try the following to test:

sudo i2cdetect -y 1
sudo i2cdetect -y 0

Setup config files


 set expandtab "et"
 set sw=4 "shiftwidth"
 set smarttab "spaces for tabs ?"
 set ts=4 "tabstop
 set shiftround
 "Indentations and line numbers
 "set number "nu"
 set autoindent
 set cindent "ci"
 set incsearch "incremental search
 set hlsearch "highlight search
 "line break - causes word granularity wrapping
 set lbr
 set wrap
 syntax on
 syntax sync fromstart
 set foldmethod=indent
 filetype plugin indent on
 syntax enable
 "set invlist
 set showmatch "sm"
 set smartcase "scs no ignore case when pattern has uppercase
 set backspace=2 "bs how backspace works at start of line
 filetype on "filetype detection on
 "dont indent when pasting with middle clicks
 set paste
 " Make shift-insert work like in Xterm
 set nocompatible "dont emulate bugs
 " configure command line completion
 set wildmode=list:longest,full
 " setup status line
 set statusline=%F%m%r%h%w\ [%{&ff}]\ [%Y]\ [%07l,%07v][%p%%]\ [%L]
 set laststatus=2 " always show the status line


 set prompt="%~ %# "
 bindkey -v
 set history = 5000
 set savehist = (5000 merge)
 set histfile="$HOME/.tcsh_history"
 history -L
 # Make command history (arrow keys) use history search operations
 bindkey -k down history-search- forward
 bindkey -k up history-search-backward
 setenv MANPAGER less
 alias l ls -lh
 alias cls clear
 alias vi vim -p
 alias vim vim -p

Disable root ssh

$ sudo vi /etc/ssh/sshd_config

Change to following:

PermitRootLogin no

Measure Core Temperature

$ /opt/vc/bin/vcgencmd measure_temp

For other options possible with this command run:

$ /opt/vc/bin/vcgencmd commands

Serial port setup

Remove the following text from the /boot/cmdline.txt file:

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

And comment out the following from /etc/inittab file:
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

You also might want to install minicom for monitoring:

$ sudo apt-get install minicom

To setup the serial port packages required for use with python:

$ sudo easy_install pyserial

Reboot time

As per my rough measurement it takes around 90-120 seconds for the raspberry pi to be able to accept ssh connections after initiating a reboot. I suspect that this is partly because of my slowish class 4 memory card. I was getting much quicker boot times with the 45MBps UHS-I SD card from my camera.

Change hostname

Update it in the following files and reboot:

$ sudo vim /etc/hostname /etc/hosts

And lastly

Once you are happy with your config remember to make a backup img of your card which you can restore if anything goes wrong.
A useful suggestion I found here was to zero out the free space to get better compression.

# cat /dev/zero > /zeroes ; rm /zeroes

Giving Arduino a second UART over I2C by stacking another Arduino on top

Now that I have the Cellular modem working with the PC over serial I am trying to get the same thing implemented on an Arduino. Since the modem uses the serial interface I will not be able to use that to debug my program via a serial terminal running on my PC.

I tried using the SoftSerial (or the NewSoftSerial) library but ran into data corruptions even at the low speeds, so I decided to look for other ways to get another hardware UART on the Arduino. For a while I was contemplating getting a Mega Pro from Sparkfun but its price was a turnoff. In the end I realized that I could just stack another Arduino on my Diavolino (both interfaced to each other over I2C as master slave) and after trimming the UART rx/tx shield pins be able to separate out the two UARTs.

The other issue is that the UART pins of the Cellular modem are not 5V tolerant. This had not affected me till now since I was using the Adafruit FTDI friend at 3.3V logic levels when connecting to the PC serial port. So I could not get another 5V Diavolino. In the end I decied to get a 3.3V Arduino Pro and stack that on top of my 5V Diavolino.

Overall the connections will look something like the following diagram:

arduino extra serial port plan

Note that when I drew this I was planning on using another Diavolino at 3.3V but decided against it since it seemed like more work because I would need to get the 3.3V regulator on my own and also somehow get an 8MHz AVR chip for it. In hind sight this may not have been a good idea since the Arduino Pro seems to be connecting both the Vcc pins in the header to 3.3V. The Diavolino instead has them separated which seems more sensible to me.

Another thing I forgot to show in the diagram above are the pull up registers for the I2C lines (SDA & SCL) between the two Arduino boards. I am hoping that connecting those to 3.3v would work for the 5v arduino, but I am not sure.