Hi, thank you for your advices,
I don't need to print the values at all, I just did it for debug reason because my whole program is not functionning correctly because of these potentiometers issues.
But why is it only happening while turning the second potentiometer and not the first ??
This is a common problem when doing multiple analogReads consecutively.
There is only one ADC that is shared by all of the analog pins and basically it can't keep up.
To avoid this use a small delay or just do something else in between.
void loop() {
int potentiometerValue1 = analogRead(potentiometer1);
Serial.println(potentiometerValue1);
int potentiometerValue2 = analogRead(potentiometer2);
Serial.println(potentiometerValue2);
int piezo1Value = analogRead(piezotrigger1);
Serial.println(piezo1Value);
}
Like @Hutkikz said, there is really only one ADC in the Arduino processor. The ADC inputs are multiplexed into that one ADC. There is a small value capacitor on the input that the voltage coming in must charge or discharge. It takes time for the input voltage to charge that capacitor. The output impedance (resistance) of the voltage source determines how long the charge time is. The higher the impedance the more time that the charging (or discharging) takes. So if your pots impedance is too high, it takes longer to charge (or discharge) the cap than the time between samples. Therefore the error in reading.
The maximum recommend output impedance for inputs to the ADC is 10K for full resolution.
So increase the time (delay or lower the impedance (cap) or lower pot resistance.
@van_der_decken when this happens you should flag the topic as a cross-post and give a link to the original topic. Don't think of this as being mean to @humbucker. Think of it as being nice to everyone, including @humbucker and all the other forum members. Let the moderators merge the topics then post your advice to @humbucker.
Thank you for these extra details, I'm only beginning to program with Arduino,
my potentiometers are 50K, maybe not the best then
I will try the capacitor, and also will order 10K potentiometers.
I will write back with the results of these tests, for others who might run into the same problems.
It is fine to use higher resistance parts if you are concerned with current use like in battery powered devices. Just be aware of the pitfalls and mitigate the potential of crosstalk with caps and/or lowering the sample rate.
@humbucker Although it will slow your code a bit, which may be irrelevant, doing a double-read of each analog input, discarding the first value and using the second, often cleans up this issue.
Well, this project is a "midi controller", piezo trigger are being used as switches to send midi notes,
so I guess I have no choice to read analogs as quick as possible all the time to be able to play notes in time, without extra delay. Right ?
Thank you!
Strongly suggest trying this, anyway. The change in your code speed is likely not going to be noticeable. ISTR each analog read takes a few tens of uS, so the added time will never be noticeable by humans.
I suspect, though I don't know for sure, it would be an exceptional person who could detect even a 5 mS delay. We're just not tuned to that.
So, 600 uS to do three double reads, plus the execution time of the rest of the code. We have no idea of "the rest of the code", either not written yet or not presented. Hopefully, sub millisecond loop time.
analog.read() is blocking, so one will want to distribute it through the loop, or even check one analog input in each iteration of the loop, but that's all optimization. A millisecond loop time is indistinguishable from instantaneous, for a person.
Point of order - if one does
read AI 1
delay of some form, even doing other things
read AI 2
etc.
You don't benefit from the delay, because the mux is only set to the new channel at the time of the new read; you really have to point the mux to the new channel, then delay, then read, like this:
read AI 0
point to new channel
do something else while mux settles
read AI1
etc
I'm not sure if there's a way to do that, short of a bit of assembly code. Doing the analogread(0), analogread(0), successively, gets it done, and suffices for most of us, but it does suffer from the penalty of the first conversion time.