# Raspberry Pi I2C with multiple slaves behaves erratically

I have a Raspberry Pi 3B+ that I'm using with two I2C devices on the same bus. The first device is a DAC from Adafruit (product here). The second device is an accelerometer, also from Adafruit (produce here). The DAC is used to control a DC motor driver for a very large 24V motor. The accelerometer will measure vibrations caused by the motor.

When I use either device with the other disconnected, it works great. (I'm using the python libraries provided by Adafruit for both devices.) But when I try to use both devices at the same time (that is, both are powered on), I get interference. The accelerometer reports frequent spikes in acceleration (despite being perfectly still), and the DAC will occasionally change its output voltage. Also, the RPi frequently fails to read the accelerometer, throwing me an IOError. I don't understand what's going on.

### More details

The DAC is powered by 5V. The accelerometer is powered by 3.3V. Both devices have built-in 10kOhm pullup resistors on their I2C lines going to their respective voltage sources. The RPi has built-in 1.8kOhm pullups.

Even though the DAC is on 5V, I measure 3.3V on the I2C lines as the resting high state. Also, the accelerometer is designed to work with 3.3V or 5V I2C voltage.

The DAC is connected to the Raspberry Pi by mere 4-inch wires, which are held apart from each other to minimize parasitic capacitance and EMI. The accelerometer is connected by 3 ft wires, which may seem long, but my o-scope shows clean I2C waveforms at the device end. At the RPi end, the waveforms are not quite as clean, but they seem good enough.

At the accelerometer, the I2C waveform is slightly rounded when the DAC is turned off (which is what you'd expect from high parasitic capacitance). When I turn the DAC on, those waveforms get crip and square. I would've expected the opposite to happen when I add a second device to the bus.

I took the RPi, DAC, accelerometer, and motor driver out of the machine for further testing. I disconnected the 24V power supply and 24V motor and left them in the machine. When I test the I2C devices now, I am unable to replicate any of the errors. It works flawlessly.

I considered that the problem might be EMI from the motor being picked up on my long accelerometer wires, but the motor did not have to be moving for my problems to appear. My 24V power supply is switch-mode, so perhaps it is what was producing EMI.

I considered faulty/damaged/shorted-while-troubleshooting components. I tried a new accelerometer and a new RPi and got the same results. I have not tried a new DAC. However, if any of these components are faulty, I would not expect them to work at all; and yet they work fine most of the time.

I tried changing the I2C frequency on the RPi between 100Hz to 100kHZ (the default) but I saw no effect.

I have verified all the wire connections.

As it is, I cannot put my electronics back in the machine without them behaving erratically.

### Edit

Just for kicks, I put two knots in the accelerometer wire. The errors and spikes coming from the accelerometer were much reduced. However, I lost all communication with the DAC.

• Those long leads do seem suspicious. But can you absolutely confirm that the problem occurs when both are connected, even if you only run the software for one? – Chris Stratton Jun 12 at 20:19
• 3-ft is a long wire for I2C. It might work alone, but the added capacitance of the other device might be screwing things up. What happens if you shorten by 50% – Scott Seidman Jun 12 at 20:20
• @ChrisStratton When both devices are connected, but the DAC is powered off, the accelerometer works perfectly every time. – Eli-1.772 Jun 12 at 20:22
• I realize that 3-ft is very long, but when I move the electronics out of my machine and onto my desk, everything works fine. Also, I o-scoped the I2C at the accelerometer and found pretty good waveforms. – Eli-1.772 Jun 12 at 20:24
• "when I move the electronics out of my machine and onto my desk, everything works fine" that sound like pickup from the motor circuitry. Try it with the DAC powered but the motor amp off. Incidentally, having an unpowered I2C device on a bus is a horrible spec violation; you're lucky that worked at all. – Chris Stratton Jun 12 at 20:48

The I2C frequency is 100kHz and the noise frequency was ~3MHz, so I made a low-pass filter. I put 500pF capacitors on both clock and data, and I put 39Ohm resistors in series directly off of the RPi. (Interestingly, the "official" maximum bus capacitance of I2C is 400pF.) This significantly attenuated the noise.
I also twisted the 3-ft I2C wires of the accelerometer: clock with GND, and data with 3.3V.