LCD is not onnnnnn!

#include <LiquidCrystal_I2C.h>

// Define LCD and button pins
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Adjust I2C address if needed
const int buttonUpPin = 2;
const int buttonDownPin = 3;

// Variables for counter and button states
int counter = 0;
int buttonUpState = 0;
int buttonDownState = 0;
int previousButtonUpState = 0;
int previousButtonDownState = 0;

void setup() {
  lcd.begin(2, 16, 8);
  pinMode(buttonUpPin, INPUT_PULLUP);
  pinMode(buttonDownPin, INPUT_PULLUP);
  lcd.print("Write a number ");
}

void loop() {
  // Read button states
  buttonUpState = digitalRead(buttonUpPin);
  buttonDownState = digitalRead(buttonDownPin);

  // Check for button presses (using debouncing)
  if (buttonUpState != previousButtonUpState && buttonUpState == LOW) {
    counter++;
    lcd.setCursor(9, 0);
    lcd.print(counter);
  }
  if (buttonDownState != previousButtonDownState && buttonDownState == LOW) {
    counter--;
    lcd.setCursor(9, 0);
    lcd.print(counter);
  }

  // Update previous button states for debouncing
  previousButtonUpState = buttonUpState;
  previousButtonDownState = buttonDownState;

  delay(50);  // Delay for stability
}

this is my code, there's an error that the screen is off... although i imported all correct!

Define off ie no backlight, no characters? Post an annotated schematic showing exactly how you have wired it. Be sure to show all connections, power, ground and power sources. Run the I2C scanner and let us know what the results are.

1 Like

There are 2 buttons. one descreases it, and the other increases it, using 2 buttons and an LCD. for some reason, when i run it, the backlight is on, but no text although i wrote "write a number" so it can be displayed... but the increase button turns it off while being pressed... and the other does nothing

@alispacearduino - See above post...

3 Likes

All linkage:

LCD:

VCC - 5V
GND - GND
SDA - A4
SCK - A5

Button:
GND: GND
Output: PIN 9
VCC: 3V3

That's really it...

Which mcu board are you using? I see it has a 3v3 output... and a 5v output used on the LCD.

Buttons are input devices (from the perspective of the mcu).

I think this needs a wiring schematic... showing pins and wires between devices.

Does this example work?

i am using the most popular one, arduino UNO

You have wired this incorrectly. Show a drawing and a picture to verify they match, and can be corrected.

2 Likes

There are many LiquidCrystal_I2C libraries out there.
They are not all the same. Some initialize differently, some have different API functions,
most of them have some issues and pretty much none of them are maintained anymore.
The one in the IDE library manager that now links to the johnrickman git repo is no longer maintained. John, hijacked the library after it was assigned to him and made a wreck of it.

Most of the LiquidCrystal_I2C libraries out there default to having the back light being off after init() or begin() is called.
lcd.backlight() turns on the backlight.

You are calling begin() with 3 parameters, I would not recommend doing this.
The 3rd parameter is for selecting a font size which some LCDs support and some don't. Also, it only works on 1 line displays so it should be being used with a display that is larger than 1 line.

I would recommend using the hd44780 library and the hd44780_I2Cexp i/o class.
It auto locates the i2c address and auto detects the wiring used on the i2c backpack between the PCF8574 chip and the LCD.
This is useful since not all backpacks are wired the same but most libraries are hard coded to work with only one type of backpack.

The hd44780 library can be installed directly from the IDE using the library manager.

The hd44780 library has lots of documentation.
I would highly encourage spending a few minutes going through the documentation to understand how the library uses i/o classes and where to find the i/o class examples.
The documentation can be referenced in the IDE using the Documentation example sketch.
Make sure to have a read of the hd44780_I2Cexp i/o class wiki page.

Here is link to the wiki for the hd44780 library:
https://github.com/duinoWitchery/hd44780/wiki

Install the hd44780 library using the IDE (do not install from a zip file)
Use the hd4480_I2Cexp i/o class.
Run the included I2CexpDiag sketch in the hd44780_I2Cexp i/o class examples.
It will test the I2C connections and the LCD and report any issues it finds.

If I2Cexpdiag reports any issues post them here and we can walk you through how to resolve them.

--- bill


That is the entire linkage

Well, it's difficult to be sure from that Fritzing style diagram, but it would appear that you have the 3.3V output shorted to ground, and digital input 9 permanently grounded.

1 Like

I thought the third parameter was number of lines...

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

Your sketch says pins 2 and 3 are buttons... I think you need to remove all parts, make a drawing, follow the drawing, then write a sketch to match the drawing.

A schematic would give us a much better picture of what you have. I have a feeling you do not know what a schematic is or do not want to draw one. Reference post #1. I could take a SWAG and say some parts are missing, not connected correctly but I cannot be sure.

The 3rd parameter of the constructor is the number lines.
But I was referring to the 3rd parameter of begin().
From my post:

You are calling begin() with 3 parameters, I would not recommend doing this.

Here is the begin() API prototype from LiquidCrystal_I2C.h
void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS );

Here is the begin() function declaration from LiquidCrystal_I2C.cpp
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {

Here is the setup() function from the OP

void setup() {
  lcd.begin(2, 16, 8);
  pinMode(buttonUpPin, INPUT_PULLUP);
  pinMode(buttonDownPin, INPUT_PULLUP);
  lcd.print("Write a number ");
}

Note that begin() is being called with a 3rd parameter of "8".
Also note that backlight() is not called so depending on which LiquidCrystal_I2C library is installed the backlight may not ever be turned on.

The hd44780 library will turn on the backlight as part of begin()

--- bill

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