LCD screen with push button don't show text [SOLVED]

I am new to arduino and trying to make a lcd l2c screen turn on and off and that is possible with the code below. But i cant print on the screen when connected to button, without button it works. Sorry for the messy connection because breadboard is used.
IMG_0532
IMG_0533

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

LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27


/////// Constant Variables Intialised ///////
const int LED = 13;
const int Button = A1;

/////// Changeable Variables Intialised ///////
int ButtonState = 0;      // take current button state
int LastButtonState = 0;  // take last button state
int LEDState = 0;         // take light status

void setup() {

  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop() {

  ButtonState = digitalRead(Button);

  if (LastButtonState == 0 && ButtonState == 1) {
    if (LEDState == 0) {
      digitalWrite(LED, HIGH);
      LEDState = 1;
      lcd.println("HELLO!");
    }

    else {
      digitalWrite(LED, LOW);
      LEDState = 0;

    }
  }
  LastButtonState = ButtonState;
  delay(100);
}

where do you clear the screen once you've written "HELLO"?

Can you clarify how you wired the button? with INPUT_PULLUP you get LOW when pressed if you wired
Arduino pin <--> button <--> GND

"HELLO" was just writen to see if i cound print text

Button is wired,

Arduino pin A1 <--> button <--> GND
 button <--> resistor 10k <--> 5V

im not sure if wired correct

you don't need that part

just

Arduino pin A1 <--> button <--> GND

but then because it's pulled up, it's HIGH when you don't press and LOW when you press, so that test

  if (LastButtonState == 0 && ButtonState == 1) { // would have been better with LOW and HIGH instead of 0 and 1

reads "if it was pressed and now is released", so it's not detecting the moment you press

mind bouncing too

Okey thank you!

But why dont't the text display when it is connected to pin 13 but when in 5V pin the text is displayed?

It feels like it has to do with the voltage becuase I connected wrong but how can I use the 5V pin so the text will be displayed?

don't you need to call begin() or init() for your lcd ? I don't see that in the setup

I added this to the setup but still in pin 13 it don't display anything. And in 5V pin it display "HELLO!" but then i cant turn it on and off
IMG_0535
IMG_0534

void setup() {

  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
  lcd.init();             // initialize the lcd
  lcd.backlight();
    lcd.setCursor(1,0);
}

try something like this

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27

const int buttonPin = A1;
int lastButtonState;

void testButton() {
  int currentButtonState = digitalRead(buttonPin);
  testButton();

  if ((currentButtonState == LOW) && (lastButtonState == HIGH)) {
    digitalWrite(LED_BUILTIN, HIGH);
    lcd.setCursor(0, 0);
    lcd.print(F("PRESSED ")); // Note the space at the end to fully erase the RELEASED message that might have been there
    delay(15); // poor's man anti-bounce
  } else if ((currentButtonState == HIGH) && (lastButtonState == LOW)) {
    digitalWrite(LED_BUILTIN, LOW);
    lcd.setCursor(0, 0);
    lcd.print(F("RELEASED"));
    delay(15); // poor's man anti-bounce
  }
  lastButtonState = currentButtonState;
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the lcd
  lcd.init();
  lcd.backlight();
  lastButtonState = digitalRead(buttonPin);
}

void loop() {
  testButton();
}

(typed here, fully untested)

1 Like

I changed some things in the code but still have same problem. When connected to pin 13 nothing happens, when i have to 5V pin in turns on and code say "PRESSED" and if I change back to pin 13 when button is pressed it turns on and off but no text is displayed.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27

const int buttonPin = A1;
int lastButtonState;

void testButton() {
  int currentButtonState = digitalRead(buttonPin);

  if ((currentButtonState == LOW) && (lastButtonState == HIGH)) {
    digitalWrite(LED_BUILTIN, HIGH);
    lcd.print(F("PRESSED"));
    delay(15); // poor's man anti-bounce
  } else if ((currentButtonState == HIGH) && (lastButtonState == LOW)) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(15); // poor's man anti-bounce
  }
  lastButtonState = currentButtonState;
}

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the lcd
  lcd.init();
  lcd.backlight();
    lcd.setCursor(0, 0);
  lastButtonState = digitalRead(buttonPin);
}

void loop() {
  testButton();
}

please explain better what's the connection... I don't know what's connected to pin13, what you mean by "i have to 5V pin in turns on"...

draw a readable circuit on a piece of paper (just mention pins you use) and post a picture of the circuit

This is how it is connected

A1 <--> BTN <--> ground
SLC <--> SLC
SDA <--> SDA
VCC <--> Digital 13 
GND <--> GND

VCC of the screen ?

An arduino pin should not have to provide more than 20mA in a sustainable way (40mA peak for a short time max).

Typically the LCD driver draws around 1mA but the LED backlight will draw MUCH more current and you'll have to refer to the individual datasheet to get that number. I've seen typically values in the range of 50-200mA.

➜ you should not power anything that requires lots of current from a digital pin. if you want to control the backlight, use the API.

1 Like

Ahaaa okey, i got it now!
Thank you alot! :grin:

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