N
N
Nihhilistum2019-08-12 18:41:59
Arduino
Nihhilistum, 2019-08-12 18:41:59

How to control power and display information on the lcd 1602 screen via I2C?

For one of the Arduino projects I bought an lcd 1602 (16 x 2) with I2C. My device requires the screen to turn on and display text when a button is pressed. I connected the button to the ground and 2 pins, and the screen to the ground, 4 and 5 pins (for I2C) and after a little thought, I decided to power the lcd from pin 9 (because only through it I got the opportunity to turn the lcd on and off without turning off the Arduino) and wrote this code , providing software protection against chatter:

#include <Arduino.h>
#include <Wire.h> 
#include <LiquidCrystal_PCF8574.h>

#define button 2    //пин кнопки
#define screenPower 9   //пин экрана

boolean buttonBounce = 0;
boolean buttonDebounce;
boolean buttonValue;
boolean powerFlag = 0;    //флаг переключения питания экрана

LiquidCrystal_PCF8574 lcd(0x27);


void setup(){
  pinMode(screenPower, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}


boolean debounce(boolean buttonBounce){    //устранение дребезга кнопки
  uint32_t buttonTimer = millis();
  boolean buttonDebounce = digitalRead(button);   //первичное счтывание значения
  if (buttonBounce != buttonDebounce){    //если дребезг был зафиксирован
    if (millis() - buttonTimer > 5){    //жди 50 миллисек
      buttonDebounce = digitalRead(button);   //значение без дребезга
    }
  }
  return buttonDebounce;    //возврат значения без дребезга
}


void screen_function(){   //вкл-выкл дисплея 
  if (digitalRead(button) == LOW){    //сигнал инвертирован из-за подтяжки
    powerFlag = !powerFlag;   //инвертирование флага
    digitalWrite(screenPower, powerFlag);  //флаг вкл-выкл питание экрана
  }
}


void loop(){
  buttonValue = debounce(buttonBounce);   //присвоение "исправленного" значения кнопки
  screen_function();    //вызов функции экрана
  lcd.setCursor (0, 1);
  lcd.print ("Hello");
}

Without commands lcd.setCursor(0, 1); and lcd.print("Hello"); the screen more or less works, but if you try to display something through it, it stops turning on.
I ask for your advice regarding the hardware and software parts

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lonelymyp, 2019-08-13
@Nihhilistum

Some screens have a command to programmatically turn off the backlight, see the documentation.
We erase the screen, turn off the backlight, it looks like it is turned off and there is no need to re-initialize it after turning it on.
And connecting the screen power to the arduino output directly is a bad idea.

N
Nikita Gennadich, 2019-08-12
@Psychosynthesis

And why do you have buttonValue and a bounce exception here if it is not used anywhere?
Also, if I didn't mix anything up, for these displays it is necessary to send lcd.init () every time the power is turned off;
And yes, one more thing - some instances of such displays consume almost 90 mA when the backlight is turned on, so it's better to hang power through the field switch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question