noise/interference when reading two analog in pins

I am reading two analog in pins (both with voltages in the range of 0-5V)
on an atmega168.

This voltage value is then being processed and then a new voltage output is generated using a TLV5638 DAC.

On the Analog inputs I have a timer (debouncing) so that the input is only read every 1ms. At the time of reading I also take 4 readings in a row and then average them (oversampling).

Everything works fine when I only apply a voltage to one analog input. However when I have two different voltage sources connected (both in the same range, shared ground and to different analog in pins) there is a certain combination of inputs that creates a really noisy reading (the values at both inputs start to fluctuate rapidly and in a much more pronounced way than just the accepted LSB error.

Any tips for using the Analog Ins and getting smoother readings? Both in terms of coding and electrical interface.

Both are being fed voltage from low impedance sources. Both have clamping diodes for input protection.

I am now wondering if the issue is wit the second analog input.

The first input simply uses a resistor voltage divider (source signal is 0-10V and divided down to 0-5V). Then fed to analog in but with clampin diodes connected to +5V and GND to limit the range.

The second input has a series of two inverting opamps powered on +/-12V. The output of the second opamp has clamping diodes to limit the range to 0-5V and is directly connected to the analog in pins. Should I be putting a resistor in series here? capacitor to ground?

The second input has a series of two inverting opamps powered on +/-12V. The output of the second opamp has clamping diodes to limit the range to 0-5V and is directly connected to the analog in pins. Should I be putting a resistor in series here? capacitor to ground?

Definitely don't drive the diodes directly from the opamp output - the opamp will saturate trying to source/sink current into the diodes - try 4k7 or so on the output. (Conceivably this could put spikes onto the +5V line, causing loss of A->D accuracy or even damaging the ATmega if the opamp is beefy enough.)

Also remember the Arduino is not specifically designed for good analog performance as the digital and analog power rails are shared - its worth using a 'scope if you have one to investigate the signals.

Ok I modded the circuit so that the opamp goes through a 4.7k resistor, then clamping diodes and then the analog input.

Still getting the same problem. It does not happen that often but it still happens. If I have a pot hooked up and sweep through voltages between 0-10V I can find some very narrow settings where I get the "freak out" behavior.

I'll try with a scope next and see if I can better isolate the problem. The source voltage is definitely stable.

It could be an impedance issue with your divider network or op amp or possibly a timing issue between samples.

You can try "throwing away" the first reading when switching between channels to see if that helps.

Another thing you can try is tying one of the unused Analog inputs to GND and take a reading from that input between the other channels (i.e AIN0 --> AIN3(GND)--> AIN1 --> AIN3(GND) -->AIN1....)

Putting a 100n cap to ground on both analog inputs seems to have done the trick. I guess some high freq noise (clock?) was leaking into the inputs due to my pcb layout and this LPF was the thing needed to get rid of the spikes.