Introduction: Arduino Tutorial 2 : Led E Button

Accensione e spegnimento led con pulsante

In questo secondo tutorial andremo ad accendere e spegnere

un led con un pulsante.

In this second tutorial we will turn on and turn off
a led with a button.

Step 1: Progettazione

Colleghiamo l'anodo del led al pin13, uno dei due piedini del pulsante a +5V, mentre l'altro,posizionandosi prima la resistenza da 10kOhm che viene collegata a GND, va al pin 7.

Materiale Occorrente:

-diodo led;

-pulsante Normalmente Aperto (NA);

-resistenza da 10kOhm.

Connect the LED anode to pin 13, one of the two buttons on the + 5V button, while the other one, first placing the 10kOhm resistance that is connected to GND, goes to pin 7.

Material Required:
-diode led;
-normally open button (NA);
- 10kOhm resistance.

Step 2: Programmazione / Programming

Dopo aver caricato il programma su Arduino, il led si accenderà soltanto qualdo voi premerete il pulsante.

Il codice è uguale a quello del Tutorial 1, solo con l'aggiunta del pulsante.

si comincia col dichiarare il nome del pin assegnato al pulsante (#define pul 7;), si definisce come pin di input (pinMode(pul,INPUT)) e poi, attraverso la funzione "digitalRead()" andiamo a verificare in quale stato si trova il pulsante in quell'istante( se è premuto: HIGH, se non è premuto:LOW);

Se pul risulta "HIGH" il led si accende per 1 secondo, altrimenti resta spento.

After loading the program on Arduino, the LED will only light up whenever you press the button.
The code is the same as in Tutorial 1, just by adding the button.
you begin by declaring the pin name assigned to the button (#define pul 7;), it is defined as input pin (pinMode (pul, INPUT) )and then, through the "digitalRead ()" function, finds the button at that time (if it is pressed: HIGH, if not pressed: LOW);
If pul is "HIGH", the LED will turn on for 1 second, otherwise it will be off.

Code:

#define led 13        //Diamo il nome "led" alla porta 13#define pul 7         
#define pul 7		//Chiamiamo "pul" la porta 7
void setup() {pinMode(led,OUTPUT);  //Definiamo la tipologia di funzionamento che dovrà assumere
                      //la porta "led", in questo caso è OUTPUT
pinMode(pul,INPUT);   //Definiamo la porta "pul" come INPUT
}
void loop() {  

  digitalRead(pul);       //Leggo in che stato si trova il pin "pul"
  if(pul==HIGH)           //se il pulsante è premuto
  {
  digitalWrite(led,HIGH); //Attribuisco al pin "led" un valore logico ALTO
  delay(1000);            //Aspetto 1000ms
  } 
  else{                   // se non è premuto
  digitalWrite(led,LOW);  //Attribuisco al pin "led" un valore logico BASSO
             
  }
}