Arduino LED on/off switch

 

Arduino LED on/off switch
Arduino LED on/off switch





Arduino switch wiring  to  turning on/off using arduino switch code.


Components used :

» Arduino uno

» LED       - Red

»Resistor - 10 kiloohms, 220 ohms


There are multiple programming methods for achieving this project.


All pins in arduino can be used as inputs or outputs. So we need a pin which should act like an input and we need another one which should act like an output.


Here iam taking pin 2 as input and pin 10 as output.


The switch is an input device, which will feed positive or negative voltages to the microcontroller(Arduino board). So we can say switch is an input.


The led is an output device, which will turn on when the switch is pressed and turn off when the switch is released.


The led is provided with a 220 ohms resistor for regulating the current.


In the above figure you can see that i use a 10 kiloohms resistor to the switch which is going to the positive rail of breadboard arduino. It means that the switch is getting positive supply always.


The ground is disconnected with the switch as the switch isolates this connection.


Whenever we press the switch negative voltages from ground flows through the switch and enters directly to the pin 2 of arduino. At this time arduino recognize the current voltage as negative.


As we said earlier at normal mode(switch released mode) the output led will be off, which is preprogrammed in arduino.


When switch is pressed the output led will turn on.


I will provide the easiest program for arduino led on/off switch. I think this will be the easiest program for a beginner because the default programs will make a little confusion to the beginners. So i modified and simplified the program.


Here is the simple program for arduino led on off switch circuit.


Procedure

» Declare a variable. You can take bit also. But here iam taking the variable as byte.


» Declare the input output pins


» Read the signal from the input pin and save the value in the variable


» Check the incoming value using conditional statement if and feed output according to our needs.



Program

byte value;

void setup()

{

pinMode(10,OUTPUT);

pinMode(2,INPUT);

}

void loop()

{

value=digitalRead(2);


if(value= =1)

{

digitalWrite(10,LOW);

}


if(value==0)

{

digitalWrite(10,HIGH);

}


}

Previous Post Next Post