Go Down

Topic: Reading one analog pins affects another analog pin (Read 374 times) previous topic - next topic

ShadowSA

Hi everyone

(If there is a solution to this already, please point me in the right direction, I don't know what to search for.)

I have an itead studio LCD shield and a LM35 temp sensor. The LCD shield has 5 buttons connected to A0 with different resistances on each button.

If I read and display the temperature(Analog pin 4), it works 100%.  If I read and display the button that is pressed (Analog pin 0), it works 100%.
But as soon as I do both in the same sketch, the temp values jump around randomly and the button press affects the temp value.

What am I missing here?

This works:
Code: [Select]

void loop()
{
  readTemp();
  displayTemp();   
  delay(500);
}

void readTemp()
{
  sensorValue = analogRead(TEMP_ADC_PIN);
  sensorValue = round((5.0 * sensorValue * 100.0)/1024.0);
  maxtemp = max(maxtemp, sensorValue);
  mintemp = min(mintemp, sensorValue);
}

void displayTemp()
{
  output = "Max: " + String(maxtemp) + "  " + "Min: " + String(mintemp);
  lcd.setCursor(0, 0);
  lcd.print(output);
  lcd.setCursor(0, 1);
  output = String(sensorValue) + " C";
  lcd.print(output);
}



and this works:
Code: [Select]

void loop()
{
  byte button;
  button = ReadButtons();
 
  lcd.setCursor(15,1);
  lcd.print(String(button));  //Display button press in right bottom corner.
 
  delay(500);
}


ReadButtons() I copied from this website http://www.freetronics.com/pages/16x2-lcd-shield-quickstart-guide#.UaHASbWNrg8,  but just doing a simple analogRead on pin A0 yields the same results.

Calling ReadButtons() and readTemp() in loop causes the temp values to be random and is affected by a button press.

Grumpy_Mike

This happens when the impedance of what is on the analogue input is too high for the sample and hold to come up to full voltage in the time between switching over the multiplexer and taking the reading.
A software fix is to read each channel twice and only use the second reading. Or in extreme cases read the channel, delay the read the channel again, use that second reading.

ShadowSA

Thanks a million. That fixed my issue.  Just out of interest sake, what would a hardware fixed involve?

dc42

If it's a signal that varies only slowly, a hardware fix is to connect a 0.01uF capacitor between the analog input pin whose reading is being affected and ground.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Grumpy_Mike

Quote
what would a hardware fixed involve?

Well I would have reduced the impedance of the analogue signals, by either reducing the values of the resistors or adding an op amp configured as a voltage follower on the temperature sensor's output.

Go Up