Help getting LCD16x2 to print the Serial Monitor

I've got a project that I've been working on that has a PIR Motion Sensor that triggers a Buzzer and LED. That part works perfectly but I want to add a LCD16x2 that prints what the Serial Monitor says.

When the PIR is triggered, the Serial Monitor displays "Motion Detected!", and then "Motion Stopped!". I used the Arduino Tutorial to make this:

This is the code I have so far. Everything but the LCD works. It turns on but doesn't display anything.\

#include <LiquidCrystal_I2C.h> // Library for LCD
const int MOTION_SENSOR_PIN = 7;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int BUZZER_PIN        = 3;   // Arduino pin connected to Buzzer's pin
int motionStateCurrent      = LOW; // current  state of motion sensor's pin
int motionStatePrevious     = LOW; // previous state of motion sensor's pin

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows

void setup() {
  lcd.init(); // initialize the lcd
  lcd.backlight(); 
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(BUZZER_PIN, OUTPUT);          // set arduino pin to output mode
}

void loop() {
  motionStatePrevious = motionStateCurrent;            // store old state
  motionStateCurrent  = digitalRead(MOTION_SENSOR_PIN); // read new state

  if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    lcd.print("Motion detected!");
    digitalWrite(BUZZER_PIN, HIGH); // turn on
  }
  else
  if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    lcd.print("Motion stopped!");
    digitalWrite(BUZZER_PIN, LOW);  // turn off
  }
}

I attached a picture of the wiring. I tried to make it as clear as possible.

Edit: I had to link the picture:
Edit 2: I can't figure out how to make it a link so you'll have to copy and paste it. Sorry.

OK, so have you practised using a "Hello World" example to operate the display? This is clearly the first step before integrating display code into another program.

It is recommended that you install the "HD44780" library using the Library Manager in the IDE and use the examples that come with that library to operate your I2C 1602 display. Once that is working, you will have the appropriate code to include your other functions.

Once you can get the backlight operative (did you try connecting just ground and Vcc to the display?) you can adjust the contrast potentiometer to get either blocks on the first row if the code is not initialising the display, or the desired text is it is successful.

The I2C pins on a Mega are SDA pin 20 and SCL pin 21 or the I2C pins me t to Aref. It looks like you are wired to digital pins 4 and 5.

Once you install the hd4480 library, take some time to read the included documentation, then run the included diagnostic sketch, I2CexpDiag. It will test everything to make sure things are working. It will also print out the Arduino pins used for SDA and SCL pins for your board so you can be sure you have the connected wires to the proper Arduino pins.

--- bill

Paul__B:
OK, so have you practised using a "Hello World" example to operate the display? This is clearly the first step before integrating display code into another program.

It is recommended that you install the "HD44780" library using the Library Manager in the IDE and use the examples that come with that library to operate your I2C 1602 display. Once that is working, you will have the appropriate code to include your other functions.

Once you can get the backlight operative (did you try connecting just ground and Vcc to the display?) you can adjust the contrast potentiometer to get either blocks on the first row if the code is not initialising the display, or the desired text is it is successful.

Yeah, I've done most of the examples. Including multiple others. I got confused because some of them declared the pins and some didn't like the one this was based off of.

I'm gonna try the suggestion that someone said.

Also, it wasn't turned on in the pic. The backlight does turn on. Just no words displayed.

Did you move the SDA pin to pin 20 and the SCL pin to pin 21?

scwolves10:
Yeah, I've done most of the examples. Including multiple others. I got confused because some of them declared the pins and some didn't like the one this was based off of.

The initialisation code must match the working examples; you cannot use initialisation code that was for a different system/ library. :astonished:

groundFungus:
Did you move the SDA pin to pin 20 and the SCL pin to pin 21?

Yeah. Still just the backlight on and no display. I also tried using the hd44780 library like someone suggested but kept getting the error "Wire.h was not declared" even though Wire is a basic library. So I downloaded a new version and got the error "There are multiple folders". And I can't find the basic Wire.h library. It's not with the others.

scwolves10:
I also tried using the hd44780 library like someone suggested but kept getting the error "Wire.h was not declared" even though Wire is a basic library.

You were using the example code in the HD44780 library?

You will need to be using a recent version of Arduino IDE.

Paul__B:
You were using the example code in the HD44780 library?

You will need to be using a recent version of Arduino IDE.

For that try I did. Which made no sense to me. I decided to try the non-I2C LCD and still get nothing besides the backlight turning on. I'm just confused at this point.

scwolves10:
Yeah. Still just the backlight on and no display. I also tried using the hd44780 library like someone suggested but kept getting the error "Wire.h was not declared" even though Wire is a basic library. So I downloaded a new version and got the error "There are multiple folders". And I can't find the basic Wire.h library. It's not with the others.

This error doesn't make sense when compiling the I2CexpDiag sketch, which is what you were doing right?

Did you install the hd44780 library using the IDE library manager from the IDE GUI without doing any downloads or messing the zip files?

Show the full compiler output you got.

--- bill

Bill to the rescue!

(Author of the HD44780 library.)

Yeah, that "Wire.h was not declared" error hasn't been seen for years! :astonished:

I saw that error yesterday. It was because the W wasn't capitalised.

AJLElectronics:
I saw that error yesterday. It was because the W wasn't capitalised.

I seriously doubt it. If the name of an included header is misspelled by using wire.h instead of Wire.h and you were not using Windows, you would get:

wire.h: No such file or directory

not "Wire.h was not declared".

I have not ever seen that error: ""Wire.h was not declared", nor do I have any idea how to create that specific error.
If that is the actual error (I'm guessing it isn't), it is likely from either creating or using code that doesn't compile due to syntax errors.

--- bill

bperrybap:
I have not ever seen that error: ""Wire.h was not declared", nor do I have any idea how to create that specific error.
If that is the actual error (I'm guessing it isn't), it is likely from either creating or using code that doesn't compile due to syntax errors.

With your vast experience and my inexperience with programming, I wouldn't disagree with you. I had the error, changed the capitalisation and it went away. It is therefore likely that something else was changed in the editing but I didn't note anything else at the time.

AJLElectronics:
I had the error, changed the capitalisation and it went away. It is therefore likely that something else was changed in the editing but I didn't note anything else at the time.

I'll be a bit more direct. If you changed the capitalization of a header file name and the error went away.
The exact error message you got was not "Wire.h was not declared".

"was not declared" is type of error that relates to using a variable or object that, well, hasn't been declared.
Something more likely would be:

'Wire' was not declared in this scope

based on a needed header file, like Wire.h not being included.

Having full and accurate information is vital when trying to debug issues.
Inaccurate information is often worse than no information.
This is why it is always best to post the actual full output from the compiler rather than attempt to summarize it.
Not only will this provide the exact error message, but it will also show any/all other messages that lead up to that error message and quite often, there is another issue/error that was indicated prior to the error message in question that is the real cause of the issue.

--- bill