Output Became high without checking input condition

Kidly help to correct program no any pullup resistor connected to input...

const int buttonPin = 2;
const int LED1 = 13;
int buttonState = 0;

void setup()
{
pinMode(LED1, OUTPUT);
pinMode(buttonState, INPUT);
}

void loop()
{
int buttonState = digitalRead(buttonPin); //read and store the button state (0 or 1)

if(buttonState == HIGH){ //check if state is high (button is pressed)
digitalWrite(LED1,HIGH); //turn on LED
} else {
digitalWrite(LED1,LOW); //turn off LED
}}

If you don't have a pull-up or pulldown resistor then the value is undefined when the button is not pressed.

Use pinMode(buttonState, INPUT_PULLUP); and check for == LOW instead.

1 Like

So how exactly is the input wired ?
Is anything keeping the input LOW when the button is not pressed or is it floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing ?

1 Like

Learn how to use inputs here:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

2 Likes

.. or here (you beat me to it Mike!)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.