4

I'm trying to send data through the I²C interface from the Arduino Uno to the Raspberry Pi using I²C. This was the code I used.

In Arduino:

#include <Wire.h>
unsigned int watt;
unsigned int watt1;
byte watt2;
byte watt3;
void setup()
{
    Wire.begin(30);
    Wire.onRequest(requestEvent);
    Serial.begin(9600);
}

void loop() {
    delay(100);
    int sensorValue = analogRead(A0);
    int sensorValue1 = analogRead(A1);
    watt = sensorValue * (5 / 1023) * 2857;
    watt1 = sensorValue1 * (5 / 1023) * 2857;
    watt2 = map(watt, 0, 4294967295, 0, 255);
    watt3 = map(watt1, 0, 4294967295, 0, 255);
    Serial.println(watt2);
    Serial.println(watt3);
}

void requestEvent()
{
    Wire.write(watt2);
    delay(30);
    Wire.write(watt3);
}

And in the Raspberry Pi:

import smbus
import time
bus = smbus.SMBus(0)
address = 0x1e
while (1):
    watt=bus.read_byte_data(address,1)
    watt2=bus.read_byte_data(address,2)

I received the following error.

Traceback (most recent call last):
File "/home/pi/i2ctest.py" , line 8, in <module>
watt = bus.read_byte_data(address,1)
IOError: [Errno 5] Input/Output error

How do I fix this? Also, are there any alternatives for using I²C in Raspberry Pi other than the SMBus library?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user17151
  • 2,607
  • 3
  • 18
  • 13

1 Answers1

4

If you have a Raspberry Pi with a revision 2.0 board, you need to use I²C bus 1, not bus 0, so you will need to change the bus number used. In this case, the line

bus = smbus.SMBus(0) 

would become

bus = smbus.SMBus(1)

You can check that the device is present on the bus by using the i2cdetect program from the i2ctools package. Try

i2cdetect 0 -y 

to look for the Arduino on bus 0. Run

i2cdetect 1 -y 

to look for it on bus 1. Of course, the Arduino program must be running for this to work. This will also confirm that the Arduino is present at the expected address.

You'll also need to make sure that you have the appropriate rights to use I²C, so run your Python program from an account that is a member of the i2c group.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I run revision 1 and I had tested it with i2cdetect 0 -y and it showed the right address. I had found the problem with it (it was that the arduino was waiting for a signal from the rpi but it never came. I had removed that line) but now I have another problem, the rpi's i2c input is constantly 0 irrespective of what i transmit from the arduino – user17151 Dec 14 '12 at 19:05
  • On the Arduino, if you multiply your value by 5 / 1023, I believe this will always evaluate to 0. Try 5.0 / 1023. – Romilly Cocking Dec 15 '12 at 13:16
  • 1
    @user17151: are you using pullup resistors? I2C are "open drain" lines and can't pull the signal up, so you need pullup resistors to get to +V. (See any I2C tutorial if you're not using these yet.) – tom10 Dec 16 '12 at 06:13
  • Also, please keep in mind that i2c on Rpi is 3.3v compatible while i2c on arduino is TTL level (5V). so you will be needing a conversion circuit to change levels. (something like this : http://playground.arduino.cc/uploads/Main/i2c-level-shift-mosfet.png) and also as per tom10 pointed out, you will need to pull up lines. – dhruvvyas90 Nov 19 '14 at 09:23