Arduino push button led tutorial

Mr.ElectroUino
2



Description: -

In this tutorial, we have to control LED with a button with the help Arduino Uno.
A push button is used to controlling some aspect of a machine. When we press the button, the LED is on (Arduino read it HIGH) and when we pressed again the led if off (Arduino read it LOW).

Requirement:

           1.    Arduino Uno.     
     2.   1 x 220 ohms Resistor.
     3.   1 x 10k ohms Resistor.
     4.   1 x LEDs (5mm).
     5.    Push button.
     6.    Breadboard.
     7.    Jumper wire.   
   
Step:-
1. A push button having 4 pins and we use 3 pins of the push button for controlling LED.

2. Connecting Button pin 4 to Arduino pin 5v using a jumper wire.

3. Attach a 10k ohms resistor to button pin 2 and at the end of the resistor is connected to Arduino pin GND.

4. Button pin 1 is connected to Arduino digital pin 4 using a jumper wire.

5. Attach a 220 ohms resistor to the positive pin (anode) of led and at the end of the resistor is connected to Arduino digital pin 2.

6. Connecting Negative pin of the LED to Arduino pin GND using a jumper wire.

7. Now the circuit is ready.....upload the code and enjoy it.....

Circuit:-

Code:-

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // 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 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.println("Button is pressed");
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    Serial.println("Button is not press");
  }
}

Control a 3 LED with a Button.

We have to control 3 LED with a Push button with the help Arduino Uno.

Requirement:-

   1.   Arduino Uno.
     2.   3 x 220 ohms Resistor. 
     3.   1 x 10k ohms Resistor.
     4.   3 x LEDs (5mm).
     5.   Push button.
     6.   Breadboard.
 7.   Jumper wire.  

Circuit:-




 Code: -



// constants won't change. They're used here to set pin numbers:

const int buttonPin =  4;     // the number of the pushbutton pin

const int ledPin        = 13;      // the number of the LED pin
const int ledPin1      = 12;
const int ledPin2      = 11;

// variables will change:
int  initial    = 0;       //hold current  initial
int oldstate    = 0;       //hold last  initial
int buttonstate = 0;      // variable for reading the pushbutton status

void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop(){
  //debouncing routline to read button
  buttonstate = digitalRead(buttonPin);  //state the  initial of button
  if(buttonstate == HIGH){               //check if it has been pressed 
    delay(50);
    buttonstate = digitalRead(buttonPin);//state button again
    if(buttonstate == LOW){              //if it is 0 considered one press
     initial = oldstate + 1;        //increase  initial by 1
    }
  }else{                          //check if it has been NOT pressed
      delay(100);
      }
   switch (initial){               //react to button press a  initial
     case 1:                     //if  initial is 1
       digitalWrite(ledPin, HIGH);//on
       digitalWrite(ledPin1, LOW);//off
       digitalWrite(ledPin2, LOW);//off
       oldstate =  initial;               //set oldstate  initial as current  initial
       break;
     case 2:
       digitalWrite(ledPin, LOW);
       digitalWrite(ledPin1, HIGH);
       digitalWrite(ledPin2, LOW);
       oldstate =  initial;
       break;
     case 3:
       digitalWrite(ledPin, LOW);
       digitalWrite(ledPin1, LOW);
       digitalWrite(ledPin2, HIGH);
       oldstate =  initial;
       break;         
     default:                      //if  initial is not 1 2 3
       digitalWrite(ledPin, LOW);  //off
       digitalWrite(ledPin1, LOW);
       digitalWrite(ledPin2, LOW);
       oldstate = 0;                    //reset to all off/initial 0
       break;         
     }
}

Watch this video

Tags

Post a Comment

2Comments

Post a Comment