Answer the question
In order to leave comments, you need to log in
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");
}
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question