Button w/ LED ... not working ( First Project )

New to Arduino / Prototyping … exicted to get to learn tons more. But I picked a project which I thought would be simple for even someone like me.

I wired up my breadboard to match his discription. However, I think he used pin 3 not pin 2. I’ve tried both and neither has worked. So having an engineer mindset and poor eyes I thought maybe I didn’t squint my eyes hard enough to see clear and I downloaded Fritzing and created a drawing please see attached. I used a 220 ohm resistor as I didn’t have a 330 ohm one.

The following is my code.

int buttonInput = 2;
int lightOutput = 13;
int buttonState = 0;

void setup(){
  
    pinMode(lightOutput, OUTPUT);
    pinMode(buttonInput, INPUT);
    digitalWrite(buttonInput, HIGH);
} 

void loop(){
 
   buttonState = digitalRead(buttonInput);
  if ( buttonState == HIGH) {
     digitalWrite(lightOutput,LOW);
  } else {
     digitalWrite(lightOutput,HIGH);
  } 
}

Your time spent helping me getting traction in prototyping is really appreciated.

Might be switch wired wrong? The ones I have would be the two pins on the left, unless you tried that already.
Also the pins are very short and may not be making contact in the breadboard.

steinie44,

Thank you for your time.

  1. Ok per your suggestion. Tried the following
    --- top top as shown in drawing
    --- bottom bottom ---- nothing no change
    --- diagonals ---- nothing no change

  2. Then because all the switch does is send the +5 volts to pin 2 then if I put the lead from the + rail to the same row as the lead going to pin 2 then it should light up ( at least that's my take ) .... nothing

Nothing more frustrating when you're trying something new and what appears should be simple and direct isn't .... I'm hoping that I can get this working so I can begin to learn how to measure volts/ohms/mA and begin to understand schematics.

any other thoughts? how do I test the state of pin 2?

Iwrk4dedpr:
New to Arduino / Prototyping … exicted to get to learn tons more. But I picked a project which I thought would be simple for even someone like me.

http://www.raywenderlich.com/32392/arduino-tutorial-for-complete-beginners-using-a-button

I wired up my breadboard to match his discription. However, I think he used pin 3 not pin 2. I’ve tried both and neither has worked. So having an engineer mindset and poor eyes I thought maybe I didn’t squint my eyes hard enough to see clear and I downloaded Fritzing and created a drawing please see attached. I used a 220 ohm resistor as I didn’t have a 330 ohm one.

I think that’s your mistake, ground the switch…
Let me know it anything happened…

Remove lead from pin 13 and put it in the 5volt rail. If LED won't lite up, reverse the LED pins.

zaxarias ,

Thanks for your reply.

I did that but now the LED just stays on. It still doesn't blink.

Also isn't the point of having the switch wired +5v rail -> switch -> pin2 so that when the button is pushed it sets the pin status to HIGH? Wouldn't setting it to the ground rail just set it low?

steinie44:
Remove lead from pin 13 and put it in the 5volt rail. If LED won't lite up, reverse the LED pins.

There is wrong wiring that's why it won't work.. Switch is not grounded ..

steinie44,

Did that and it lit up.

I know that where I got this project from has a forum.... I really just don't want to have to join another forum to see if this guy is still active and have him see what I did wrong.

I thank you for all your help thus far.

Do what Zaxarias said and change

pinMode(buttonInput, INPUT);

to

pinMode(buttonInput, INPUT_PULLUP);

Make sure you put the LED lead back into pin 13.
Sorry, I mean INPUT_PULLUP

Before you change what steinie44 says , when you push the button nothing happens? Also check that the button sits good on your breadboard ...

steinie44,

Ok I’ve changed the wiring on the switch to go to the (-) rail and the other goes to pin2

I tried to change the “INPUT” to “INPUTPULLUP” but I get a compile error of “Not Declared” …

gotta run out for a bit.

any other thoughts? how do I test the state of pin 2?

The Serial Monitor can send data to your computer screen. It’s also handy for checking analog inputs or for displaying variable values when debugging your program (sketch).

Another test would be to make the LED blink entirely under software control (like the Blink LED Example) to make sure it’s working. (It’s often useful to test the inputs & outputs separately.)

Assuming you don’t already have one, I recommend you get a multimeter. You can pick a cheep one up for around $20 USD if cost is an issue.

It can also be handy to make a little probe out of an LED & resistor to test if voltage is present or not. A long time ago, I made one with a red & green LED wired in opposite directions so I could see if the voltage was positive or negative. But you do need to be aware that in certain cases, such as your circuit that uses the internal pull-up resistor on the input, there may not be enough current to light-up the LED, and a simple probe like that can interfere with the operation of the circuit. A multimeter is high-impedance (when reading voltage) so it generally doesn’t affect the circuit.

Give 5v to the other pin of the switch and try again

Hi Iwrk4dedpr

Try “INPUT_PULLUP”.

Also isn’t the point of having the switch wired +5v rail → switch → pin2 so that when the button is pushed it sets the pin status to HIGH? Wouldn’t setting it to the ground rail just set it low?

The INPUT_PULLUP sets an internal pull-up resistor on the Arduino chip connected to +5V. So when you wire the switch between the digital pin and GND, closing the switch gives a LOW reading, and leaving the switch open gives a HIGH reading.

A trick that someone else posted on the forum recently might help with the switch. Since it can get confusing as to whether the sets of connected contacts are “vertical” or “horizontal” once you plug the switch into the breadboard, connect the two wires (in your case, to the input pin and GND) to opposite corners of the switch. Say, top left and bottom right.

Regards

Ray

Ok I thought I had a rudimentary grasp of electricty ac or dc. but I appear to be baffled when you throw in a micro-controller.

Attached in the new Fritzing schematic. Changes are: Went diagonal across switch ( horrible of someone, IMHO, to not warn noob’s that switches are different ) , removed the "5v lead from breadboard altogether, ran one side of the switch to ground.

…It works … booooyah ( but why )

int buttonInput = 2;
int lightOutput = 13;
int buttonState = 0;

void setup(){
  
    pinMode(lightOutput, OUTPUT);
    pinMode(buttonInput, INPUT);
    digitalWrite(buttonInput, HIGH);
} 

void loop(){
 
   buttonState = digitalRead(buttonInput);
  if ( buttonState == HIGH) {
     digitalWrite(lightOutput,LOW);
  } else {
     digitalWrite(lightOutput,HIGH);
  } 
}

Can someone help trace the circuit for me in a way that I begin to understand how the switch is affecting the circuit?

LEDwButton_v2.fzz (4.85 KB)

zaxarias:

Iwrk4dedpr:
New to Arduino / Prototyping .... exicted to get to learn tons more. But I picked a project which I thought would be simple for even someone like me.

Arduino Tutorial for Complete Beginners: Using a Button | raywenderlich.com

I wired up my breadboard to match his discription. However, I think he used pin 3 not pin 2. I've tried both and neither has worked. So having an engineer mindset and poor eyes I thought maybe I didn't squint my eyes hard enough to see clear and I downloaded Fritzing and created a drawing please see attached. I used a 220 ohm resistor as I didn't have a 330 ohm one.

I think that's your mistake, ground the switch..
Let me know it anything happened..

On an earlier post i told you to ground the switch, however you replied that this doesn't work..
But now works ? :~ :~ :~

Also it would be better if you attached an image file and not a .fzz file because someone might not have the fritzing application installed..

My apologies for not posting the jpg…

I changed leads of the switch from top 2 top configuration to diagonal configuration.
Here is the jpg image.

Ok … so here is what I think is happening.
The mC is continously looping ( does it loop the set up as well? ) The set up constantly sets pin 2 to be HIGH and while pin 2 is high (+5v) then pin13 is set to lo < 2.5v. So how does pressing the switch bring pin2 to a low state? To me it just seems that pressing the button completes the circuit and let’s +5v flow?

    pinMode(buttonInput, INPUT);
    digitalWrite(buttonInput, HIGH);

The setup() function is executed once. The above statements set pin 2 to be an input and enable an internal pullup resistor that connects pin 2 to +5V. So, if there is nothing connected externally to pin 2, it will read high.

The switch is normally open, so pin 2 is not connected to anything. When you press the switch, it closes and connects pin 2 to ground. The resistance of the switch (virtually 0 resistance) is much lower than the resistance of the internal pullup resistor, so the input voltage read by pin 2 is 0V. A small current does flow in the internal pullup resistor but this does not affect the state of the pin.