user86287: 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


user42015: Look at this breadboard image:

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.

86287: <p>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.</p>
<pre><code>20:03:30.808 -&gt; button not pressed
20:03:31.816 -&gt; button pressed
20:03:32.798 -&gt; button pressed
20:03:33.827 -&gt; button not pressed
20:03:34.821 -&gt; button pressed
20:03:35.849 -&gt; button not pressed
20:03:36.847 -&gt; button not pressed
20:03:37.842 -&gt; button pressed
</code></pre>
<p><a href="https://i.stack.imgur.com/4itwa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4itwa.png" alt="enter image description here" /></a></p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;LiquidCrystal.h&gt;

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(&quot;button pressed&quot;);
    lcd.setCursor(0, 1);
    lcd.print(&quot;pressed&quot;);
  } else {
    Serial.println(&quot;button not pressed&quot;);  
  }
  
  lcd.setCursor(0, 0);
  lcd.print(&quot;Hello&quot;);

  delay(1000);

  lcd.clear();
}
</code></pre>
<p>Top view of my breadboard</p>
<p><a href="https://i.stack.imgur.com/nIV8D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nIV8D.jpg" alt="enter image description here" /></a></p>
<p>is it something related to? <a href="https://docs.arduino.cc/learn/microcontrollers/digital-pins" rel="nofollow noreferrer">https://docs.arduino.cc/learn/microcontrollers/digital-pins</a></p>
<blockquote>
<p>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</p>
</blockquote>
 42015: <p>Look at this breadboard image:
<a href="https://i.stack.imgur.com/9K0tt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9K0tt.jpg" alt="breadboard with lines where the power rails are connected" /></a></p>
<p>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.</p>
<p>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 <strong>floating pin</strong>. It is not actively pulled in one direction, so any noise, that comes by, might change the state of the input pin.</p>
<p>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.</p>
