Simple project with LED and button

Hello, I am trying to createa a project that when button is pressed led lights on and when it is pressed again it goes off. Can anyone tell me what are the two pins of the button? I know what GND is only… I have seen a button with 4 pins in tutorials this is quite confusing… :confused:

the one marked VCC is for power to the switch.

Ground, you got.

The other one is the output of the switch, which is likely a voltage output equal to VCC. This isn’t your ordinary open/close pushbutton switch, so to speak. Post your sketch and there will be assistance for you here.

First of all , you need to use the breadboard correctly.
The +5V (Vcc) and GND should be connected to the red and blue power buses on both sides of the bread board.
From there you use short jumpers if needed.
I don't understand what your problem is.
Do you have any code ?
What is your question ?

Hey, yes I have some code but this is a secondary problem for me. The core problem is about the jumpers and the connections. The GND must be connected to the GND arduino pin. Thats easy. After that the VCC is for the button power right? So I must only connect this to a 5V pin in my arduino? Right? Is the above connection wrong? :? If so, why? :confused:

Lastly the OUT pin is connected to pin 2. This means that if i give HIGH to that pin the led must be on? and LOW value means that the led must be off right? There is nothing more complicated here...?

xmarkx:
Hey, yes I have some code but this is a secondary problem for me. The core problem is about the jumpers and the connections. The GND must be connected to the GND arduino pin. Thats easy. After that the VCC is for the button power right? So I must only connect this to a 5V pin in my arduino? Right? Is the above connection wrong? :? If so, why? :confused:

A momentary switch (button) doesn't need power; it's simply an on/off switch. To properly use a momentary switch with an Arduino digital pin, you simply need to have it so that when the button is unpressed, the pin is connected to either HIGH or LOW, and when it is pressed, it's connected to the opposite. This is done using a pull-up or pull-down resistor so that when the switch is not pressed, it is "pulled-up" to HIGH (VCC) or "pulled-down" to LOW (GND). The easier way to do it is is to enable the internal pull-up resistor, so your circuit is simplified to Digital Pin > Switch Pin 1 and Switch Pin 2 to Gnd.

xmarkx:
Lastly the OUT pin is connected to pin 2. This means that if i give HIGH to that pin the led must be on? and LOW value means that the led must be off right? There is nothing more complicated here...?

Not necessarily, you could turn it on with LOW too, you just need to wired the LED differently. If the wiring is Digital Pin > Anode, Cathode through resistor to GND, then your statement is correct. If it is wired Digital Pin through resistor to Cathode, Anode to VCC, then you could turn it on with LOW.

First of all , you need to use the breadboard correctly.
The +5V (Vcc) and GND should be connected to the red and blue power buses on both sides of the bread board.
From there you use short jumpers if needed.

you asked for instructions. I gave you instructions (above).
Please follow them.
Connect ALL Vcc to the pins in the red line power bus on the outsides of the breadboard.
Connect ALL GND to the pins in the blue line power bus on the outsides of the breadboard.
If you DO NOT UNDERSTAND THESE INSTRUCTIONS PLEASE SAY SO.
OTHERWISE PLEASE FOLLOW THEM.
I am not going to proceed any further until that is done.
There is a right way and a wrong way to do this. You are currently doing it the wrong way.
Please follow these instructions so you can do it the right way.

Arrch:
A momentary switch (button) doesn't need power; it's simply an on/off switch. To properly use a momentary switch with an Arduino digital pin, you simply need to have it so that when the button is unpressed, the pin is connected to either HIGH or LOW, and when it is pressed, it's connected to the opposite. This is done using a pull-up or pull-down resistor so that when the switch is not pressed, it is "pulled-up" to HIGH (VCC) or "pulled-down" to LOW (GND). The easier way to do it is is to enable the internal pull-up resistor, so your circuit is simplified to Digital Pin > Switch Pin 1 and Switch Pin 2 to Gnd.

That button has an on board resistor, yes?

Right?

I am not 100% sure if that is a resistor...there is a tiny tiny thing that looks like one

What's that partnumber on there?... ends in 4072a

