L need help in Liquid Crystal I2C initialisation

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);// REMEMBER TO SCAN LCD ADDRESS PLEASE
#define dht_apin 8 // Analog Pin sensor is connected to
dht DHT;
const int light =9;
const int buzzer = 12;
const int fan = 7;
int irPin=6;
int exPin=10;
int count=0;
int excount=0;
int exstate=true;
boolean state = true;
int sound = 250;
void setup() {
pinMode(light, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd (init);
lcd.backlight();
lcd.begin(16,2);
pinMode(irPin, INPUT);
pinMode(exPin, INPUT);
lcd.print("Counter activated");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("People Inside");
}
void loop() {

if (!digitalRead(irPin) && state){
count++;
state = false;
lcd.setCursor(2,1);
lcd.print(count);
delay(100);
}
if (digitalRead(irPin)){
state = true;
delay(100);
}
if(count>=1){
digitalWrite(light, HIGH);
digitalWrite(fan, HIGH);
}

if (count>=6){
tone(buzzer, sound);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Number exceeded");
lcd.setCursor(0, 1);
lcd.print("Security alert!");
}

if(!digitalRead(exPin) && exstate){
count--;
exstate=false;
lcd.setCursor(2, 1);
lcd.print(count);
delay(100);
}
if (digitalRead(exPin)){
exstate = true;
}

if(count<=0){
digitalWrite(light, LOW);
digitalWrite(fan, LOW);
lcd.setCursor(0,0);
lcd.print("People Inside");
lcd.setCursor(0, 1);
}
if(count<0){
tone(buzzer, sound);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error:Intruder");
lcd.setCursor(0, 1);
lcd.print("Security alert!");
}
delay(200);

}

void checkTemp(){
int readData = DHT.read11(dht_apin);

int t = DHT.temperature; // Read temperature

Serial.print("Temperature = ");
Serial.print(t);
Serial.print("°C ");

  delay(5000);

digitalWrite(fan, HIGH);
delay(15000);
digitalWrite(fan, LOW);
delay(15000);

}

I'd try removing this line:

lcd (init);

@fortunate , your thread has been moved to a more appropriate section.

Can you please edit your post, select all code and click the < / > button above the text box and next save you post. It will show in a scrollable window and makes it easier for those that want to help to copy it; it will also prevent misinterpretation by the forum software.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Have you tried the contrast adjustment?

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.

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.

If this does not solve your problem, tell us what the problem is, post photos of the LCD showing the soldering of the LCD to the I2C backpack and the chips on the I2C backpack.

Actually, a very good way to start! :laughing:

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.