Hi, I have a problem while trying to read multiple analog inputs at the same time. I have connected two joysticks (basically 4 potentiometers) from an old controller to A0, A1, A2 and A3 on Arduino Mega. I am using the simplest code where I just store the analogRead(A0), analogRead(A1) … to floats and print them on Serial port at 9600 baud rate.
But the problem is that all four inputs interfere with each other, a lot. All four outputs vary while changing any of the four inputs.
For testing, I disconnected all but one input at A0. Reading A0 gives a fairly accurate value. Reading A1 (Even though there is nothing connected to A1) gives a slightly noisy value of A0. The values becomes noisier as you go further down to A2, A3, A4 … Even A15 gives the same value as A0, even though it is on the far side of the board.
Please explain to me why this is happening? And how do I solve this issue? For my desired project, I need clean values from all four potentiometers that does not interfere with one another.
Post a schematic of your wiring. Include all components, their values and power supplies. What are the values of the pots in the joystick?
Post a photo of your wiring.
Post the code that gives the erroneous readings, in code tags.
Reading A1 (Even though there is nothing connected to A1) gives a slightly noisy value of A0.
Any input that has nothing connected to it is floating and the state is indeterminate (unknown).
Things in a single core MCU never happen at the same time. There is about 0.1ms between analogue readings in a Mega. Should not be a problem for humans.
The A/D returns an int (0-1023), so you should use the int datatype, not a float.
If your pot wiring is ok (not shared with other devices on a breadboard), then try double readings of each pot (ignoring the first reading after you switch to the next pot).
‘Floating’ inputs pick up noise, so will return random results.
Your topic has been moved to this forum section as this location is more relevant to the subject
Have a look into the datasheet of the ATmega at the ADC section. There is a Analog Input Circuitry image. Before the ADC can convert the voltage an internal capacitor (called sample and hold, S&H capacitor) needs to be charged.
Charging the capacitor takes time. There is a time constant called tau. See here
For a 10-bit ADC you need at least 7 times tau to charge the S&H capacitor to within 1LSB.
The time constant depends on your source impedance e.g. the Potentiometer, the sampling capacitor is fixed. If the resistor value is too high the capacitor is not charged or discharge enough by the time the sampling is finished. The datasheet states the impedance of the source should be 10kOhm or less.
When you leave a pin open the impedance is many MOhms, so the S&H voltage stays near the level it was before.
If your potentiometer is larger than 10kOhm you could add some buffer capacitor at each analog pin.
For 10-bits I would suggest to make the capacitor more than 2048 times larger than the S&H. Because if you remove or add 1 S&H charge the voltage would stay within 1/2 LSB. But, doing that will reduce the sampling frequency. You have to calculate tau with your source and the large capacitor.
What are the resistor values of the joystick?
Hi, thanks for your detailed reply. That is exactly the real reason for my problem. I will look into it in depth.
The joystick’s resistance values are 0-10 kOhm.
If the cable length from the joystick is too long, there can be problems too. It is necessary, at least, to install capacitors at the inputs according to post #5.
0-10k implies a variable resistor (two wires).
I hope you are using a pot (three wires).
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.