lcd does not name a type

I am trying to build a weather station with arduino and send the data to a site using esp8266 and also display some basic information also on my 16x2 lcd display but Iam getting this unsual error.

THE ERROR MESSAGE IS SHOWN BELOW

Arduino: 1.8.13 Hourly Build 2020/02/19 03:33 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

FINAL:156:3: error: 'lcd' does not name a type

 lcd.backlight();

 ^

FINAL:158:3: error: 'lcd' does not name a type

 lcd.createChar (6, W6);     // load arrow_d to memory 6

 ^

FINAL:159:3: error: 'lcd' does not name a type

 lcd.createChar (7, W7);     // load  to memory 7

 ^

FINAL:160:3: error: 'lcd' does not name a type

 lcd.createChar (9, W9);     // load to memory 7

 ^

FINAL:161:3: error: 'lcd' does not name a type

 lcd.createChar (10, W10);     // load diamond to memory 7

 ^

FINAL:162:3: error: 'lcd' does not name a type

 lcd.createChar (12, Battery);     // load diamond to memory 7

 ^

FINAL:163:3: error: 'lcd' does not name a type

 lcd.createChar (13, signaL2);     // load diamond to memory 7

 ^

FINAL:164:3: error: 'lcd' does not name a type

 lcd.createChar (14, signaL1);     // load diamond to memory 7

 ^

FINAL:166:3: error: 'lcd' does not name a type

 lcd.backlight(); // turn on back light

 ^

FINAL:167:3: error: 'lcd' does not name a type

 lcd.clear();

 ^

FINAL:170:2: error: 'lcd' does not name a type

lcd.setCursor(0,0); //set cursor to upper left corner

^

FINAL:171:2: error: 'lcd' does not name a type

lcd.print("Welcome");

^

FINAL:172:2: error: 'lcd' does not name a type

lcd.setCursor(9,0); //set cursor to upper left corner

^

FINAL:173:2: error: 'lcd' does not name a type

lcd.print(char(6));

^

FINAL:174:2: error: 'lcd' does not name a type

lcd.print(char(7));   //display a ohm

^

FINAL:176:2: error: 'lcd' does not name a type

lcd.setCursor(15,0); //set cursor to upper left corner

^

FINAL:177:2: error: 'lcd' does not name a type

lcd.print(char(12));   //display a ohm

^

FINAL:179:2: error: 'lcd' does not name a type

lcd.setCursor(14,0); //set cursor to upper left corner

^

FINAL:180:2: error: 'lcd' does not name a type

lcd.print(char(13));   //display a ohm

^

FINAL:181:2: error: 'lcd' does not name a type

lcd.setCursor(13,0); //set cursor to upper left corner

^

FINAL:182:2: error: 'lcd' does not name a type

lcd.print(char(14));   //display a ohm

^

FINAL:188:3: error: 'lcd' does not name a type

 lcd.setCursor(6,1); //set cursor to bottom left second line

 ^

FINAL:189:3: error: 'lcd' does not name a type

 lcd.print("To");

 ^

FINAL:190:3: error: 'lcd' does not name a type

 lcd.setCursor(9,1); //set cursor to bottom left second line

 ^

FINAL:192:3: error: 'lcd' does not name a type

 lcd.print(char(9));

 ^

FINAL:193:3: error: 'lcd' does not name a type

 lcd.print(char(10));

 ^

FINAL:194:3: error: 'lcd' does not name a type

 lcd.setCursor(10,1); 

 ^

FINAL:195:3: error: 'lcd' does not name a type

 lcd.print("refarm"); 

 ^

FINAL:196:8: error: expected constructor, destructor, or type conversion before '(' token

 delay(3000);  

      ^

FINAL:197:3: error: 'lcd' does not name a type

 lcd.clear();

 ^

FINAL:198:3: error: expected declaration before '}' token

 }

 ^

