Electronic – Raspberry Pi I2C with multiple slaves behaves erratically

i2craspberry pi

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.

Best Answer

I solved the problem. The interference on the I2C lines was probably caused by EMI from the switching power supply and PWM motor controller.

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.

The errors and spikes in the acceleration readout are totally gone now and it works great.

Related Topic