In trying to do the project in the above book on the water level sensor, I got the following error.
Sketch
`// include the library code: #include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resval = 0; // holds the value
int respin = A5; // sensor pin used
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop() {
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to
resval variable
if (resval<=100){ lcd.println("Empty "); } else if (resval>100 &&
resval<=300){ lcd.println("Low "); } else if (resval>300 && resval<=330){
lcd.println("Medium "); } else if (resval>330){lcd.println("High");}
delay(1000);
}
Error Message
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino: In function 'void setup()':
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:8:1: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino: In function 'void loop()':
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:14:1: error: 'lcd' was not declared in this scope
lcd.setCursor(0, 1);
^~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:16:8: error: expected ';' before 'variable'
resval variable
^~~~~~~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:17:44: error: 'else' without a previous 'if'
if (resval<=100){ lcd.println("Empty "); } else if (resval>100 &&
^~~~
exit status 1
Compilation error: 'lcd' was not declared in this scope
//initialise the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Where is the code that this comment refers to ?. From the error message saying that 'lcd' has not been declared I assume that it should have been declared in that code
Another error
resval variable
This line of code should be on the previous line as part of the comment
I have tided up the code and it compiles but I cannot test it
// include the library code:
#include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resval = 0; // holds the value
int respin = A5; // sensor pin used
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to resval variable
if (resval <= 100)
{
lcd.println("Empty ");
}
else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
else if (resval > 300 && resval <= 330)
{
lcd.println("Medium ");
}
else if (resval > 330) { lcd.println("High"); }
delay(1000);
}
I was able to upload the sketch to my board all right but then nothing appeared on the display. Not the water level line, nor any values. I'm enclosing a photo of my setup because I wonder if the picture in the book and the code don't match up. Thanks again for your all your help.
You actually need to set up the correct address for the display, similar to "LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address old display".
There is a sample program with your IDE that will find and display the address of your diaplay.
Yes. I don't know what all the extra parameters in the example are for, usually it's simple like
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
The program @moecat might use to verify the address and see if the display is hooked up correctly is called an i2c scanner and google will turn up a few variations.
But at least you do have to use the code that is correct for an I2C display as opposed to the older fashioned so-called four or eight wires interface.
And of course not bungle copy pasting the code so critical elements get absorbed by comments…
// include the library code: #include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(A4, A5);
int resval = 0; // holds the value
int respin = A5; // sensor pin used
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to resval variable
if (resval <= 100)
{
lcd.println("Empty ");
}
else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
else if (resval > 300 && resval <= 330)
{
lcd.println("Medium ");
}
else if (resval > 330) { lcd.println("High"); }
delay(1000);
}
Error Message
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:4:1: error: 'LiquidCrystal_I2C' does not name a type; did you mean 'LiquidCrystal_h'?
LiquidCrystal_I2C lcd(A4, A5);
^~~~~~~~~~~~~~~~~
LiquidCrystal_h
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino: In function 'void setup()':
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:10:5: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^~~
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino: In function 'void loop()':
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:17:5: error: 'lcd' was not declared in this scope
lcd.setCursor(0, 1);
^~~
exit status 1
Compilation error: 'LiquidCrystal_I2C' does not name a type; did you mean 'LiquidCrystal_h'?
I just noticed that. If @moecat copied the patterns from a working example, perhaps there is a different library… the few I just looked at did not seem to have calls that would default to a certain I2C address, nor calls to use I2C pins other than the hardware I2C at A4 and A5.
@moecat please read #2 above before you post code anymore.
Have you gotten the display to work using code you did not write or mess with?
Where did you exactly get the library you are using?
Have you tried any I2C scanner as advised?
Gack! I just now see
Using A5 for both analog input and I2C signal SCL is not gonna go well. Move your sensor to an unused analog input pin.
It looks like your book was using an old fashioned interface and you have been left to struggle heroically converting it to I2C.
TBH liquid crystal displays can be a challenge, which is why the first stop always should be to,use code you did neither write nor mess with and get to "Hellish World!".
It suddenly dawned on me when I saw the picture of the setup provided by xfpd, that this code actually applies to an older version of LCD display. So I tried wiring it up as in the picture, and using the corrected code provided by UKHeliBob, but I still got no display of the words "water level". What could I have missed? See the photo I provided of my setup.
Plus I'm wondering why the LCD only shows one row lit up. Shouldn't there be two rows? Thanks again for all the help.
I went back to my original project with the LCD to compare the code to the present project. I tried to copy the original code as much as possible, and it uploaded just fine. But it still won't display "Water Level" or any values.What is going on? Here is my code .
// include the library code: #include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int resval = 0; // holds the value
int respin = A0; // sensor pin used
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to resval variable
if (resval <= 100)
{
lcd.println("Empty ");
}
else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
else if (resval > 300 && resval <= 330)
{
lcd.println("Medium ");
}
else if (resval > 330) { lcd.println("High"); }
delay(1000);
}