0

i have an Arduino Mega hooked as slave to the raspi via i2c.

My python scripts looks like this

import smbus
import time


bus = smbus.SMBus(1)

SLAVE_ADDRESS = 0x08

def requestreading():
    block = bus.read_i2c_block_data((SLAVE_ADDRESS), 0, 8)

    print(block)


while True:
    #var = input("Press any key for reading: ")
    requestreading()
    time.sleep(0.1)

    #n = input("Please enter 's':")
    #if n.strip() == 's':
        #break

The code runs for a short time and prints out the values in the shell but then gives me the following error message:

Traceback (most recent call last):
      requestreading()
block = bus.read_i2c_block_data((SLAVE_ADDRESS), 0, 8)
OSError: [Errno 121] Remote I/O error

This is the slave code on the Arduino Mega:

#include <Wire.h>

int SLAVE_ADDRESS = 0x08;
int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;


void setup(){
  Wire.begin(SLAVE_ADDRESS);
  Wire.onRequest(sendAnalogReading);
}

void loop(){


}

void sendAnalogReading(){

  uint16_t reading1 = analogRead(analogPin1);
  uint16_t reading2 = analogRead(analogPin2);
  uint16_t reading3 = analogRead(analogPin3);
  uint16_t reading4 = analogRead(analogPin4);


Wire.write((char *) &reading1, 2);
Wire.write((char *) &reading2, 2);
Wire.write((char *) &reading3, 2);
Wire.write((char *) &reading4, 2);

}

can anybody tell me the reason for this error?

Many thanks

enter image description here

  • Does i2cdetect -y 1 find your Arduino after you script crashes? Also, try slowing the loop down with a short delay. – Roger Jones Feb 6 at 12:45
  • Is that your actual code - seems like the formatting is missing? – CoderMike Feb 6 at 13:36
  • @RogerJones yes it continuously finds the Arduino via i2cdetect. i added a time.sleep(1) in the loop but it still crashes after around 35 readings. any other ideas? – Philip Wahl Feb 6 at 14:45
  • @CoderMike yes this is the complete python script – Philip Wahl Feb 6 at 14:46
  • 1
    How about while i2cdump -y -r 0-7 1 0x08 c; do sleep 1; done? I think that's the right invocation to replicate your Python code. – Roger Jones Feb 6 at 16:11
0

I wasn't using a level converter before. I now integrated it for the SCL and SDA line and the program runs a lot more stable even for shorter sleep times. However it still crashes eventually which i figure is due to a voltage that is between 1.1V and 2.4V und thus is neither defined as high or low and the program therefore shuts down.

is there a way to filter values that are in between?

  • That's what the pull-up resistors on SCL and SDA are for: to drive the voltage up to 3.3v when the lines are "idle". The Pi or Arduino clamp them to ground when they want to send data. What level converter are you using and where are you seeing 2.4v? – Roger Jones Feb 8 at 15:33
  • @RogerJones i am using a TXS0108E Texas Instruments. i am not seeing the 2.4V. it's just that i'm guessing that there is value thats neither high or low and the raspberry therefore can't interpret it and goes to error mode and returns I/O Error. the level converter has improved the stability quite a bit but not indefinitely. – Philip Wahl Feb 8 at 15:49
  • The level converter should be driving the signals high and low without a problem. Are you using extra pull-up resistors on either side of the level converter chip? From a quick look at the datasheet that might be a problem as they can reduce the effect of the ones built into the chip. Give the "Remote IO Error" you are seeing (suggesting that the Arduino is not sending an ACK back) and that the level converter helped my next suggestion would be to to check your connections and try and inspect the I2C signals with an oscilloscope as the problem seems to be an electrical one. – Roger Jones Feb 9 at 8:39
  • @RogerJones no i am not using extra pullup resistors, as both the mega and the Arduino have built in ones. I am however connecting the OE pin on the converter via 10kO to ground as suggested in the data sheet from the level converter. i looked with an oscilloscope but couldn't identify a possible problem. – Philip Wahl Feb 9 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.