Analog0 going "haywire" with LiquidCryst

Hi all,
I put together a quick kit to do some accuracy testing on the LM35 temp sensors, with a Duemilanove and a 20x4 HD44780 LCD to display the output. I am using two LM35’s on two analog inputs, and averaging their readings, to get as accurate temp results as possible. Everything works fine with one LM35 attached to Analog0, but when I have the two attached to Analog0 and 1, the outputs are all over the place. When I switch it over to Analog 1 and 2, everything is fine. The thing I notice (watching the serial output as well), is when I comment out the LiquidCrystal library from my code, everything works normally on Analog 0 again. Is this a function of my bad coding, the way the LCD interfaces, or possibly a bug?

I am a “snippet cutter” programmer at best, so forgive my sloppy pieced-together coding. below is my full code on the Arduino (using Analog 1 and 2, again it only goes haywire when I switch it to Analog 0 and 1). Image of my hardware below.

//declare variables
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 14, 11, 7, 8, 9, 10);
int tempC1;
int tempC2;
int tempF1;
int tempF2;

void setup()
{
Serial.begin(9600);
lcd.begin (19, 3);
}
void loop(){
analogReference(INTERNAL);
int span = 50;  // take x samples from sensor, to get accurate reading
int tempC1 = 0;
int tempC2 = 0;
int tempF1 = 0;
int tempF2 = 0;

for (int i =0; i < span; i++) 
  tempC1 = (1.1 * (analogRead(1) * 100.00)/1024.00);             //read the value from the sensor
  tempC2 = (1.1 * (analogRead(2) * 100.00)/1024.00);             //read the value from the sensor
Serial.print((byte)255);                     // preceed serial data with header "255" as identifier
Serial.print((byte)tempC1);                  //send the data to serial port
Serial.print((byte)tempC2);

tempF1 = (((tempC1*9)/5)+32);
tempF2 = (((tempC2*9)/5)+32);

lcd.setCursor(0,0);
lcd.print("Temp Sensor");

lcd.setCursor(0,1);
lcd.print("Temp 1 - ");
lcd.print(tempF1);
lcd.print(" F");

lcd.setCursor(0,2);
lcd.print("Temp 2 - ");
lcd.print(tempF2);
lcd.print(" F");

lcd.setCursor(0,3);
lcd.print("Average Temp ");
lcd.print((tempF1+tempF2)/2);

delay(1500);   //wait 1500ms before sending new data
}

Glad to see you took the initiative to do some debugging on your own. The fact that every thing works fine when you disable the lcd is a good indicator of what is going on. Take a look where declare your lcd class, what pin is pin 14? It's the same physical pin as A0.

Looking at your picture though you don't have anything hooked to A0, and the RW line is tied to ground , so you don't need to use the constructor with 7 arguments. Read http://arduino.cc/en/Reference/LiquidCrystalConstructor and you should find the answer to your problem. Hint your not using the RW line, look for the constructor that doesn't use it.

Thank you Egdinger, I didn't realize that I didn't actually have a pin 14 on the digital side (thus the code snippeting!). I re-read the LiquidCrystal constructor and removed the r/w definition, and re-assigned the pins back to Analog0 and Analog1 for my temp sensors, and everything is working correctly now!

Also, a big thank you for your approach in helping me. I do much better being told with a little guidance where to find the answer (through comprehension) rather than being given the answer and not knowing why it worked. You're a testament to how and why the open-source model works!

It's not very well described outside of the header files, but you can use all the analog in pins as digital io pins, they are pins 14-19.

One thing that I find helpful is to give all the pins useful alias towards the begining of the program, is forces you to have a understanding of what the pins are for and easier to spot if assign two things to the same pin. So for your code it would be something like

rs = 12 en = 11 d4 = 7 d5 = 8 d6 = 9 d7 = 10 tempSensor1 = A0 tempSensor2 = A1

Thanks for the alias’ing tip, I think I will make a habit of that. I didn’t realize the analog pins double as extra digitals; very good to know!

One other LCD-related question: I added a lcd.clear(); in my code, to clear the LCD on each refresh of the data displayed, because sometimes one of the values I’m displaying will go from 3 digits to 4, so when it goes back to 3 digits, that 4th digit on the end will stay. adding lcd.clear() does the trick, however it adds a slight flicker or blink to the LCD each time it clears (presumably the miniscule amount of time it takes for the loop() code to run. Is there a way to get rid of this slight blink?

An example of what i mean about the extra digit is this:
if on one refresh, the number displayed is 995, then on the next pass it is 1021, then the third refresh it is 999, it will continue to display the ending “1” from 1021, so the output of what should be “999” will be “9991”.

Here is my complete code so you can see what I’ve done exactly:

//declare variables
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 8, 9, 10);
int backlight = 2;
int IRsens;
int tempC1;
int tempC2;
int tempF1;
int tempF2;
int redLED = 3;
int grnLED = 4;

void setup()
{
Serial.begin(9600);
lcd.begin (19, 3);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop(){
analogReference(INTERNAL);
int span = 20;  // take x samples from sensor, to get accurate reading
int tempC1 = 0;
int tempC2 = 0;
int tempF1 = 0;
int tempF2 = 0;
int IRsens = 0;

for (int i =0; i < span; i++) 
  tempC1 = (1.1 * (analogRead(0) * 100.00)/1024.00);             //read the value from the sensor
  tempC2 = (1.1 * (analogRead(1) * 100.00)/1024.00);             //read the value from the sensor
  IRsens = (analogRead(5));
Serial.print((byte)255);                     // preceed serial data with header "255" as identifier
Serial.print((byte)tempC1);                  //send the data to serial port
Serial.print((byte)tempC2);

tempF1 = (((tempC1*9)/5)+32);
tempF2 = (((tempC2*9)/5)+32);

// analogWrite(backlight, (IRsens / 10) + 75);

 if (IRsens > 650)
  {
  digitalWrite(backlight, HIGH);
  }
  else
  {
  digitalWrite(backlight, LOW);
  }
  
 if (((tempF1+tempF2)/2) > 74)
  {
  digitalWrite(redLED, HIGH);
  digitalWrite(grnLED, LOW);
  }
  else
  {
  digitalWrite(redLED, LOW);
  digitalWrite(grnLED, HIGH);
  }

lcd.setCursor(0,0);
lcd.print("Temp Sensor     ");
lcd.print(IRsens);

lcd.setCursor(0,1);
lcd.print("Temp 1: ");
lcd.print(tempF1);
lcd.print(" F");

lcd.setCursor(0,2);
lcd.print("Temp 2: ");
lcd.print(tempF2);
lcd.print(" F");

lcd.setCursor(0,3);
lcd.print("Average Temp: ");
lcd.print((tempF1+tempF2)/2);
lcd.print(" F");

delay(1000);   //wait 1000ms before sending new data
lcd.clear();
}

Rather than clearing the whole display, you can just ensure that you always write at least 4 characters. The simplest way to do this is by printing 3 spaces after the value printed. That will ensure that there is nothing leftover in those spots.

Good call sir, that did the trick!