0% Complete
0/25 Steps

Digital Input | What is Tactile Switch State?

Introduction

Digital Input: A digital input detects if a voltage is above/below a specific threshold. If the voltage is higher than some value, Arduino/evive will detect the digital input as HIGH or 1. If the voltage is lower than some value, evive will detect the digital input as LOW or 0.

According to the technical specifications of Arduino boards, digital input pin can be in either of the three states namely HIGH, LOW or Floating.

  • 0V – 1.5V: LOW
  • 3.3V – 5V: HIGH
  • 1.5V – 3.3V: floating (can either be HIGH or LOW)

How to use the digital pin as Input

Arduino/evive digital I/O pins are default assigned as inputs and hence they don’t need to be explicitly declared as input initially in the program compared to the output case. But it is advisable that one should declare each and every pin as input or output for consistency.

Digital Input pins can be configured input using this statement:

pinMode(pin, INPUT);

where the pin is the digital pin number you want to initialize.

To make the state of the digital pin to be correct for all cases, we have to make the voltage at the pin below 1.5V for reading as LOW and greater than 3.3V to be HIGH.

If the value is between 1.5V to 3.3V, then the result will be floating and any of the states may be possible. This can be done by adding a pull-up resistor (to +5V), or a pulldown resistor (resistor to ground) on the input. A 10K resistor is a good value for a pull-up or pull-down resistor. We will see it ahead.

Function for reading the state

Using digitalRead() function in Arduino IDE, you can read the value from a specific digital pin as HIGH or LOW. The statement is 

digitalRead(Pin);

where Pin is the digital pin for which we want to read the state. 

The function returns true or 1 if the state is HIGH and false or 0 if the state is LOW.

Tactile Switch

A switch is one of the most basic, but very important components in a circuit. When the switch is open, the circuit is broken, and no current will flow through it. When the switch is closed, the circuit is closed or made, and therefore, current will flow through it.

Switches can broadly be classified into two types:

  1. Mechanical Switches
  2. Electrical Switches

Mechanical switches can further be classified into two types:

  1. Maintained Switches: These switches stay in one state i.e. either ON or OFF, unless the state is changed by someone. E.g. wall switches. Once you press them, they remain in the same state, i.e. ON till the time you manually change it.
  2. Momentary switches: These switches stay in a state i.e. either ON or OFF, as long as they are activated. They come back to their original state once they are released. E.g. keys on a keyboard. They remain active only until the time they are pressed. As soon as you stop pressing them, they go back to their original state, i.e. OFF state.

Interfacing Switch with Arduino

Imagine that Arduino/evive has only one digital pin configured as an input. A code tries to read the state of the pin, while nothing is connected to it. It will be difficult to tell whether the state is HIGH or LOW. This phenomenon is known as floating as discussed earlier. To prevent this situation of an unknown state, either a pull-up or a pull-down resistor is connected to ensure that the pin is always either in either a HIGH or LOW state while using only a small amount of current. A pull-up or pull-down resistor is always used with switches.

With a pull-up resistor, the input pin reads a HIGH state when the switch is not pressed because the resistor is connected to the supply (+5V in this case) and as a result, a small current flows from the supply to the input pin. When the switch is pressed, it connects the input pin directly to ground. The current flows through the resistor to ground, bypassing the input pin; thus, the input pin reads a LOW state.

Pull Up Configuration

When a pull-down resistor is used, the input pin reads a LOW state when the switch is not pressed. As soon as the switch is pressed, it connects the input pin directly to the supply. The current flows through the resistor to ground, thus the input pin reads a high state.

Pull Down

Generally, a 10kΩ is considered both a good pull-up and a pull-down resistor.

Example: Changing the state of Pin 13 LED using a tactile switch

In this example, we will change the state of the LED each time the tactile switch is pressed.

Case I: Pull-up Resistor

Circuit

Follow the steps below to make the circuit:

  1. Take a red male-to-male jumper wire and connect one of its ends to the 5V supply pin above the breadboard, as shown in the circuit diagram. Take the other end and insert it in the 5th hole from the left of the first row of the breadboard.
  2. Take a black male-to-male jumper wire and connect one of its ends to the GND pin above the breadboard, as shown in the circuit diagram. Take the other end and insert it in the 6th hole from the right of the first of the breadboard.
  3. Take the tactile switch/push button and insert it across the dip in the breadboard such that it top-right leg is in the same column as that of the black male-to-male jumper wire.
  4. Take the 10kΩ resistor and insert one of its legs in the same column as that of the top-left leg of the tactile switch, exactly above it, as shown in the circuit diagram. Insert the other leg in the same column as that of the red jumper wire.
  5. Take an orange male to male jumper wire and insert one of its ends in the same column as that of the bottom-left leg of the tactile switch, as shown in the circuit diagram. Take the other end and connect it to digital pin 2, below the breadboard, as shown.

Below is the complete circuit diagram:

Pull Up Circuit

Pull Up Switch

Flow Chart

Below is the flow chart:

Pull Up Flow Chart

Arduino IDE Sketch

Below is the Arduino IDE Sketch:

Arduino Pull Up Switch LED Control

Case II: Pull-down Resistor

Circuit

Follow the steps below to make the circuit:

  1. Take a red male-to-male jumper wire and connect one of its ends to the 5V supply pin above the breadboard, as shown in the circuit diagram. Take the other end and insert it in the 9th hole from the left of the second row of the breadboard.
  2. Take a black male-to-male jumper wire and connect one of its ends to the GND pin above the breadboard, as shown in the circuit diagram. Take the other end and insert it in the 1st hole from the right of the first of the breadboard.
  3. Take the tactile switch/push button and insert it across the dip in the breadboard such that it top-left leg is in the same column as that of the red male-to-male jumper wire.
  4. Take the 10kΩ resistor and insert one of its legs in the same column as that of the top-right leg of the tactile switch, exactly above it, as shown in the circuit diagram. Insert the other leg in the same column as that of the black jumper wire.
  5. Take an orange male to male jumper wire and insert one of its ends in the same column as that of the bottom-right leg of the tactile switch, as shown in the circuit diagram. Take the other end and connect it to digital pin 2, below the breadboard, as shown.

Below is the complete circuit diagram:

Pull Down Switch

Flow Chart

Below is the flow chart:Pull Down Flow

Arduino IDE Sketch

Below is the Arduino IDE Sketch:

Log in with your credentials

or    

Forgot your details?

Create Account