In my project I connected an Arduino UNO (as a "measuring slave") with an Arduino Mega2560 (as master, for ananlysis and display tasks) via I2C. The I2C uses A4 / A5 on the UNO for SDA and SCL. The measured signal (0 ... 5V) is connected to A0 on the UNO board and I want to read it with high sampling rate (e. g. 2kHz).
Now I found a hint (https://www.arduino.cc/en/Tutorial/Foundations/AnalogInputPins) that using analog in pins for digital purposes might influence the accuracy of the analog reading on other pins:
"The ATmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins."
Do I understand right? Does the usage of I2C via A4 / A5 compromise the results on A0?
As I want to achieve high sampling rates I'd like to shorten any delays to a minimum. What would be a sufficient delay? Does anybody have any experiences regarding this topic?
Thanks for any help!
When switching ADC channels there can be crosstalk. The usual approach is to read an input twice after switching the channel, ignoring the first reading. No delay should be needed.
The analog source impedance should be less than 10K, and to avoid electrical interference from digital signals, keep wires short and separated from each other.
Finally, it helps to turn off the digital driver from each analog input by setting the appropriate bit in the DIDR0 register on AVR based Arduinos:
DIDR0 = 0x01; // turn off the digital input for adc0
I have no experience with analog pins used as digital interfering with other analog pins readings, I can't help you with that, sorry. But, just curious, do you have any special reason to not use the 2-Wire (I2C compatible) hardware peripheral? That would also prevent eventual digital switching near the analog pins...
@jremington: Many thanks for your quick answer!
Hint #1: worked pretty well (eliminated bad reading on the very first attempt), I intuitively used a similar approach in a different context ...
Hint #2: I kept the wires short anyway (where possible) and used shielded wires for longer distances
Hint #3: I'm not sure if I did it sensible:
I used the internal Pullups as I've set the mode of all unused pins:
pinMode(PIN_NO_USE_B, INPUT_PULLUP);
pinMode(PIN_NO_USE_C, INPUT_PULLUP);
...
Following your hint, I've turned off the digital driver of the Analog In "A0", where the sensor is connected on my UNO (0 ... 10V signal, reduced to 0 ... 5V by a voltage divider (2 resistors) on the breakout board directly beside the UNO) by
DIDR0 = 0x01;
I guess this is what I can do to make the system robust? Right now I don't have the equipment to check more precisely (no oscilloscope available here), but my 2kHz-Min-Max-check shows a +/-2 step jitter when connected to a 9V battery + resistor as "sensor simulation" (i. e. 853 ... 857 of the 0 ... 1023 analog read), which refers to +/-10mV, which isn't too bad, right?
Any further hints appreciated, but I can live with this results.
@ tinyelectr:
Good point: My understanding was that on the UNO board, the A4/A5 analog pins in fact ARE the standard I2C pins for SCL and SDA. I found this in the documentation for the wire library:
But you're right: looking at the UNO pinout diagram, it shows pin 18 and 19 for SDA and SCL.
Would it be better to use those?
I will have to look up, which connection parameters the sensor expects. I guess, two 20K resistors would be fine. Then I'd prefer this, because (as far as I understand) a 100nF cap as suggested could cause problems with measuring a fastly varying signal.
(By the way: please excuse linguistic mistakes, it's not my mother tongue )
because (as far as I understand) a 100nF cap as suggested could cause problems with measuring a fastly varying signal.
With two 100K resistors in the voltage divider, the RC time constant would be 50K*100nF = 5 milliseconds.
That could be a problem if you expect input signal frequency to be higher than a few hundred Hz, in which case you can reduce the size of the capacitor or the resistors.
I checked the requirements of the sensor and could reduce the 2 resistors of the voltage divider to 18k each. Additionally I added a 22nF cap for further stability and now there is no (visible) jitter at all.
I'll have to check the max. signal frequency, but looking a RC time constant of <0.2ms, I don't expect any issues.
Thanks for your help!