LCD Code causes Arduino to not execute void setup AND void loop

Hello!

I am currently using a I2C LCD2004 module from Sun Founder ( LINK .
Currently, all that is plugged into the Arduino is the USB from the laptop, and the 4 LCD pins.

I've noticed that any time I begin coding to use the LCD, the Arduino stops running in the void setup and void loop. I've confirmed this by creating a simple program without the LCD that caused the pin 13 light to blink on and off every second. In that code I had serial prints to prove the Arduino went through the void setup and void loop.

I then downloaded the simple test code from the above link and saw inserted serial prints. I noticed there that the void setup and void loop were not being executed.

I tried again and this time added a serial print before and after the LCD code and noticed once lines of code for the LCD were created the Arduino program halts. (SEE PHOTO)

A few days earlier I created a much more lengthy code that used a few push buttons, a light and the LCD and everything was working fine. Once I rewired everything so I could use the project in a production floor environment everything stopped working.

I appreciate any help!

Images of code are hard to work with. Please post code correctly. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Post a schematic of your wiring to show how it should be wired. Post photos of the wiring to show how it is wired. How to make a schematic that you can post.

There are several libraries with the name LiquidCrystal_I2C. The are not all the same and code from 1 may not run in another. Those LiquidCrystal_I2C libraries are old and most are not maintained. The newest and absolute best library for I2C LCD with the hd44780 controller (1602, 2004) is the hd44780 library by Bill Perry. For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.

To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

If you post your code so that it is easy to copy and paste into my IDE, I can modify your code to work with the, much better, hd44780 library. It is very easy and requires only changing 3 or 4 lines of code.

@sworsh, as you can upload code, your problem has nothing to do with Avrdude, stk500 or Bootloader issues. Hence your topic has been moved to a more suitable location on the forum.

/********************************
   name:I2C LCD2004
   function:You should now see your I2C LCD2004 display "Hello,world!","IIC/I2C LCD2004"
  "20 cols, 4 rows","www.sunfounder.com"
 ********************************/
//Email:service@sunfounder.com
//Website:www.sunfounder.com

/********************************/
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*********************************************************/
void setup()
{
  Serial.begin(9600);
  Serial.println("OKAY");
  delay(2000);
  lcd.init();  //initialize the lcd
  lcd.backlight();  //open the backlight

  lcd.setCursor ( 0, 0 );            // go to the top left corner
  lcd.print("    Hello,world!    "); // write this string on the top row
  lcd.setCursor ( 0, 1 );            // go to the 2nd row
  lcd.print("   IIC/I2C LCD2004  "); // pad string with spaces for centering
  lcd.setCursor ( 0, 2 );            // go to the third row
  lcd.print("  20 cols, 4 rows   "); // pad with spaces for centering
  lcd.setCursor ( 0, 3 );            // go to the fourth row
  lcd.print(" www.sunfounder.com ");
  Serial.println("Also");
}
/*********************************************************/
void loop()
{
  Serial.println("IN LOOP");
}
/************************************************************/

If it is a library problem, I'm confused as to why it was working literally yesterday morning and today it isn't.

Arudino is only letting me post one photo at a time so my apologies for the spam of posts.

The code I posted is basically just the test code provided by the supplier from the Link in my first post. It is not the full code that I'm using for my project, it is just a test code that I used to discover the setup and loop voids not working.

Try this code using the hd44780 library. You will need to install the library per the instructions in my first reply. This code works fine with my Uno and a 2004 LCD. I prints all the stuff in setup() and sends OKAY, Also, IN LOOP to serial monitor. I made it send IN LOOP only once so it does not spam serial monitor and also it will erase line one and print the current value of millis() once per second.

/********************************
   name:I2C LCD2004
   function:You should now see your I2C LCD2004 display "Hello,world!","IIC/I2C LCD2004"
   "20 cols, 4 rows","www.sunfounder.com"
 ********************************/
//Email:service@sunfounder.com
//Website:www.sunfounder.com

/********************************/
// include the library code
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

//LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
hd44780_I2Cexp lcd;

/*********************************************************/
bool loopFlag = false;

void setup()
{
   Serial.begin(9600);
   Serial.println("OKAY");
   delay(2000);
   //lcd.init();  //initialize the lcd
   lcd.begin(20, 4);
   lcd.backlight();  //open the backlight

   lcd.setCursor ( 0, 0 );            // go to the top left corner
   lcd.print("    Hello,world!    "); // write this string on the top row
   lcd.setCursor ( 0, 1 );            // go to the 2nd row
   lcd.print("   IIC/I2C LCD2004  "); // pad string with spaces for centering
   lcd.setCursor ( 0, 2 );            // go to the third row
   lcd.print("  20 cols, 4 rows   "); // pad with spaces for centering
   lcd.setCursor ( 0, 3 );            // go to the fourth row
   lcd.print(" www.sunfounder.com ");
   Serial.println("Also");
}
/*********************************************************/
void loop()
{
   if (loopFlag == false)
   {
      Serial.println("IN LOOP");
      delay(2000);
      loopFlag = true;
   }
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      lcd.setCursor(0,0);
      lcd.print("                    "); // clear old data
      lcd.setCursor(3,0);
      lcd.print("millis = ");
      lcd.print(millis());
   }
}
/************************************************************/

If all that you see is still just blocks, try adjusting the contrast.

Wiring looks OK.

I downloaded the library and copy pasted your code and the only thing that comes up in the serial monitor is OKAY.

Does that mean that you followed my instructions on how to install the library through library manager?

To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

If not, you need to install the library properly.

I tested the code before I posted it so I know that the code works.

Yup, I downloaded the hd44780 library from Bill Perry. Confirmed by researching for library and it is labeled as "INSTALLED"

I've also now switch the older Arduino for a newer (a REV3 Arduino) one. I update the COM Port and now I'm see that the sketch never finishes uploading. According to Arduino IDE, the program is still "Uploading" and it has been for the past several minutes. I'm not sure if this has anything to do with the void setups and void loops not being completely executed.

1 minute later:
Arduino gave up trying to upload and I'm now seeing the avrdude: stk500 error messages.

That has nothing to do with the code. That is a failure of the PC to communicate with the Uno. Make sure that the right board and port are selected in the IDE Tools menu. Try a different USB cable.

Okay, all I did was reset the Arduino board and the code uploaded. However, just like last time. Only thing that shows up in serial monitor is OKAY.

I thought that a bad jumper (SDA or SCL) might cause that to happen, but testing on my setup still prints OKAY, Also, IN LOOP in serial monitor even with one or both disconnected. So, I have no idea what is going on.

Try this. Run this I2C address scanner to see what comes up. Run the scanner, cut and past the serial monitor output to a new post, please.

// I2C scanner by Nick Gammon.  Thanks Nick.

#include <Wire.h>

void setup() {
  Serial.begin (115200); //*****  make sure serial monitor baud matches *****

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

16:11:50.237 ->
16:11:50.237 -> I2C scannK⸮⸮

Is your serial monitor baud rate set to 115200?

Is your serial monitor baud rate set to 115200?

It wasn't, just fixed. I'll send serial monitor in a second

Arduino limits post to new users, here is the serial monitor:

16:13:47.887 ->
16:13:47.887 -> I2C scanne
16:13:47.887 -> I2C scanner. Scanning ...