Upload code twice for LCD display to work

Hello,

I have an Arduino Due and an LCD 16x2 HD44780 display. Everything is working correctly, with any code, except for one thing: I need to upload the code twice onto the Arduino. Otherwise, the display only shows Chinese characters (see image below). It happens with any code, but there’s an example below.
I have checked the wiring countless times, and it is correct according to:

  1. LCD GND → Arduino GND
  2. LCD VDD → Arduino 5V
  3. LCD V0 → Arduino pin 6
  4. LCD RS → Arduino pin 12
  5. LCD R/W → Arduino GND
  6. LCD E → Arduino pin 11
  7. LCD DB0 → Not connected
    8 ) LCD DB1 → Not connected
  8. LCD DB2 → Not connected
  9. LCD DB3 → Not connected
  10. LCD DB4 → Arduino pin 5
  11. LCD DB5 → Arduino pin 4
  12. LCD DB6 → Arduino pin 3
  13. LCD DB7 → Arduino pin 2
  14. LCD BL+ → 220 Ohm resistor → Arduino 5V
  15. LCD BL- → Arduino GND
// 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 Contrast=15;

void setup() {
  lcd.begin(16, 2);
  lcd.clear(); // clear and cursor in upper left corner
  lcd.setCursor(0, 0);
  lcd.write("Hello, world");
  lcd.setCursor(0, 1);
  lcd.write("Hello, me");
  analogWrite(6,Contrast);
  Serial.begin(9600);
}

void loop() {
}

LCD Display pinout:
http://www.uctronics.com/download/1602.pdf

It is not a major problem, since it works perfectly after uploading twice, but it’s annoying and I’m curious to know why it happens.
Thank you for your help!

Does it work differently when you get rid of the extraneous code in your sketch?

// 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 Contrast=15;

void setup() {
  lcd.begin(16, 2);
//  lcd.clear(); // clear and cursor in upper left corner
//  lcd.setCursor(0, 0);
  lcd.write("Hello, world");
  lcd.setCursor(0, 1);
  lcd.write("Hello, me");
// analogWrite(6,Contrast);
//  Serial.begin(9600);
}

void loop() {
}

Don

floresta:
Does it work differently when you get rid of the extraneous code in your sketch?

// 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 Contrast=15;

void setup() {
 lcd.begin(16, 2);
//  lcd.clear(); // clear and cursor in upper left corner
//  lcd.setCursor(0, 0);
 lcd.write(“Hello, world”);
 lcd.setCursor(0, 1);
 lcd.write(“Hello, me”);
// analogWrite(6,Contrast);
//  Serial.begin(9600);
}

void loop() {
}




Don

Thank you for your reply. With that piece of code, it doesn’t display anything, since the contrast is not set properly.

Interesting. What happens if you upload your original code once, then press [RESET] on the Due? Does it still display garbage, or print to the LCD correctly without needing to upload again?

Also, have you tried a short delay in 'setup()', before 'lcd.begin'? An LCD module takes time to initialise, although I would think that usually the library would take care of that. When NOT using a library, I use a 500mS delay.

On an aside, I'd move this to the beginning of 'setup()':-

analogWrite(6, Contrast);

(It's not your current problem though.)

Thank you for your reply. With that piece of code, it doesn't display anything, since the contrast is not set properly.

How about providing a complete description of what you are using since right now, with the information we have, we are merely providing guesses.

Here are some observations and more guesses:

Since you can get the device to work properly some of the time it would appear that your wiring is correct.

It also looks like whatever scheme you are using for setting the contrast is working as well.

You have made the troubleshooting a lot easier by doing everything in setup() and leaving loop() empty.

I don't know why you are initializing the serial port but I believe that also resets the UNO which may be messing things up. If you absolutely must include that initialization for some reason then try putting it at the beginning of setup() as mentioned in the previous post.

I notice that the word 'Hello', which appears on both lines, gives the same the unusual (but not Chinese) characters in both cases. Also the same unusual character appears for both of the doubled 'l' characters. This implies that your device is working consistently, not randomly.

The real explanation is probably that your LCD itself is of questionable quality. Most likely it is out of specification and that is why you are getting erratic results. The LiquidCrystal library does not deal with such a situation very well, especially when running in 4-bit mode. What is happening is that somewhere along the line you are losing one 4-bit nibble and your characters then are being sent out of synch. (If you don't understand this part I can put together an example. I don't have time right now!)

Don

floresta:
The real explanation is probably that your LCD itself is of questionable quality. Most likely it is out of specification and that is why you are getting erratic results.

I think it might first be worth trying setting the contrast correctly. With a 1k variable resistor from ground to Vo pin 3. Using PWM to set the contrast is not a valid approach.

I'm baaaaack. It's definitely a case of a dropped nibble.

It's easy to see where the word 'Hello' is (trying to be) displayed due to the double 'l'. Now take a look at the binary values for the ASCII codes for some of those characters compared to the binary values for the ASCII codes for the corresponding characters that are actually being displayed, shifted by one nibble.

                     e........ l........ l........ o........ comma....
ello,          -->   0110 0101 0110 1100 0110 1100 0110 1111 0010 1100


                          V........ ......... ......... theta.... .........
His characters -->        0101 0110 1100 0110 1100 0110 1111 0010 1100 0010

Don

floresta: I'm baaaaack. It's definitely a case of a dropped nibble.

It's easy to see where the word 'Hello' is (trying to be) displayed due to the double 'l'. Now take a look at the binary values for the ASCII codes for some of those characters compared to the binary values for the ASCII codes for the corresponding characters that are actually being displayed, shifted by one nibble.

                     e........ l........ l........ o........ comma....
ello,          -->   0110 0101 0110 1100 0110 1100 0110 1111 0010 1100

                          V........ ......... ......... theta.... ......... His characters -->        0101 0110 1100 0110 1100 0110 1111 0010 1100 0010



Don

Well-spotted, Don.

OldSteve: Well-spotted, Don.

Not difficult for Don, because it has been documented previously and he has a good recall. :grinning:

Has the OP tried simply uploading once, then disconnecting the power, thus resetting the HD44780?

Paul__B: Not difficult for Don, because it has been documented previously and he has a good recall. :grinning:

:D

I can't even remember what I had for breakfast so it took me a while to find any of the previous threads where this has come up. Here's one from about two years ago. --> http://forum.arduino.cc/index.php?topic=297040.msg2072531#msg2072531

Don

floresta: I can't even remember what I had for breakfast.....

You're not the only one. :)

floresta:
I can’t even remember what I had for breakfast so it took me a while to find any of the previous threads where this has come up. Here’s one from about two years ago

Or almost identical.

Would have posted that earlier, except for that annoying thing they call “work”. :astonished:

Or almost identical.

They go to the same thread. Your link goes to the original post, mine goes to my answer at reply #9.

I guess I should propose some solutions. One is to use an 8-bit interface.

Another is to use a library called LiquidCrystal1.0 . This is the updated version of LiquidCrystal440 which, while written to deal with 40x4 displays also handles slow (cheap) displays as well. To get a copy start here:--> http://code.google.com/p/liquidcrystal440/ and follow the [u]Downloads[/u] link to get to the latest version.

Don

I thought they looked rather similar - I was looking at the wrong numbers in the URL. :grinning:

So I was in a hurry to get to lunch, wife breathing over shoulder.

floresta:
I guess I should propose some solutions. One is to use an 8-bit interface.

Nah!