LCD display not working when I call Analogread()

I want to read the input on an analog pin and display a value based on this reading on an LCD display

I am using an 8x2 LCD which uses HD44780.

It works fine when I use the Hello World sketch http://www.arduino.cc/en/Tutorial/LiquidCrystal (modified slightly for 8 character display)

However, as soon as I add an Analogread command to the main loop the LCD stops working.

I have tried using different digital pins for the display and also all the analog pins but the same thing happens.

This works fine:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11,5,4,3,2);
int test = 0;

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(8, 2);
// Print a message to the LCD.
lcd.print("hello");
}

void loop() {
}

However, as soon as I change the loop to this:

void loop() {
test = analogRead(0); // this causes LCD to be blank
}

It stops the LCD working

Commenting out the analogread line then makes it work again

Can someone explain what might be the cause of the problem

Peter

Pin 0 is not an analogue pin, try A0.

Pin 0 is the serial Rx line which is on the same PORT D as your LCD, so this probably upsets the other port pins!

Thanks for the reply but my understanding is that analogread(0) and analogread(A0) are the same. I thought A0 was only necessary for digital operations on analog pins.

There is info here: arduino - analogRead(0) or analogRead(A0) - Electrical Engineering Stack Exchange

Anyway, I have tried A0 and every other analog pin so I don't think your answer solves it

Thanks anyway

Peter

Okay, I can't see anything else that may be wrong... which Arduino board are you using and which IDE version?

Does putting the analog Read before the LCD in the sketch do anything odd?

Try a different older IDE as there are some incompatibilities with some libraries with the newer 1.6.X IDE.

Problems with the newer IDE and LCD library has been noted by someone in another thread but no one seems to have been interested in investigating it...

Sorry, should have said - it's a Uno and I am using Arduino 1.05 IDE (actually its Visual Studio/Visual Micro but that uses the Arduino settings.)

If I take the LCD out and send data via serial.writeln it works fine. If I can't sort it I may try using a separate ADC

Peter

I don't have access to any Arduino hardware at the moment, but I will try your sketch with an old LCD I have and see if I can track down the root cause. In which case I will update this thread.

Lastly, for now, is that 1.0.5 or 1.0.5 r2 you are using?

This is just so I can use the exact same setup for testing.

1.0.5

I have done some more testing here.

I have a 16x2 display and when I hooked that up it behaved fine including analogread(A0).

However, with exactly the same pin connections on my 8x2 I get the analogread problem.

I can't understand this as supposedly it uses the same LCD controller chip. Despite that, it doesn't seem to be compatible unless there is a problem with the Liquidcrystal library that isn't happy with an 8x2 display

I will contact the people I bought the 8x2 display from

How odd. I suspect the display is fine but the library is doing something strange when an 8 x2 is called up. As the controller is common you could try 8x2 size settings in the sketch with the 16x2 LCD attached and see what happens.

It makes no difference. 16x2 display works fine on 16x2 and 8x2 modes.

8x2 display works fine in 16x2 and 8x2 modes but not if I do analogread

Identical pin settings in both cases

Peter

There is absolutely no difference in how the library deals with the 8x2 and the 16x2 devices. The first parameter is not used and the second is only checked to see if it is > 1. (The fact that the first parameter is not used is the reason why the library does not deal with 16x4 devices properly, but that is another issue entirely.)

I have no explanation for the phenomena that you are experiencing since, once you have displayed information on the LCD, it remains there until that information is overwritten. Reading information from an analog pin should have no effect on the LCD.

I suggest that you try using the Arduino IDE rather than by the supposedly 'fully compatible' Visual Micro and see if you get the same result.

Don

I already have used the Arduino IDE. Makes no difference

I'm going to try using a separate ADC as I've wasted enough time already :frowning:

Just had a thought

I wonder if the information is there but somehow the display has been blanked. I'm using fixed resistors instead of a potentiometer for the contrast. I'll have a play with that

Since your loop contains only that one statement you are doing that analog read over and over again really quickly. This shouldn't affect the LCD, but apparently it is. What happens when you put a delay in that loop?

Don

The fact remains though that it works with the 16 character display

I thought it was working with the 32 character display and not working with the 16 character display.

Don

Sorry I meant 16x2 so, yes, its the 32 one thats working but not 16 (8x2)

I have fixed the issue and I am embarrassed to admit to what the cause was. :-[

What I had done (and don't ask me why) was to power the trimmer for the LCD contrast from the ARef socket.

Now - I KNOW that isn't what its there for but it seemed to be providing a convenient source of 5V.

Now I have rewired it slightly so the 5V is coming from the correct rail and it works.

The reason why the other display worked is that it had an on-board contrast trimmer which was wired correctly :slight_smile:

Thanks for your help

Actually, the trimmer does not need 5V at all. It will actually work better with that end unconnected. A 2k2 variable resistor between VO and ground will work even more conveniently.

Now - I KNOW that isn't what its there for but it seemed to be providing a convenient source of 5V.

AREF is an input pin, it expects to receive a voltage not to supply one.

I wonder how long this would have taken to solve if we had been provided with sufficient information. Data sheets for the displays and a photograph of the setup come to mind.

Don