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.
#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);
}
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();
}
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.