Pins INPUT mode not working properly

Hi !
I am having trouble with some pins of my arduino uno.

The input mode of most of digital pins are not working properly:
When i use them as an INPUT (for turning on a led with a button) pins 1 to 12 don’t work. Button connected to pin 13 (INPUT) works properly.

However, OUTPUT mode of all pins works very well (blinking led program confirms it).

It is weird that all those pins don’t work as INPUT and work properly as OUTPUT.
Does it mean that all those pins INPUT mode are broken?
Is it possible that only INPUT mode is broken and OUTPUT mode works properly?

Thank you

What does "doesn't work" mean?
Perhaps you need a pull-up or pull-down resistor.

Well, I am using a button (brand HOYA) that includes a 10 kOhm resistor. I suppose it is the pull-up resistor.

The circuit is very easy, I just want to turn off a led pushing a button.

The code is very simple:

int BUTTON_PIN = 2;
int led = 12;
void setup() {
    pinMode(BUTTON_PIN, INPUT);
    pinMode(led, OUTPUT);   
}
void loop() 
{
  if (digitalRead(BUTTON_PIN) == HIGH)
  {
    digitalWrite(led, HIGH);
  }
  else
  digitalWrite(led, LOW);

When I push the button (button == low), the led should turn off. If the button is not pressed (button ==high) the led is supposed to be ON.

So when I set BUTTON_PIN to 13 it works very well.

But, when I set BUTTON_PIN to any other digital pin (signal cable of button is connected to corresponding pin of course) the led doesn't even turn on!
That means that pin 12 (Led Pin) is at 0V even when the button is not pressed, which is surprising.
I can also see that LED turns ON intermittently may be due to interference?

I tested on different ARDUINOs and the results is the same.

-thank you!

diego_arduino:
Well, I am using a button (brand HOYA) that includes a 10 kOhm resistor. I suppose it is the pull-up resistor.

Post a link to a datasheet for that button. Then we can see if what you suppose is correct.

Steve

Here is the link to the button description.

Button description

diego_arduino:
Well, I am using a button (brand HOYA) that includes a 10 kOhm resistor. I suppose it is the pull-up resistor.

When I push the button (button == low),

The very first thing I see on the datasheet you linked is:

Arduino Key Switch Module Keyes KY-004 is a push button that will output a high signal when pressed.

Thank you for your time,

Yes, you are right, but that is not the problem.

It isn't important for me if the led turns ON or OFF when I press the button.

The problem is that when I change the state of the button (pressed or not pressed) nothing happens, unless I connect the button's signal to pin 13.

When it is connected to pin 13 everything works very well and I can't explain why.

I connected to pin 3 as said in the link I sent before, but the program doesn't work. I tried 3 different arduinos and the behavior is the same in all of them.

Only magic pin 13 works

Ok well.... I don't have a button with a built in pulldown, but I wired a normal button with a pulldown like in the example here and shown below.

A pulldown with your code from post #2 means pressed is led on, released is led off. It works correctly for me on all pins from 2-13, except for 12 where your code has the led.

You do have a LED and resistor connected to pin 12 ?

sterretje:
You do have a LED and resistor connected to pin 12 ?

Well I did yes, but I assume that's at OP :wink:

It's time OP showed his or circuit. Particularly, since s/he thought the switch had a builtin pullup which is actually a pulldown, let's see how the switch is wired.

Perhaps OP should also try the same thing with a more conventional switch (or just a jumper), ie with no resistor built in, and tried it a) with an external pulldown and /or b) input pullup.

I got the answer.
I assume it is a mistake of the manufacturer: 5V and Ground pin of the button are inverted. Pressed or not, signal pin of the button was 0 if the button was connected as shown in the datasheet.
I connected
"-" pin of the button to arduino's 5V
5V of the button to arduino's ground

and everything worked correctly.

There is just 1 thing that I remarked and I don't know if it is normal: when pin 13 is set as input, digitalread of the pin returns 1 even if the input signal is 0. But that is another problem...

Thank you for your time!

diego_arduino:
There is just 1 thing that I remarked and I don't know if it is normal: when pin 13 is set as input, digitalread of the pin returns 1 even if the input signal is 0. But that is another problem...

That might be related to this:

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.