Arduino Digital Button with Pullup and Pulldown resistors

click to enlarge
Let us learn how a Pushbutton works on an Arduino's board. We will program the Arduino so that It can send a signal to a LED whenever the push button is pressed.

We can make the LED turn on with the press of the button, and with some rearrangement of components and the program we can make the LED turn off when the button is pressed.


Components

  • Arduino Uno's board with USB cable
  • one breadboard small
  • one Tact Switch
  • one LED
  • Resistors 220 Ohms, 10 K Ohms, one each.
  • Jumper wires (Male-Male)
You can easily find all of the components in any of the Arduino starter Kits. If you do not already have one with you, you will find some links in the right column of this page. 

Prerequisite

You will need the Arduino's programming software installed on your computer. If you haven't already done so, you can download it from this link.

Circuit

We will do this experiment in two different circuits. In one we will use them as Pull Down resistors and in another circuit we will connect then as Pull Up resistors. In one case the LED will be normally off and in another it will be normally on.


LED Normally Off

click to enlarge
We will be using two digital ports of Arduino Uno board. Pin 13 and Pin 2. You can use any other pin. There is nothing particular about the pin numbers.

The Tact switch is placed over the groove on the breadboard. In this position you can use both sides of the breadboard for connecting components to the switch. I hope you may have some idea of the pins of the Tact switch. If not, you will find this article useful to understand more about Tact Switches.

Place the LED on the breadboard, and note the shorter and longer pins. The longer pin is the positive and the shorter is the negative. 

We will be connecting the 10 Kilo Ohms resistor to one of the sides of the Push button / Tact Switch. Does not matter which side you connect it to, the other end of the resistor can go into the blue lane of the breadboard. 

You can connect this circuit in many different ways. But I am using minimum number of wires to do that. 

We will connect the 220 Ohms resistor to the negative pin of the LED and the other end of the resistor can go in the blue lane of the breadboard.

You can do this if you have a fairly good idea of how the connections on the breadboard are done. If not then this article can give you a primer on breadboard.

We will connect the 5V and Gnd pins on Arduino Uno board with the switch. The 5V pin can be connected with a Male-Male jumper wire to one end of the switch on the breadboard. The Gnd pin can be connected with a Male-Male jumper wire with the blue lane on the breadboard. This will provide ground for both the resistors.

We will connect the ground side of the switch to digital pin number 2.

We will connect the longer pin of the LED to digital pin number 13.

Now connect the Arduino board to the computer with the USB cable, and open the example program.

File - Examples - Digital - Button

Use the Upload button or the menu Skecth - Upload. When the upload is complete you can test it by pressing the button. The LED should glow when you press the button and turn off when you let go of your finger on the button.

To understand how this program is written, I have created a trimmer version of the same program, which works the same, but it easier to understand the programming.

click to enlarge

We need an integer variable to store the value of the input on pin 2. This is either High, or Low. Pin 2 receives High when the button is pressed, and Low when it is not. The Pull down resistors are useful to maintain this state. 

In the setup function we assign pin 2 as our input port and pin 13 as the output port. 

In the loop function we read the value received at the port 2, and send signal to the LED through the port 13. 

So it is that simple.


LED Normally On

click to enlarge

This circuit after uploading the modified program will keep the LED on all the time, the LED will turn off as and when you press the switch and it will remain off as long as you keep the button pressed. 

We would change the resistor's position. If you compare this circuit with the earlier one, you will notice that we have connected the 10 Kilo Ohm resistor to  + 5 V instead of Ground. The other end of the switch will be connected to ground. And we connect the same end to which resistor is attached to the digital pin2 ( yellow wire ) This will keep the pin2 normally HIGH, therefore the LED will also receive HIGH thus it will be normally on except when you press the button.  

In case of the LED we will connect the 220 Ohms resistor between the shorter pin of the LED ( the cathode, -ve) and the ground.

The resistors that is connected to the 5V power pin is called the Pull up resistors, The pullup resistor will help to keep the signal voltage normally high.

Now the program. 

The same program will apply to both of the circuits. 

Summary

This is how the digital input and output in Arduino takes place. And this is how a resistor connected to the power supply can be used in two different ways, pullup and pulldown.