Wednesday, September 4, 2019

On Off LED with 1 Button-Arduino

1 Switch Control 1 LED Arduino
In this arduino project we will learn how to connect 1 Switch with 1 LED from arduino board.When the button (any button e.g Push Button,Toggle Button etc but i recommended a push button and i have a use push button in this video.) is open (unpressed) there is no connection between the two legs of the button, so the pin is connected to ground (through the pull-down resistor) and we read a LOW and LED (Light Emitting Diode) OFF. When the button closed (pressed), it makes a connection between its two legs, connection the pin to 5 volts, so that we read a HIGH and LED (Light Emitting Diode) ON.
Tip
You can also write this circuit the opposite way, with a pull-up resistor keeping the input HIGH,and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed , with the LED normally ON and turning OFF when you press the button.If you disconnect the digital input/output pin from everything, the LED may blink change-fully. This is because the input is "floating" that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.
Hardware Required

You will need the following components:-
  • Arduino Mega2560/UNO R3 
  • Breadboard
  • 1 LED( any color)
  • 1 Push Switch
  • 1 Resistor 330Ω
  • 1 Resistor 10kΩ
  • Jumpers
Procedure
Follow the circuit diagram and hook up the components on the breadboard as shown in the image given below: 


Note:- To find out the polarity of an LED,look at closely.The shorter of two legs,towards the flat edge of the bulb indicates the negative terminal.
The Code
Here's the 'Button' code, embedded using codebender!
Keep in mind that setup () routine runs only one after power ON/re-program or press the reset button. In the program below, the first thing you do is to initialize pin 9 as an output pin with pinMode () function in setup () routine.
The loop() routine runs over and over again,forever. In the main loop , you read the state of button (pressed=high, unpressed=low) and you store it in buttonState variable. When button pressed once, the LED turns ON, and when pressed twice, the LED turns off. 
The code of arduino program 

int buttonInput = 7;      // initialize variable pin
int ledOutput = 8;         // initialize variable pin
int buttonstate = 0;          // initialize button condition that's  0 or 1

void setup() {            // main loop in this program for one time run 
pinMode(buttonInput,INPUT);   // define this Button pin for input
pinMode(ledOutput,OUTPUT);   // define this Led pin for output 
}

void loop() {   // put your main code here, to run this loop repeatedly

buttonstate = digitalRead(buttonInput);//Read state of the Button
if    (buttonstate == HIGH)             //if is pressed button
{
digitalWrite(ledOutput,HIGH);    //Write HIGH to led pin
}
else                                                       //if not pressed button
{
digitalWrite(ledOutput,LOW);       //Write LOW to led pin
}
   }
.........................................
You Can download this code from this here:Download
I hope you'd like this and give me feedback through comment section..Thank you.