How to use a button normally close ?

Hi everybody !
I'm a French young developper and I want to discover the Arduino world.
So I bought a Arduino Mega and some components.
I'm a begginer and when I bought my button, it was not a normal button, but a normally close ("bouton normalement fermé" if a French is here .. :wink: )
I've test a lot of solutions but my Led (I want to turn on a led when I push on the button) never turn on/off.
I've tested my led without button and it's work perfectly .. So the problem is on the button ...

Thank you for your answers ! :smiley:

PS : Sorry for my English :~

This is this button : http://uploads.siteduzero.com/files/344001_345000/344773.png

const byte buttonPin = ; // the pin the switch is connected to
const bye LEDPin = ;     // the pin the LED is connected to.

void setup ()
{
  pinMode (buttonPin, INPUT);
  digitalWrite (buttonPin, HIGH);
  
  pinMode (LEDPin, OUTPUT);
}


void loop ()
{
  digitalWrite (LEDPin, digitalRead (buttonPin));
}

Connect your switch between your input pin and ground.
Don't forget a resistor for your LED (you didn't say how it is wired, I'm assuming between the output pin and ground)

How is the switch wired? What code is reading the state of the switch?

Most push button switches are normally open. Pressing the switch closes the circuit. Yours does the opposite, opening the circuit.

The Arduino cares only whether the circuit is open or closed, so your switch, with the proper wiring and code, can work just fine.

My problem is, when I push on the button, ALL the leds (ON too) switch off ... I don't know why ...

I've make this : http://uploads.siteduzero.com/files/355001_356000/355252.png

My code ...

const int bouton = 2; //le bouton est connecté à la broche 2 de la carte Adruino
const int led = 3; //la LED à la broche 13

int etatBouton; //variable qui enregistre l'état du bouton

void setup()
{
   pinMode(led, OUTPUT); //la led est une sortie
   pinMode(bouton, INPUT); //le bouton est une entrée
   etatBouton = HIGH; //on initialise l'état du bouton comme "relaché"
}

void loop()
{
   etatBouton = digitalRead(bouton); //Rappel : bouton = 2
   
   if(etatBouton == HIGH) //test si le bouton a un niveau logique HAUT
   {
       digitalWrite(led,HIGH); //la LED reste éteinte
   }
   else  //test si le bouton a un niveau logique différent de HAUT (donc BAS)
   {
       digitalWrite(led,LOW); //le bouton est appuyé, la LED est allumée
   }
}

If it's possible, can you send me a schema of the circuit ? (I've some troubles to understand you ... :frowning: )

I want to make this :

When I push and release : my led turns on
When I push and release : my led turns off
When I push and release : my led turns on
...

const int bouton = 2; //le bouton est connecté à la broche 2 de la carte Adruino
const int led = 3; //la LED à la broche 13

int etatBouton; //variable qui enregistre l'état du bouton

void setup()
{
   pinMode(led, OUTPUT); //la led est une sortie
   pinMode(bouton, INPUT); //le bouton est une entrée
   etatBouton = HIGH; //on initialise l'état du bouton comme "relaché"
}

void loop()
{
   etatBouton = digitalRead(bouton); //Rappel : bouton = 2
   
   if(etatBouton == HIGH) //test si le bouton a un niveau logique HAUT
   {
       digitalWrite(led,HIGH); //la LED reste éteinte
   }
   else  //test si le bouton a un niveau logique différent de HAUT (donc BAS)
   {
       digitalWrite(led,LOW); //le bouton est appuyé, la LED est allumée
   }
}

Is the same as

const int bouton = 2; //le bouton est connecté à la broche 2 de la carte Adruino
const int led = 3; //la LED à la broche 13  Non! trois != treize



void setup()
{
   pinMode(led, OUTPUT); //la led est une sortie
   pinMode(bouton, INPUT); //le bouton est une entrée
   // etatBouton = HIGH; //on initialise l'état du bouton comme "relaché" -- inutil
}

void loop()
{
   int etatBouton = digitalRead(bouton); //Rappel : bouton = 2
   digitalWrite(led, etatBouton); //la LED reste éteinte
 }

If it's possible, can you send me a schema of the circuit ?

Connect one side of the switch to a digital pin. Connect the other side to ground. Enable the internal pullup resistor. Hard to get much simpler than that,

After the pinMode() statement, for the switch pin, add

digitalWrite(bouton, HIGH); // Enable the pullup resistor.

My problem is, when I push on the button, ALL the leds (ON too) switch off ... I don't know why ...

Your problem is likely that the pin is floating, because you do not have a pullup or pulldown resistor.

Or, I don''t understand, because you only have one LED defined/used. So, the difference between one and all escapes me.

because you do not have a pullup or pulldown resistor

Aaaaah ok !!!
In the tutorial I read, I've understand the pullup is not compulsory ...
I go tomorrow to buy this !

Thank's !!

I go tomorrow to buy this !

Why? The Arduino has a built in pullup resistor. I showed you how to use it. Saves money and doesn't come disconnected. What's not to like?

ALL the leds (ON too) switch off

I guess the ON led is the power LED. If that goes off, you take too much current.
--> You must create a shortcut when pushing the button.

I guess it's not a NC ( Normally Close ) button, and your actual wiring is different than what your picture shows.
(Is the resistor really 1k, and 220R for the Pin13 LED ?)

Arduino has a built in LED on Pin13, you do not need that external LED, resistor and wire for a most basic button test.
And you don't need that blue capacitor (?) in your picture neither, for a first test.

Reduce it to Paul's suggestion. You can enable the builtin one with that line of code ( digitalWrite(bouton, HIGH); ), or take that 1k resistor from your picture.

And recheck this line:
const int led = 3; //la LED à la broche 13

I prefer 13 :wink:

Finally I've already buy this ...
And it's work perfectly now !!!!
Thank's and sorry for the stupid question .. !

EDIT : Ah, ok i had not understand you show to me how to use this ... My english is not so good :wink:

Thank's and sorry for the stupid question .. !

It wasn't a stupid question. You had a question. You got, and finally understood, the answer. So, it's good.