Introduction: Push Switch to Control Led(Arduino)

About: Student | Developer

Hi!, Today I'm going to show how to use the push-button for controlling the ON/OFF state of the LED in Arduino.

For this, I'm going to use TinkerCAD, which is very easy to use and serves our purposes when it comes to things like this.

If you want to know how to use TinkerCAD, you can check my post on the basic use of TinkerCAD for Electronic Projects.


Step 1: Getting All the Components Ready

Now get all the components needed for our testing:

1) Arduino Uno

2) Small Breadboard

3) LED

4) Push-button

5) Resistor(10K-ohms)(value can be changed in the resistor option menu, by clicking it)

6) Resistor(220 ohms)

Step 2: Connecting the Components

Now we need to connect all the components according to our requirement. So for this, we need to think of simple logic. When we receive any input signal from push-button, only then we need to apply the output signal to LED connected to Arduino.

For this, place the push button in the bridge of the breadboard(as shown), and drag off a wire from one of the pins of the push button and connect it to positive series of the breadboard. Then connect the 10k-ohms resistor to the other pin of the resistor(as shown). Now this will act as a switch between the positive section and the resistor section.

Drag off a wire from the same terminal of the push-button connected to the resistor and connect it to pin 2 of the Arduino. This will act as the input from the push-button. Connect the other end of the resistor to the ground section(-ve) of the breadboard. Connect the positive section to the 5V supply of the Arduino and negative section to the GND(ground) of the Arduino.

Now we need to connect the LED to the pin 13(you can choose any) of the Arduino through the 220 ohms resistor.

Step 3: Writing the Code

Open the Code tab on the right side of the screen and select the coding mode as text and delete the existing code in it.

First, declare the button and LED pins connected to Arduino. Now we need a variable that can store the state of the button(acting as memory). So declare an integer variable for this and assign the default value as 0(being OFF state denoted as 0).

Now in setup function declare the led pin mode as OUTPUT and button pin mode as INPUT.

In the void loop function read the button state using digitalRead and store it in the variable.

Now check if the button state is HIGH, apply high voltage to led pin else low voltage.

Test the code by clicking the simulation.

Step 4: Demo!

If there is any issue, please let me know