Input Pull-Up / Pull-Down

OVERVIEW

At some point you will have to connect and use switches to control stuff in your Arduino projects.

Here are some basics to successfully use switches and make sure they respond correctly.

tact switch arduino

Most switches are simple mechanical devices that basically make a connection between two inputs.

In this tutorial we will be using a standard tact switch.

A tact switch is a momentary mechanical switch that makes a connection when the switch is pressed.

There are two things we can detect with an Arduino:

  • Detect when the switch is pressed or not pressed
  • Detect if the switch is HIGH (5V) or LOW (Ground) in either of those 2 states
Why do we need a resistor when using a switch?

A typical tact switch has 4 pins, but has you can see from the picture below some are connected together which means that they basically have 2 pins.

tact_switch_connections

You connect one of those either to Ground (LOW) or 5V (HIGH)  and connect the other one to an Arduino Digital pin.

Tact_connection_01

This is an example of ‘FLOATING’

When the switch is pressed it makes the connection between those two pins and connects the Arduino Digital pin to either Ground or 5V which we can detect in code and do the appropriate action.

But when the switch is not being pressed it enters a state of “Floating”.

Floating is when the Arduino Digital pin is neither High or Low and results in noise.

So to fix this we connect the tact switch pin that is connected to the Arduino Digital pin to either Ground or 5V depending on what we are trying to read when the switch is pressed.

So if we want to read LOW when the switch is pressed we would connect the 5V, which will results in the switch being “Pulled UP” when the switch is not being pressed and the Arduino would read the Digital pin HIGH without any noise.

Tact_connection_02

Here the pin is Pulled UP to 5V when the switch is not being pressed…

Now we can’t just go ahead and connect that pin directly to ground or 5V since that would create a short circuit when the switch is pressed, this is why we need to use a Resistor.

The Resistor prevents the short circuit by limiting the amount of current to flow when the switch is pressed.

Resistor value and Arduino integrated PullUp resistors

In your projects you can either use external resistors or the ones available inside the Arduino.

The Arduino UNO has PullUp resistors available on each one of the Digital Pins.  These are activated inside your code and have a value between 20k and 50k (average of around 34k).

Notice that the Arduino UNO only has PullUp resistors and no PullDown resistors, which mean that using the Arduino resistors only enables you to make the state of the switch when it’s not pressed to HIGH (PullUp), which is not a big problem since most of the time you want to read HIGH when not pressed and LOW when the switch is pressed.

To my knowledge only the Arduino ZERO has both PullUp and PullDown resistors.

If you project need to read LOW when the switch is not pressed then you will need to use external resistors, and a value between 20k and 50k will provide the same results as if using the Arduino resistors.

Connection Diagrams

In this tutorial we will look at 4 different connections scenarios:

  • Demonstrate the Floating state

Arduino Tact Switch 01

  • Using a resistor to PullUp

Arduino Tact Switch 03

  • Using the Arduino integrated resistor to PullUp

Arduino Input PullUp

  • Using a resistor to PullDown

Arduino Tact Switch 04

THE CODE

To visualize what happens when I press the tact switch, I’m using a MAX7219 LED Matrix.

The Matrix will display a ZERO or a FIVE (Ground or 5V) when the switch is pressed.

If the scenario is a PULLUP then the matrix will display a FIVE when the switch is not pressed and a ZERO when it is pressed.

If the scenario is a PULLDOWN then it will do the opposite.

You can find links to the library used to control the LED Matrix at the bottom of this page.

As always don’t forget to watch the tutorial video for more information.

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::ICSTATION_HW
/*  PAROLA_HW,    ///< Use the Parola style hardware modules.
    GENERIC_HW,   ///< Use 'generic' style hardware modules commonly available.
    ICSTATION_HW, ///< Use ICStation style hardware module.
    FC16_HW       ///< Use FC-16 style hardware module.
*/

#define MAX_DEVICES 1
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

int switchpin=5;

// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);


void setup(void)
{
  P.begin();
  P.setTextAlignment(PA_CENTER);
  
  pinMode(switchpin, INPUT_PULLUP); 
  // this will use the Arduino PullUp resistors
  
  // pinMode(switchpin, INPUT); this needs external resistor to work
}

void loop(void)
{
    if (digitalRead(switchpin) == LOW) P.print(0);
    else P.print(5); 
  }

Les commentaires sont clôturés