Multiple libraries were found for "LCD.h"
Used: C:\Users\USER.000\Documents\Arduino\libraries\NewLiquidCrystal_lib
Not used: C:\Users\USER.000\Documents\Arduino\libraries\NewliquidCrystal
Multiple libraries were found for "ESP8266WebServer.h"
Used: C:\Users\USER.000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\ESP8266WebServer
Not used: C:\Users\USER.000\Documents\Arduino\libraries\ESPWebServer-master
exit status 1
'lcd' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You're probably* missing a line like:

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

The exact format and content of that line will depend on which lcd library you are using. (That line is from the liquid crystal example that comes with the ide.)

  • I say probably, since without seeing your code it's impossible to say.

You need to declare the "lcd" instance. It depends on your LCD type and LCD library you used.
See step-by-step instruction in Arduino - LCD and Arduino - LCD I2C tutorials

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <dht.h>
#include <Wire.h>
#include <LCD.h>
//#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);

dht DHT;
#define DHT11_PIN 11
int lit=3;
int Rn=1;
int moist=0;
int rel=2;
int red_LED=8;
int blue_LED=9;
int green_LED=7;
int buzzer=6;

//-------------------------------------------------------------------------------------------------------------------------------

/* Set these to your desired credentials. */
const char *ssid = "";  //ENTER YOUR WIFI SETTINGS
const char *password = "";

//Web/Server address to read/write from 
//const char *host = "http://192.168.43.102:8000";  //localy
const char *host = "http://prefarmapi.herokuapp.com";   //https://circuits4you.com website or IP address of server

//=======================================================================
//                    Power on setup
//=======================================================================



void setup(){
// put your setup code here, to run once:
pinMode(lit, INPUT);
pinMode(Rn, INPUT);
pinMode(moist, INPUT);
pinMode(red_LED, OUTPUT);
pinMode(blue_LED, OUTPUT);
pinMode(green_LED, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(rel, OUTPUT);
Serial.begin(115200);
lcd.begin(16,2);
WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot

WiFi.begin(ssid, password);     //Connect to your WiFi router
Serial.println("");

Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
}

//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
lcd.backlight();

lcd.createChar (6, W6);     // load arrow_d to memory 6
lcd.createChar (7, W7);     // load  to memory 7
lcd.createChar (9, W9);     // load to memory 7
lcd.createChar (10, W10);     // load diamond to memory 7
lcd.createChar (12, Battery);     // load diamond to memory 7
lcd.createChar (13, signaL2);     // load diamond to memory 7
lcd.createChar (14, signaL1);     // load diamond to memory 7
        //clear the screen
lcd.backlight(); // turn on back light
lcd.clear();

//FIRST COLUMN
lcd.setCursor(0,0); //set cursor to upper left corner
lcd.print("Welcome");
lcd.setCursor(9,0); //set cursor to upper left corner
lcd.print(char(6));
lcd.print(char(7));   //display a ohm

lcd.setCursor(15,0); //set cursor to upper left corner
lcd.print(char(12));   //display a ohm

lcd.setCursor(14,0); //set cursor to upper left corner
lcd.print(char(13));   //display a ohm
lcd.setCursor(13,0); //set cursor to upper left corner
lcd.print(char(14));   //display a ohm



//SECOND COLUMN

lcd.setCursor(6,1); //set cursor to bottom left second line
lcd.print("To");
lcd.setCursor(9,1); //set cursor to bottom left second line

lcd.print(char(9));
lcd.print(char(10));
lcd.setCursor(10,1); 
lcd.print("refarm"); 
delay(3000);  
lcd.clear();
}

