I2C - RPi -> Arduino error after few minutes

Hello,
I have trouble with I2C communication between RaspberryPi and Arduino (tryed nano and mega).

RPi is master and Arduino is slave.

When I start my python script in Raspberry it works only for few minutes (sometimes hours). When Arduino stopped last received character is strange(wrong/different).

Thanks to everybody for any idea.

RPi python program:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import smbus
import time, datetime
bus = smbus.SMBus(1)
address = 0x2a

errorcounter = 0;
i = 0;
def toWrite(a):
	time.sleep(1);
	for i in a:
		bus.write_byte(address, i)

while True:
    timestamp       = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S");
    print "Timestamp:                   |%s"        %timestamp;
    print "Write byte:"
    print datetime.datetime.now().strftime("%m-%d %H:%M:%S!"); 
    print map(ord, datetime.datetime.now().strftime("%m-%d %H:%M:%S!")) 
    toWrite(map(ord, datetime.datetime.now().strftime("ABCDEFGHIJKLMNOPQRSTUVxyz%m-%d %H:%M:%S!")))
    time.sleep(1)

Arduino code:

#include <Wire.h>
#define SLAVE_ADDRESS 0x2A

char command;
void setup() {
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);
    Wire.onRequest(sendData);
    Wire.onReceive(readData); 
    Serial.begin(9600);
}

void loop() {
}

int index = 0;

// callback for sending data
void sendData() {
 }
 
// callback for receiving data.
void readData(int numbytes) {
    // loop through all but the last
    char c = Wire.read(); //Convert byte to char.
    command = c;  //Convert each character into a string
    Serial.print(command); 
    if(command == '!'){
      Serial.println(' '); 
    }
}

Capture.PNG

Could you post how these devices are connected ?
Ar you using pullup resistors on SDA and SCL ?

Hello,
thanks for your reply.

I tryed to use 1,2KOhm and 10KOhm pull up resistors. I bought and connect I2C level shifter too, but still ends with error.

I also tryed connect both 5V source, Arduino Only 5V, RPi only 5V and RPi only 3v3 with same result.

Image with RPi and Arduino connections is in attachement.

10k is too high and 1.2k is too low :wink:

Per I2C spec the SDA pin can sink a maximum of 3mA. As you have a 1.2k pullup resistor connected to +5V according to ohm law a current of 4.2mA is going through the resistor. The SDA pin cannot sink that much current and is not able to pull the line voltage below logical zero.

You should as a minimum use a 1,8k resistor. The usual recommended value is 4.7k.

There are two resources for I2C:

Nick Gammons outstanding website:

And DSS circuits explanations of the effect of differnt I2C pullup resistors:

Also, breadboarded connections are really prone to failure and may actually be another very possible cause for this intermittent problem. So grab a soldering iron and solder some of the connections. That should also improve the situation.

Thanks for your post :slight_smile:

I bought 4k7 resistors and change it on my breadboard. But still same error :frowning:

My possible further steps:

  • Solder connections
  • Reinstall Rpi system

I changed speed of I2C on RaspberryPi from 100000 to 75000 in file: /sys/module/i2c_bcm2708/parameters/baudrate

With 4k7 pull-up resistors It works now for 2 hours :slight_smile:

Thanks to Headroom