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 .. )
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 ...
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)
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 ... )
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.
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