Pages: [1]   Go Down
Author Topic: Reading one analog pins affects another analog pin  (Read 342 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 635
Posts: 34572
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 635
Posts: 34572
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: