ADC analog reading varies when 2 potentiometer is connected

Hello

I am finding a strange issue when working with ADC using Arduino Mega board.

I have 2 pots(10K each) connected to the A0 and A1 pin of arduino board.

What I have observed is when A1 value(pot wiper is moved) changes, the A0 value is also changed. The vice-versa does not happen.

Below is the code that I have written.

My aim is to use A1 channel to vary the intensity of the LED's
A0 channel is to set the color of the LED's.

Has anyone encountered such kind of behavior before.

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int intensityPin = A0;
const int colorPin = A1;

const int pwm_pin1 = 9;
const int pwm_pin2 = 10;

const int max_pwm_value = 128;
int intensity = 0;
int color = 0;
String p1value;
String p2value;
String ivalue;

void setup() {
  // put your setup code here, to run once:

  lcd.begin(16, 2);
  //analog pins
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  //pwm pins
  pinMode(pwm_pin1,OUTPUT);
  pinMode(pwm_pin2,OUTPUT);
  //analog reference value set to default
  analogReference(DEFAULT);
  //this is for pwm. to be used later
  TCCR2B = (TCCR2B & 0xF8) | 0x03 ;
}

void loop() {

  intensity = 0;
  color = 0;
  lcd.setCursor(0,0);
  lcd.write("Range:3200-5600"); 
  
  intensity = analogRead(intensityPin);
  color = analogRead(colorPin);

  intensity = intensity / 8;
  ivalue = String(intensity);
  color = color / 8;

  analogWrite(pwm_pin1, max_pwm_value - color);
  analogWrite(pwm_pin2,color);

  lcd.setCursor(0,1);
  p1value = String(color);
  p2value = String(max_pwm_value - color);
  
  String formatted_value("");
  formatted_value = p1value + ":" + p2value + ":" + ivalue;
  lcd.write(formatted_value.c_str());

  
  // put your main code here, to run repeatedly:

}

Thanks
pwarrie

How big an effect is this? Normally I'd expect this setup to be rock steady unless you have very long
wires involved to the pots, or you've miswired the pots.

Does each pot have full control 0..1023 to its respective analogRead() value?

Agree with MarkT. Could be a wiring problem.
Higher value pots (>100k) can do this, but not 10k pots.

Some strange things in the code.

Pin numbers are less than 255, so can all be byte, to save memory.
const byte intensityPin = A0; // will work

Not a professional coder, but I read that "String" should be avoided on Arduinos.

pinMode(A0, INPUT); // why?
pinMode(A1, INPUT); // all pins are 'input' on bootup, so these two lines are useless

analogReference(DEFAULT); // useless line too, defaults to default at bootup, remove

in loop()
intensity = 0; // two useless lines, remove
color = 0; // because they are overwritten three lines after that

intensity = analogRead(intensityPin);
intensity = intensity / 8;

Can do that in one go with
intensity = analogRead(intensityPin) >> 3; // read and divide by two three times
Same for "color".

Why make a String of values.
Just lcd.print() each item on a separate line.
The LCD will join it if you don't move the cursor.
Leo..

Might try some "dummy" reads to give the sample & hold capacitor time to "drain out" the previous pin reading:

intensity = analogRead(intensityPin);
intensity = analogRead(intensityPin);
color = analogRead(colorPin);
color = analogRead(colorPin);

outsider:
Might try some "dummy" reads to give the sample & hold capacitor time to "drain out" the previous pin reading:

intensity = analogRead(intensityPin);

intensity = analogRead(intensityPin);
color = analogRead(colorPin);
color = analogRead(colorPin);

Can't help if the pots are 10k, they are low enough impedance for this not to be an issue.

However if they are 100k it would help.