Reading HIGH on input when not expected

Hi everyone,

I am writing a simple code to increase a counter everytime a pushbutton is pushed. I am using the serial port to check the value of the counter.

My problem is that the counter increases when the button is not pushed, in other words, the input is reading HIGH when its suposed to read LOW. I have tried to use different codes to solve this problems but I have not being able. I have used a simple code that reads the stated of the pin, and another one that detects the change in the input value.

I have also tried to solve it using delays, but it does not work, it keeps reading HIGH when it should read LOW.
I will appreciate any kind of help or advice you could give me.

Thank you very much.

I attach one of the codes, it is very simple

const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

Maybe the pin is floating and needs a pulldown resistor.

Or wire the switch from ground to input and enable the internal pullup.

Riva,

How do I know id the pin is floating? What would the pulldown resistor do when applied to my case ?

The pin must be connected to either V+ or ground. Never should it be open as the input in the open state is undefined and could be high or low or oscillating between. A pull up or pull down resistor holds the pin in a defined state when the switch is open and the switch, when closed, will pull the pin to V+ or ground. The resistor holds the pin at ground and also limits current, when the button is pushed, so there is not a short to V+ to ground.

Grounfungus,

I have tried both methods but still does not work.

When appliying the pullup resistor I changed the following code:

const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 11; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 1; // current state of the button
int lastButtonState = 1; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

But the led turns on when I am not pushing the button which is not what I was expecting. I have implemented the circuit shown in your image. Do you have any ideas?

Look in the playground for"button debounce".

Mark

Maybe the LED doesn't change because you never change it in the loop() code. You declare ledPin, and in setup() set it to output but that is the last it is mentioned in the code.

I forgot to copy that part of the code, sorry.

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 11; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

Mark,

I have look into the matter and have written this code that takes into account button debounce. It still does not work
I have also tried to do it with hardware but did not work.

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11; // the number of the LED pin
int a=0;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}

// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
if( buttonState==HIGH){
a++;
Serial.print("number of button pushes: ");
Serial.println(a);
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

What exactly is your button circuit?

This is the circuit por the pushbutton, I am also using an led, to light it when there is a high input

image.jpg

Where is the LED connected?
If the pushbutton is in series with the LED, then there won't be enough voltage left to read. If its something like this and it still doesn't work, just try something simple and read directly as a quick verification test.