I have connected my LCD with arduino uno. I am controlling contrast with the library and not the potentiometer. I am trying to read input from the button, it is connected to pin 9 of arduino. The resistor in the image is grounded, now arduino is not sensing signal from the button it keeps printing this even though in the timestamp below I have not touched the button. The resistor is 1k ohm.
20:03:30.808 -> button not pressed
20:03:31.816 -> button pressed
20:03:32.798 -> button pressed
20:03:33.827 -> button not pressed
20:03:34.821 -> button pressed
20:03:35.849 -> button not pressed
20:03:36.847 -> button not pressed
20:03:37.842 -> button pressed
#include <LiquidCrystal.h>
const int buttonNumPad1 = 9;
int buttonNumPad1State = 0;
int Contrast = 127.5;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
pinMode(buttonNumPad1State, INPUT);
analogWrite(6, Contrast);
lcd.begin(16, 2);
}
void loop()
{
buttonNumPad1State = digitalRead(buttonNumPad1);
if (buttonNumPad1State == HIGH){
Serial.println("button pressed");
lcd.setCursor(0, 1);
lcd.print("pressed");
} else {
Serial.println("button not pressed");
}
lcd.setCursor(0, 0);
lcd.print("Hello");
delay(1000);
lcd.clear();
}
Top view of my breadboard
is it something related to? https://docs.arduino.cc/learn/microcontrollers/digital-pins
Input pins make extremely small demands on the circuit that they are sampling, equivalent to a series resistor of 100 megohm in front of the pin. This means that it takes very little current to move the input pin from one state to another
-
is it a breadboard with the power rails separated in the middle?Juraj– Juraj ♦2022年08月15日 14:39:55 +00:00Commented Aug 15, 2022 at 14:39
-
@Juraj yes it is a breadborad with power rails separated in the middleRice– Rice2022年08月15日 14:43:25 +00:00Commented Aug 15, 2022 at 14:43
-
1Then have you connected these two parts of the power rail? The described behavior suggests that you have a floating input pin. Maybe the resistor isn't actually groundedchrisl– chrisl2022年08月15日 14:50:30 +00:00Commented Aug 15, 2022 at 14:50
-
@chrisl do you want me to connect both power rails? I uploaded a top view of my breadboard(note the lcd is just kept on the breadboard and it is not connected)Rice– Rice2022年08月15日 14:53:50 +00:00Commented Aug 15, 2022 at 14:53
-
@chrisl also which parts of the rails are you talking aboutRice– Rice2022年08月15日 15:04:34 +00:00Commented Aug 15, 2022 at 15:04
1 Answer 1
Look at this breadboard image: breadboard with lines where the power rails are connected
I have marked the power rail connections with green and red lines. Do you see, that they are not connected in the middle, right there where I've drawn the blue dotted line. Often the power rails of breadboards are not connected internally at that place. If that is the case for your breadboard you would need to bridge that gap with some jumper wires.
If you don't do that, your button and pulldown resistor are not connected to anything but the digital input pin. So there is nothing setting the state of that pin (HIGH or LOW). Its just like you simply connected a wire to the digital input pin. And that's called a floating pin. It is not actively pulled in one direction, so any noise, that comes by, might change the state of the input pin.
Make sure your button and pulldown resistor are actually electrically connected to ground. Bridge any gaps in the power rails, that your breadboard might have.