Getting Started with Switches


Since now we are well acquainted with LED’s let us learn how to control the switching using a push button.
In this tutorial, we will learn how to use a push button.

A push button is used to get control of the circuit.

Components Required
  • Resistors                           2Nos. 330ohms,10k(recommended)
  • Pushbutton                        1No.
  • LED                                  1No. Standard red LED
  • Arduino UNO
  • Breadboard
Follow the video to make the circuit.

After making the circuit it will look like

Now dump the code given below

int d=2; // chose the pin which you have connected to the output of push button
void setup()
{
pinMode(2,INPUT);
pinMode(13,OUTPUT);
}
void loop()
{
d=digitalRead(2);
if(d==0)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}

Now the control is in your hands whenever you will press the button the led will be ON and when you release it, LED will go OFF.

You can alter the control like
                                   
Here when we press the button LED goes ON and when we press it again it goes OFF.

For the code of the above video mail us at contact@sudolearn.com.

I hope you are having fun, try connecting multiple switches with multiple LEDs.

  • A pull-up resistor condition.
When the switch is connected to ground and one end of the resistor is connected to VCC.
  • Pull down resistor condition.
When one end of the switch is connected to Vcc and the resistor is connected to ground.
When you use these conditions while coding just remembers when you press the switch, one will give output as 0 and the other will give output as 1.

Thinking?
Join our hands-on Training Courses.
To know more visit us at http://www.sudolearn.com/
Next
Previous