0

I'm using a device through I2C interface and I'm having some issues when the I2C bus fails, I don't know if it fails because an interferance in the cable or just timing issues in the Raspberry over high loads.

The thing is that checking the way this device is used in Arduino and Raspberry there is a difference.

In arduino you use it like this:

Wire.beginTransmission(0x27);
Wire.write(0x80);
Wire.write(75);
Wire.endTransmission();

And in RPi you first set the channel into a pointer and then write to it, but never "endTransmission" or "beginTransmission" when you need to send the message.

I just init the i2c channel at the start:

dimmer = wiringPiI2CSetup(0x27);

And then use this to set the values.

wiringPiI2CWrite (dimmer, pin) ;
wiringPiI2CWrite (dimmer, value) ;

The problem is that once in a while it stops working, maybe a message got lost in the middle and now those 2 writes never are together waiting for the next byte. So, is there a difference between the Arduino version and the wiringPi version for this? is there a way to set the begin and end of a transmission?

The device in question is the Kryda AC Dimmer: https://www.tindie.com/products/bugrovs2012/i2c-4ch-ac-led-dimmer-module/

6
  • 1
    Use int wiringPiI2CWriteReg8 (int fd, int reg, int data); instead of a pair of writes.
    – Majenko
    Commented Oct 30, 2019 at 16:14
  • And... which are you having problems with - the Arduino or the Pi? It's not clear.
    – Majenko
    Commented Oct 30, 2019 at 16:28
  • @Majenko the RPi is causing the issues. What you I set on reg? Data is the byte with the 0x80 and the 75 values for example, but what would be the reg?
    – Yind
    Commented Oct 30, 2019 at 16:39
  • "pin" is the register and "value" is the value you are setting in that register.
    – Majenko
    Commented Oct 30, 2019 at 16:46
  • If you're using WiringPi on a Raspberry then read this wiringpi.com/wiringpi-deprecated before deciding which library you're going to switch to when you ditch WiringPi.
    – Dougie
    Commented Oct 30, 2019 at 17:06

2 Answers 2

1

You should use wiringPiI2CWriteReg8 instead of the raw wiringPiI2CWrite:

dimmer = wiringPiI2CSetup(0x27);
wiringPiI2CWriteReg8(dimmer, pin, value);

This keeps everything together in one neat interface. TBH I am not sure how it differs internally, but it certainly gives you a cleaner interface.

1

The Arduino and Raspberry Pi commands are substantially different.

The Arduino command is sending:

start condition
data 0x80 to address 0x27
data 75 to address 0x27
end condition

The RPi commands are sending.

start condition
data 0x80 to address 0x27
end condition
start condition
data 75 to address 0x27
end condition

Frankly I'm surprised the device accepts the Raspberry Pi data as a valid command.

To do the same as the Arduino you would need code such as the following:

char data[]={0x80, 75};

dimmer = wiringPiI2CSetup(0x27);

write(dimmer, data, 2);
2
  • Thanks! that's what I wanted to know, yes it's a little bit more difficult to work it around with wiringPi. I now tried what @Majenko using the wiringPiI2CWriteReg8 function and it works as well.. I will see if using this avoids the issue that sometimes it stopped working.
    – Yind
    Commented Oct 30, 2019 at 17:45
  • The @Majenko command is probably implemented as the SMBus Write byte command (SMBus 2.0 5.5.4). If so that is equivalent to the code I suggested (which does S Addr Wr [A] i2cReg [A] bVal [A] P in I2C terms).
    – joan
    Commented Oct 30, 2019 at 17:54

Your Answer

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

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