void loop()
{
HTTPClient http;    //Declare object of class HTTPClient
String postData;
 String light,moisture,rain,temperatur,humidity;
// put your main code here, to run repeatedly:
digitalWrite(green_LED, HIGH);   // turn the LED on (HIGH is the voltage level)

//TEMPERATURE SENSOR
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");



//HUMIDITY SENSOR
lcd.setCursor(0,1);
lcd.print("Humdity:");
lcd.print(DHT.humidity);
lcd.print("%");
delay(5000);
lcd.clear();



//RAIN SENSOR  
lcd.setCursor(0,0);
lcd.print("Rain:");
Rn=analogRead(1);    
lcd.print(Rn);
lcd.print("mm");    
if (Rn<=500)  
   {
     analogWrite(rel,HIGH);
     digitalWrite(blue_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
     tone(buzzer, 1000);
     delay(200);                       // wait for a second
     digitalWrite(blue_LED, LOW);    // turn the LED off by making the voltage LOW
     noTone(buzzer);
                          // wait for a second      
   }
 
 else
   {
     analogWrite(rel,LOW);
     }



 //LIGHT SENSOR
 lcd.setCursor(0,1);
 lit=analogRead(3);
 lcd.print("Light:");
 lcd.print(lit);
 lcd.print("cd");   
 delay(5000);
 lcd.clear();


 //MOISTURE SENSOR
 lcd.setCursor(0,0);
 moist=analogRead(0);
 lcd.print("Moisture:");
 lcd.print(moist);
 lcd.print("%");
 if (moist<=500)
   {
     analogWrite(rel,HIGH);
     digitalWrite(red_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
     tone(buzzer, 700);
     delay(800);                       // wait for a second
     digitalWrite(red_LED, LOW);    // turn the LED off by making the voltage LOW
     noTone(buzzer);
                            // wait for a second      
   }
 
 else
   {
     analogWrite(rel,LOW);
             
    }
 light= String(lit);
moisture= String(moist);
rain= String(Rn);
temperatur= String(chk);

  postData = "temperature =" + temperature + "&moisture=" + moisture  + "&rain=" + rain + "&light=" + light + "&humidity=" + humidity  ;
http.begin("http://prefarmapi.herokuapp.com/api/espdata");              //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
//    http.setAuthorization("Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjNlZDIxMDY1MWY3NTU5YjcxNmUzMTA1M2E2YzNmNGNkMzllNTMwNjU0YTZhNGE4M2E3YTVmMjNmMjZhZmZiYmI2YmMxNWVhOGEwOTEyOWNiIn0.eyJhdWQiOiIxIiwianRpIjoiM2VkMjEwNjUxZjc1NTliNzE2ZTMxMDUzYTZjM2Y0Y2QzOWU1MzA2NTRhNmE0YTgzYTdhNWYyM2YyNmFmZmJiYjZiYzE1ZWE4YTA5MTI5Y2IiLCJpYXQiOjE1ODkxMzQ0NzcsIm5iZiI6MTU4OTEzNDQ3NywiZXhwIjoxNjIwNjcwNDc3LCJzdWIiOiIxIiwic2NvcGVzIjpbX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CJzdWIiOiIxIiwic2NvcGVzIjpbXX0.M2EPuNhtazAkaNq9TclEQUcapBnTLKjqhCUfpxRccBIBFHggYXpImbQdHYNyTGxKdjlddspnuxQEtJWhJ_PbeSXUeGgxdG3Eqg0O1avIJ6vThFQKzPiH4nXltsgFnC8m97H_9XMohj41KsELeGZZRbW_xSP2f8_Z2yqrUUNzxhp6u0TCllOj9--IN5jAPR-cyIOEj5UdRyjdLVYgJh7rpE8GhW-WVgTCQQlPN0VtDhOnbgbVTW2cMuls07O47fc24qi6IqMTCFIAnkkc_lxWhXNfhal5BWhimK-EYckMcRHTviGu5ok9OAUCrkVV9TKIpgaXabtAunbcS6Ndc2snm3TaDz7RsdyiulcbelDXTQiz95GevuqAWCKYulA4MM3bb6M6R46UthWsIjeZcVOp_sBfl2fFw6A1ZSxQsZ4z5BjBpajcKTgVG1DLuftWkYP1QWzlX8v3Z88nPcNdkn0rTYtJlgK-UV0LJsSoP5bunDPpV27Ce0bn7T95maZvWlZp_kjqfyy7jrlWJGj8kx22_gwOeSCh6-CxP0xwOcQdd8tSrFzxvEoKWo7FudYpBAbj1ipH7nXa3sbf6Tu994JmBBKe_eHxavqvxuFQTpgsyIPnd_rAXIsRBPKD9LK0HPLJi6piKM7W3ZxtjGfiSyq-COzPU84aL2UCJfV8ifmhLHs");

int httpCode = http.POST(postData);   //Send the request
String payload = http.getString();    //Get the response payload

Serial.println(httpCode);   //Print HTTP return code
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.println(payload);    //Print request response payload

http.end();  //Close connection

delay(5000);  //Post Data at every 5 seconds

lcd.clear();
 

}

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.

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.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

noiasca:
step one: Load an I2C scanner and check if your ESP can communicate with the LCD. What's the correct adress?

step two: use a basic strand test from the LCD library and get the LCD working. No HTTP Client - NOTHING. Just the LCD

Do this basic steps and tell how far you get.
If you fail - describe where you fail and post clear pictures of your modules and your wiring where we can see EACH single wire and wire.

it's working now thanks

Hello. I am having the same issue where I get the error " 'lcd' does not name a type", however, i have used the code several times and it worked. In fact, I have declared lcd and have used it numerous times within the same code prior to getting the error. Any suggestions? The code is below. The error is near the end "lcd.setCursor(15,1);"

#include <Wire.h> //wire library
#include <LiquidCrystal_I2C.h> //liquid crystal with I2C library

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

char array1[] = "Hello Dana!"; //an array to print on line 0
char array2[]="You did it!"; //an array to print on line 1
char array3[]="Oops!";
char array4[]="1 Bit at a time!";
int tim = 500;

void setup()
{
  lcd.init();                      // initialize the lcd 
  
  lcd.backlight(); //Turn on backlight

  //show that I2C only accepts one byte at a time
  lcd.setCursor(0,0);//set cursor at top left
  lcd.print(array1);
  lcd.setCursor(0,1);//set cursor at left space on 2nd row
  lcd.print(array2);
  delay(2000);
 
}

void loop() 
{ 
  lcd.clear();
  lcd.setCursor(0,0);
  for ( int positionCounter1 = 0; positionCounter1 < 6; positionCounter1++)
  {
  lcd.scrollDisplayRight();
  lcd.print(array3[positionCounter1]);
 // delay(2000);
 // lcd.clear();
  }
{ 
  lcd.setCursor(0,1);
 for (int positionCounter2 = 0; positionCounter2<17; positionCounter2++)
 {
  lcd.scrollDisplayRight();
  lcd.print(array4[positionCounter2]);
 }
  delay(2000);
  lcd.clear();
 }
}
    lcd.setCursor(15,0);  // set the cursor to column 15, line 0
    for ( int positionCounter1 = 0; positionCounter1 < 11; positionCounter1++)
    {
      lcd.scrollDisplayLeft();  //Scrolls the contents of the display one space to the left.
      lcd.print(array1[positionCounter1]);  // Print a message to the LCD.
      delay(tim);  //wait for 250 microseconds
    }
    delay(1000);
    lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.
    lcd.setCursor(15,1);  // set the cursor to column 15, line 1
    for (int positionCounter2 = 0; positionCounter2 < 11; positionCounter2++)
    {
      lcd.scrollDisplayLeft();  //Scrolls the contents of the display one space to the left.
      lcd.print(array2[positionCounter2]);  // Print a message to the LCD.
      delay(tim);  //wait for 250 microseconds
    }
    delay(5000);
    }
    lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.
}

The IDE has an autoformat tool (ctrl-t or Tools, Auto Format). Use the autoformat tool on your code and you will see the problem ( an extra } that closes the loop() function prematurely right before that setCursor function).

edit: actually there are 2 extra closing brackets (}). Autoformat will find them both.

You should always run the autoformat tool on your code before posting. It really aids readability.

Thank you! I am very new to this. I didn't know about the autoformat tool.

Another IDE tool is that if you put the cursor to the right of a curly bracket or parenthesis the matching symbol will be highlighted. Can be handy, at times, to see mismatched symbols.