Flickering on lcd display

I am an arduino beginner and ran into a problem. I tried to control a lcd display with a button input, but when I upload my code the disply flickers. Here is my code:

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int buttonPin = 3;
const int ledPin =  2;

int buttonState = 0; 

void setup() {}

void loop() 
{
  buttonState = digitalRead(buttonPin);
  if(buttonState != 0) {
  lcd.begin(16,2);
  lcd.print("pressed");
  } else {
  lcd.begin(16,2);
  lcd.print("not pressed");
  }
}

I hope someone knows what is causing the flickering on the display

picture of setup:

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Why have you got lcd.begin() in loop() ?

Do it just once in setup()

add

delay(1000);

after each lcd.print and see if it flickers every second. if it does then you should reconsider looping the code

It would also be much better if you wrote to the LCD when the button state became HIGH or LOW rather than when it is HIGH or LOW

It flickers every second

When I do it ones in setup() not pressed is repeated, instead of updated

like this?

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int buttonPin = 3;
const int ledPin =  2;

int buttonState = 0; 

void setup() {}

void loop() 
{
  if(digitalRead(buttonPin) == HIGH) {
  lcd.begin(16,2);
  lcd.print("pressed");
  } else {
  lcd.begin(16,2);
  lcd.print("not pressed");
  }
}

Than I still have the flickering problem

Did you use the setCursor() function to position the cursor before printing to the LCD ?

See Arduino - LiquidCrystalSetCursor

Do you know what I can do instead?

Use setCursor() and only update the LCD message when the button state changes

As a matter of interest, how is the input wired ?

No, but I've changed my code and now it won't upload to my lcd anymore. In arduino ide it said: upload succesfull, but the code wasn't shown on the lcd.

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int buttonPin = 3;
const int ledPin =  2;

int buttonState = 0; 

void setup() {
  lcd.setCursor(0,0);
  }

void loop() 
{
  if(digitalRead(buttonPin) != 0) {
  lcd.print("pressed");
  } else {
  lcd.print("not pressed");
  }
}

The lcd has an I2C adapter. Ground goes from the lcd to the breadboard which is connected to ground on my arduino. The same for 5v. The output pins from the lcd are connected to my arduino in A4 and A5. The button is connected with a ressistor and has an output to pin 3. The rest of there to complete the circuits.

You need to use setCursor() just before you print your message. Once you do that you will have another query about why it seems to print "pressedssed", unless you deal with that yourself

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int buttonPin = 3;

int buttonState = 0; 

void setup() {}

void loop() 
{
  if(digitalRead(buttonPin) != 0) {
  lcd.setCursor(0,0);
  lcd.print("pressed");
  } else {
  lcd.setCursor(0,0);
  lcd.print("not pressed");
  }
}

This is my current code, but all the lcd does is: not pressed. It doesn't react to my button anymore. Also the display is not as bright as it was

How is the input wired ?
Is there a pulldown resistor in place or is the input floating at an unknown voltage when not pressed, maybe HIGH, maybe LOW

What do you see when you print the button state to the Serial monitor ?

One problem you may be running into is the fact that "pressed" or "not pressed" is being printed as fast as the microcontroller can handle.

#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int buttonPin = 3;
const int ledPin =  2;

volatile bool buttonIsPressed = false;

void setup() {
  lcd.setCursor(0,0);
  attachInterrupt(digitalPinToInterrupt(buttonPin), pressEvent, RISING);
}

void loop() {
  
  if(buttonIsPressed)
    myLCDprint("pressed");
  else
    myLCDprint("not pressed");

}


//Good practice to keep the ISR as short as possible
void pressEvent(){
  buttonIsPressed = true;
}

//Adding a delay after a print will help with flickering
void myLCDprint(const char *str){
  lcd.print(str);
  delay(100);
  buttonIsPressed = false;
}

now, the lcd will only re-write every 100 ms at most. By using interrupt service routines (ISR), the "print" message will only print once even if you have the button held down, so you won't get the "pressed" message multiple times when you only press the button once.

Your library is strange.
The LiquidCrystal_I2C library by Frank de Brabander has a completely different constructor.

LiquidCrystal_I2C (uint8_t lcd_Addr, uint8_t lcd_cols, uint8_t lcd_rows);

The lcd object is created simply

LiquidCrystal_I2C lcd (0x27,16,2);

The example from the library works well.
In setup()

lcd.init();
lcd.backlight();

And then you call

lcd.setCursor (0,0);
lcd.print ("Hello, world!");

where you need and everything works.
Check your library.

volatile bool

true! cheers!