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.
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.
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..
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 ?
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.
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.
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].
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.