Digital pins do not work as a INPUT pins

Hello, as i was practicing my coding with INPUTs and push buttons i realized that my push buttons aren't registered by an arduino. I thought that it was wiring problem but i have triplechecked my wiring and nothing was of. I also have pull down resistor (10k) connected to ground. The code is very simple. Here it is:

CODE:
int button = 8;
int led = 3;

void setup()
{
pinMode(button, INPUT);
pinMode(led, OUTPUT);
}

void loop()
{
if (button == HIGH)
{
digitalWrite(led, HIGH);
}
}

ENDCODE

As you can see its just a simple project. None of my pins are working as INPUTS except pin 13, though they work as OUTPUTS.

'button' is the pin's NUMBER not it's value. You've obviously found digitalWrite() so maybe digitalRead() is the next command to look at.

Steve

Also look at (from the IDE memu)
File -> Examples -> 02.Digital -> Button

Marsha

Best practice does not use a pulldown or any external resistor*. Each digital pin has an internal pullup resistor enabled with:

pinMode(pin, INPUT_PULLUP);

The reference for digitalRead() and pinMode().

  • Unless the switch wiring is long enough to pick up noise, then an, external, lower value pull up resistor (strong pullup) may be required along with the capacitor to form a low pass filter.

Your code never writes LOW to led. How can you possibly know anything about what's going on?

void loop()
{
if (digitalRead(button) == HIGH)
{
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}

I added the other problem fix from @sliderule.

That can be written in a few other ways, but that's what you have to do logically. The else part is not automatic!

a7

@benmemo

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

alto777:
Your code never writes LOW to led. How can you possibly know anything about what’s going on?

void loop()
{
if (digitalRead(button) == HIGH)
{
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}

aka

void loop()
{
   digitalWrite(led, digitalRead(button));
}

Just to be pedantic @groundFungus, could you H-flip your diagram so action flows from left to right ?
Maybe you can add the LED shown left-to-right as well ?

@benmemo - your duplicate topic created today has been deleted

Why did you create a second one, especially as this one has replies ?

TheMemberFormerlyKnownAsAWOL:
aka

void loop()

{
  digitalWrite(led, digitalRead(button));
}

I just had a coding problem. This works for me. Pulldown resistor 10k is connected from input pin to ground. Thank you for your help.

You know, you don’t need a resistor at all if you read the replies above.

@OP

1. The following diagram (Fig-1) describing the structure of DPin-8 may help you to understand your coding mistake.

IOpin8.png
Figure-1:

2. You have written --

if (button == HIGH)
  {
    digitalWrite(led, HIGH);
  }

3. Instead of writing the code of Step-2, you could write as --

set direction of DPin-8 as input without internal pull-up resistor but with external pull-down resistor.
if (logic value of DPin-8 with which Button is connected is HIGH) //Button must have pull-down resistor
{
   then ignite LED connected at DPin-3
}

4. The Arduino code for the argument of if() structure of Step-3 is:

(digitalRead(8) == HIGH)

5. The final correct codes is --

pinMode(8, INPUT);
if(digitalRead(8) == HIGH)
{
    digitalWrite(led, HIGH);
}

6. In Fig-1, we observe that the input line (DPin-8) has an internal pull-up resistor (Rip). If we like to use it instead of an external pull-down, then the Button connection would appear as follows (Fig-2):

DPin8INPullUp.png
Figure-2:

7. Now, the code to detect the closed condition of Button would be:

pinMode(8, INPUT_PULLUP);  //Rip is connected/enabled; G0OB is OFF
if(digitalRead(8) == LOW)
{
    digitalWrite(led, HIGH);
}

IOpin8.png

DPin8INPullUp.png

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