2H24072A

and here is a code tha works...

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int status=0;
int buttonpressed =0;

// 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 == LOW) 
   {    
    digitalWrite(ledPin, HIGH); 
    
  } 
  

 if(buttonState == HIGH) 
   {    
    digitalWrite(ledPin, LOW); 
    
  } 

}

how can i modify this code that when button is pressed the led status is changed? (now the status is changing when i releash my finger)

what happens electronically when you release your finger?

the signal to the pin changes yes?

your answer is in here:

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 == LOW) 
  {    
    digitalWrite(ledPin, HIGH); 
  } 
  if(buttonState == HIGH) 
  {    
    digitalWrite(ledPin, LOW); 
  } 
}

there are only two states here HIGH and LOW...

When i press LED is on. When i release LED is off. I know that the 'problem' is there...but can I modify it since there is a loop in main method? :confused: It doesnt help me out a lot.

I suspect that what you need to know is:

on one edge of your arduino board some connectors are labelled
D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,D13

I could not see cables from at least two of those to your breadboard; one for Digital Out relative to GND and one for Digtal In relative to GND.
The 'D' pin which you set in your program to be a digital input is the one which goes to your switch.

I think that you had D2 as your digital output.
Put a resistor in your breadboard (about 1kOhm would do), and test that your breadboard GND is connected to Arduino GND and that your LED is the right way around and lights up when Arduino +5V is provided above the resistor instead of D2. You can now write a script to contain at least
digitalWrite(2,1);
delay(200);
digitalWrite(2,0);
delay(200);
which should flash your LED from programmed control.
See the examples in the arduino programming environment. Sometimes they work with "D2" where I use a plain number "2"
That could only work if somewhere the value of D2 had been defined to be 2.

Next you'll need to wire one of the other 'D' pins to your breadboard, through 1kOhm and through your switch to GND.
From there you should be able to digitalRead that pin. Look at DigitalRead() on the reference page of arduino.cc I've forgotten whether you need to early on in your program DigitalWrite a HIGH to that pin to set it with a 20kOhm 5V "pullup" but testing it will see soon enough.

It might work without the 1kOhm resistors recommended here but having them makes it less likely to break anything if you wire the wrong thing.

xmarkx:
When i press LED is on. When i release LED is off. I know that the 'problem' is there...but can I modify it since there is a loop in main method? :confused: It doesnt help me out a lot.

GREAT!!! The sketch is doing exactly what you told it to do!

so, instead of having the light light up, change its state.

if(buttonState == HIGH) 
  {    
    digitalWrite(ledPin, LOW); 
  }

try instead (pseudo)

if(buttonState == HIGH  &&  ledPin == LOW ) 
  {    
    digitalWrite(ledPin, HIGH);
  }

do you know how to debounce that button?

I am trying to create a project that when button is pressed led lights on and when it is pressed again it goes off.

void loop() {
  reading = digitalRead(buttonPin);
   if (reading != lastButtonState) {
      lastDebounceTime = millis();
      lastButtonState = reading;
   } 
 
   if ((millis() - lastDebounceTime) > debounceDelay) {
       if (buttonState != lastButtonState) {
           buttonState = lastButtonState;
           if (buttonState == HIGH) {
                 ledState = !ledState;
                 digitalWrite(ledPin, ledState);
           }
       }
   }
}

Post your entire code if you want help.
That includes declarations and void setup

Where did you get that code ?

I posted as a reply to a post.

Don't remember where I got the code, I just googled "arduino push button on off" as I needed it for a sketch I made a while back.

  /* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce)
  {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

http://www.arduino.cc/en/Tutorial/Switch

Start a WORD file (or Excel) with links.
Every time you find a sketch you like , paste it in the file with a label you can remember on the line above the link.
When you want to find something you forgot , open the file, Edit/Find "Switch "

I've not been following too closely, but is there yet an understanding of how the switch itself is wired on its little board? I Googled the part number that the OP posted in response to my earlier query (2H24072A) and Google was dismayed not to find it.

@OP, can you give a link to the switch on its maker's site, or just say who the maker is?