2

I'm trying to read some data from the Arduino slave and when I receive the data it shows some numbers but not the correct ones.

I'm using node and i2c-bus module, also tried with wiringPi but got the same result.

I do the following thing in node.

const i2c = require('i2c-bus');


const bus = i2c.open(1, (err) => {

   setInterval(function() {
       var buf = new Buffer(4);
      bus.readI2cBlockSync(0x09, 50,4, buf);
      console.log(buf );


      }, 500);

});

And my Arduino code is:

#include <Wire.h>


int val = 0;
int receiveBuffer[9];
int toggle = 0;

void setup() {
pinMode(3, INPUT);
   pinMode(13, OUTPUT);
   Wire.begin(9);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendData);
}

void loop() {

}

void receiveEvent(int howmany)
{
  int counter = 0;
  while(Wire.available()) {
    receiveBuffer[counter] = Wire.read();
    counter ++;
  }
}


void sendData(){
  if (receiveBuffer[0] == 50) {
    if (toggle == 0) {
    digitalWrite(13, HIGH);
    toggle=1;
    } else {
      digitalWrite(13, LOW);
      toggle = 0;
    }

      Wire.write(66);
      Wire.write(67);
      Wire.write(68);
      Wire.write(69);
  }
}

And the result in the RPi is this:

<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>

See the at some point started to receive different numbers, but other times is mixed and show random things.

I try to do the same master/slave with 2 arduinos and I received the correct data.. I don't know why is failing in RPi and is driving me crazy.

I'm using a level shifter for the 5v i2c between the Rpi and the Arduino.

Also I know the i2c is working ok because I have 4 MCP23017 connected to the 5v i2c channel and they are working.

The arduino seems to receive the data okay as the led toggles alright.

Any ideas?

1 Answer 1

2

After hours of hitting my head against the wall I tried to connect it directly without passing through the level shifter and it worked... I don't understand exactly why it is, I suppose it's because of the pulldown resistors or something like that but can tell exactly.

3
  • 1
    Perhaps your level shifter is not suitable for I2C. I tried more than 10 level shifters and still have not found one good enough. For now I mainly use TBX0102. I once tried TSX0102/04/08 but found them problematic. I recently found PCA9306 and might try it sooner or later. If you tell me what level shifter you are using, I might give some comments make a guess why it doesn't work. One other thing is that Rpi stretch python I2C has a bug,so I have heard, ...
    – tlfong01
    Jul 7, 2019 at 2:32
  • 1
    You DO NOT need a level shifter with I²C to interconnect 3.3V & 5V logic - provided there is ONLY a 3.3V pullup (which is included on the Pi). Technically this results in marginal high levels, but I have never found problems. You CAN use a level shifter, but the need pullup on BOTH the 3.3V & 5V sides. The 10kΩ pullup on some level shifter is too high, and many cheap level shifters only have a voltage divider on the Rx circuit.
    – Milliways
    Jul 7, 2019 at 6:22
  • @tlfong01 I don't know the exact model but it's exactly like this one. luisllamas.es/wp-content/uploads/2018/01/arduino-portada.png
    – Yind
    Jul 7, 2019 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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