[Solved] Cannot read button state (with pull down)

I try to follow very basic tutorials on buttons with no success. I started with internal pull-up but switched backto pull down for testing when I failed. I also tried with my Uno before switching to the MKR1000 when it failed.

So, here is my circuit:

The hidden resistor is a 10 kOhm.

And the code:

const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  LED_BUILTIN;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

It uploads with no error. But when I press the button, nothing happens.

If I add a led with its 220 Ohms resistor between the +5V and the button, it lights on when the button is pressed.

I also tried programs that write the button state to serial and it does not work either.

Sorry for the probably very very basic error here :frowning:

Hi,
Try ledPin = 13;

or what ever the 1000 has as its onboard LED.

kleag:
If I add a led with its 220 Ohms resistor between the +5V and the button, it lights on when the button is pressed.

The LED should not light if your button is between 5V and the 1000 input pin and you press that button because it connects the input pin to 5V.....
Have you got a DMM, it would be an advantage at this point.
So no voltage across the LED.
Tom.. :slight_smile:

Hi, thanks for your reply.

I started with ledPin = 6 (the led pin on mkr 1000) and 13 when I tried with the Uno. The LED_BUILTIN gets the right value depending on the selected board.

The input pin is always connected to 5V when the button is pressed. Isn't it the way a HIGH is set ?

Finally, I have a multimeter but what should I measure ?

The voltage on the pin (relative to ground) with the button pressed and not pressed?

Sure sounds to me like the button is wired wrong... when in doubt, Always use the DIAGONALLY OPPOSED pins on those switches to guarantee you get the pins that actually switch versus the ones that behave like a "short".

pwillard:
Sure sounds to me like the button is wired wrong... when in doubt, Always use the DIAGONALLY OPPOSED pins on those switches to guarantee you get the pins that actually switch versus the ones that behave like a "short".

Yup...and another way is to use a continuity tester (or ohm meter, or multimeter set to lowest resistance range [or to continuity, if such a setting exists]), and test the pins on the button, until you see the button action on the meter.

This tutorial should get you going , as previously said connect to the diagonals of the button

Button probably wrong. Move a wire to the other side. The button has 2 'traces' that will complete when pressed, you are probably connected to two on the same side.

Thank you all.

I confirm that the button is wired correctly (it was already tested with the led). Tested with the diagonal and, separately, the ohm meter.

@hammy: my circuit is the Button tutorial.

@MarkT : when the button is not pressed, the voltage between the input pin and the ground is 0V. When it is pressed, it is +5V. Seems normal...

P.S.: one more point, the Blink tutorial works as expected.

kleag:
@MarkT : when the button is not pressed, the voltage between the input pin and the ground is 0V. When it is pressed, it is +5V. Seems normal...

If you didn't already, try measuring on the MKR1000 board [at pin8], and not at the breadboard [i.e. perhaps the wire is open - or is not making contact in the header -- as in smutz on the contacts].

Hi,
I cannot get your code to compile for the MKR1000?
It compiles for the UNO.

Tom..... :slight_smile:

The Wire ! It was it, the white one connected to the input pin. I should have thinked to test it when you suggested to test the button. It's so simple to check with the ohm meter...

My first failed wire. Another thing learnt by experience! Note for the future: CTFW (check the f****** wires).

Thanks @ReverseEMF for the solution and all the others for your time.

Gaël