

In other words, we have to sense the closed state of push button switch to turn LED ON and OFF. This means a push button press always gives us a “ HIGH” state. I used a “ normally open” mini push button switch to implement the circuit. Inside the setup() function, we have configured this pin 7 (the SW variable) as input using pinMode() instruction. The push button switch is connected to pin 7 and we have assigned this pin 7 to a variable named SW inside our sketch. We have used the variable val to store the value returned by the instruction digitalRead(). Since it returns a value, we have to assign this instruction to a variable while we write the program. In our example this instruction reads voltage level at pin number 7 returns HIGH if its +5 volts and returns LOW if its 0 volts.

In our program, this instruction reads the status at pin 7 and returns a value according to what it has read. APL (Arduino programming language) has an instruction named digitalRead() – which reads a digital input given at the configured input pin. In this program, the highlight is instruction to read push button state. Lets get into the program side of controlling LED using push button switch. This is how ardunio distinguishes between closed (ON) and open (OFF) states of push button switch. This same potential is at pin 7 as well and will be considered as state LOW. On this state, the voltage across 10K resistor is 0 volts (ground potential). On the other hand, when the push button is released (residing in its normally open state), there is no reference voltage line connected at pin 7. So when push button is pressed, a +5 volts is available at pin 7 and this will be considered as state HIGH. Â This voltage will drop across the 10K ohm resistor. When the push button is pressed, the reference voltage line will get connected to pin number 7. Arduino board has a readily available +5 volts reference on power pins cluster. The reference voltage is used to detect ON state or closed state of the push button. To avoid a short circuit between pin number 7 and ground, you should connect a resistor (preferable a 10K ohm) in between. A reference voltage should be connected to one end of switch and the other end of switch should be connected to ground. So we should connect the push button switch to pin 7 of arduino as shown in circuit.

In this example, I have set pin number 7 as a digital input. Â To connect push button to arduino, we need one of the digital I/O pins configured as a digital input. I have added the circuit diagram to control LED with arduino using a push button switch.
