Hi guys,
I'm struggling with an issue since 2 weeks and I cannot find any solution. I'd like to make a raspberry pi 3 and an arduino pro mini (3.3V) talking via i2c. I connected them directly since the raspberry pi 3 already has pull up resistors. Previous to this, I was able to make the raspberry pi 3 and an HTU21D sensor via i2c talking successfully, so I'm excluding hardware issues on raspberry pi, I'm guessing something is wrong with the arduino, and since changing the arduino module didn't fix the issue I have a software issue and cannot find what can be.
the arduino code (only the i2c part):
#include <Wire.h>
#define slaveAddress 0x08
byte command = 0;
byte txTable[4]; //for sending data over I2C
byte rxTable[4]; //for sending data over I2C
bool newDataAvailable;
void setup()
{
Serial.begin(9600);
newDataAvailable = false;
Wire.begin(slaveAddress); //I2C slave address
Wire.onReceive(i2cReceive); //register handler onReceive
Wire.onRequest(i2cTransmit); //register handler onRequest
}//setup
void i2cReceive(int byteCount) {
if (byteCount == 0) return;
//the first byte is the command
command = Wire.read();
if (command < 0x80)
{
//it is a receiving data command, we have to handle the next bytes
i2cHandleRx(command);
}
}
byte i2cHandleRx(byte command) {
// The I2C Master has sent data, store them in receiving buffer
byte result = 0;
switch (command) {
case 0x0A:
if (Wire.available() == 4)
{
for (int i = 3; i >= 0; i--)
rxTable[3-i] = Wire.read();
result = 4;
}
break;
}
if (result != 0)
newDataAvailable = true;
return result;
}
void i2cTransmit() {
//called by event
byte numBytes = 0;
switch (command) {
case 0x90:
numBytes = 4;
break;
}
if (numBytes > 0) {
Wire.write((byte*)txTable, numBytes);
}
}
Data are stored in the txByte array in the loop method. I can successfully send data from raspberry pi to arduino, but I cannot read data from it. Using some print to the monitor I can see that the arduino is reacting to the master requests. On raspberry pi I'm using python-smbus, so the command:
import smbus
bus = smbus.SMBus(1)
data = [0,50,100,100]
bus.write_i2c_block_data(0x08, 0x0A, data)
is working. But the command:
import smbus
bus = smbus.SMBus(1)
data = bus.read_i2c_block_data(0x08, 0x90, 4)
raises the error:
Traceback (most recent call last):
- File "", line 1, in *
IOError: [Errno 5] Input/output error
I'm wondering what I'm doing wrong on data transmission.
Does anyone has an idea?
Thank you.
Regards.