Physical Computing

Readings

Links

Arduino.cc Links

Class Websites



















(:source lang=arduino_cpp :)[@
image upload pmwiki-2.2.106, 2002106

 edit SideBar

Log out

Devices/Switch

Momentary Switch as Digital Sensor

In many cases switches are just switches. They directly control the flow of electricity to an appliance, flashlight or mains-voltage lamp. An example of this is the switch on the wall in your living room. In many cases nowadays however, switches are digital sensors, meaning that instead of directly controlling a high-powered device, they are controlling a logic (low power) signal to a microcontroller (eg Arduino), telling the microcontroller which state a user prefers, High or LOW, On or Off, 1 or 0 (they all amount to the same thing).

We don't often think about switches as sensors, chiefly because they are older technology, with which we are completely familiar. Nevertheless, a switch can always be used as a digital sensor, transmitting one bit of information, which is whether the switch state is Off or On (which can also be defined as 0 & 1).

This wiki entry focuses on momentary switches but it is important to note that many other types of switches, such as toggle switches or slide switches, can be wired in exactly the same manner and used as digital sensors, with the same code examples as used below.


A breadboard mounted momentary switch A diagram showing the connections on a momentary switch A momentary (pushbutton) switch designed for panel mount

The schematic symbols for switches can vary somewhat. The symbol for S2 is more descriptive of a breadboard-mounted momentary switch but the basic idea is exactly the same, a connection is being created by pushing the switch.

The are a couple of confusing things to consider in using momentary switches. One aspect is that the switch has four leads but only two wires are actually being switched (if you check the schematic symbol). The switching action happens between the leads spaced at the shortest distance. The other two leads are connected internally along the "longer" side of the switch. The middle diagram above shows the idea. You can easily prove out which leads are shorted, if you forget, but using your mulitmeter on the continuity setting (aka "beep setting").

Why do I need a pulldown or pullup resistor?

Students often have a hard time grasping why we need a pulldown or pullup resistor on a switch. One way of explaining this is in describing the nature of the Arduino pin when it is set to INPUT. The pin is described as being in a "high-impedance state". This means that it requires very little current to switch the pin from HIGH to LOW or vice versa. Without a pullup or pulldown, the input pin can be thought of as "flapping in the wind" and any small amount of charge is capable of flipping the pin's state. This charge could come from any capacitive field (all conductors act as capacitors to some extent). Often an Input pin which is left "floating" will capacitively couple to nearby pins and follow them.

The purpose of a pullup or pulldown is to steer a pin into a known state, until the switch is actuated, or an input in sensed. The pullup or pulldown resistor, banishes this "flapping in the wind" and gives the input a known behavior.

Different ways of wiring a switch

There are three different ways of wiring a switch (on an Arduino pin) with a pullup or pulldown resistor.

  1. A switch may be wired with an external pulldown resistor
  2. A switch may be wired with an external pullup resistor
  3. A switch may use the internal pullup resistors in the Atmega chip

Momentary switches wired with external pulldown resistors

A schematic diagram for a breadboard-mount momentary switch A schematic diagram for a panel-mount momentary switch
A momentary switch mounted on a breadboard with a pulldown resistor. Note that the red breadboard rail is connected to 5V and the blue breadboard rail is connected to ground somewhere beyond the frame of the photo.
A a panel-mount switch wired with a pulldown resistor. Note that the red breadboard rail is connected to 5V and the blue breadboard rail is connected to ground somewhere beyond the frame of the photo.
/*
  Button Test Sketch (LED optional)
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 The circuit:
 * LED attached from pin 13 to ground 
 * momentary switch attached to pin 2 from +5V
 * 10K resistor attached as pull-down resistor
*/
//constants
const int buttonPin = 2;     // the pushbutton pin
const int ledPin =  13;      // the LED pin

// variables 
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);  // initialize buttonPin as an input 
                              // not really required (pins default to INPUT)
  Serial.begin(9600);         // initialize Serial for debugging feedback
}

void loop()
{
  buttonState = digitalRead(buttonPin);     // read the state of buttonPin
  Serial.println(buttonState);

  if (buttonState == HIGH)       // check if the pushbutton is pressed.
  {     
    digitalWrite(ledPin, HIGH);  // turn LED on: (send +5V to pin)
    Serial.println("Button is       ON ");    //  print to serial monitor
  } 
  else 
  {
    digitalWrite(ledPin, LOW);       // turn LED off: (send 0V to pin)
    Serial.println("Button is OFF"); // print text to serial monitor
  }
  delay(200);  // delay so that print to serial monitor is not too fast
}

Momentary switch wired with external pullup resistor

I don't have photographs of this setup but you might be able to guess from the schematic that you can just switch the wires at the power rails, to their inverse, so the GND wire becomes +5V and 5V wire becomes GND. Everything else stays the same.

/*
 Button Test Sketch with external pullup resistor (LED optional)
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 The circuit:
 * LED attached from pin 13 to ground 
 * momentary switch attached to Arduino pin 2
 * other side of momentary switch attached to GND
 * 10K resistor attached as pullup resistor to 5V
*/

//constants
const int buttonPin = 2;     // the pushbutton pin
const int ledPin =  13;      // the LED pin

// variables 
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);  // initialize buttonPin as an input 
                              // not really required (pins default to INPUT)
  Serial.begin(9600);         // initialize Serial for debugging feedback
}

void loop()
{
  buttonState = digitalRead(buttonPin);     // read the state of buttonPin
  Serial.println(buttonState);

  if (buttonState == LOW)        // buttonState is LOW when pressed.
  {     
    digitalWrite(ledPin, HIGH);  // turn LED on: (send +5V to pin)
    Serial.println("Button is       ON ");    //  print to serial monitor
  } 
  else 
  {
    digitalWrite(ledPin, LOW);       // turn LED off: (send 0V to pin)
    Serial.println("Button is OFF"); // print text to serial monitor
  }
  delay(200);  // delay so that print to serial monitor is not too fast
}

Momentary switch wired with internal pullup resistor

Once you have the sketch above working, you can just eliminate the pullup resistor. We will use the Atmega (Arduino) chip's internal pullup resistor. This simplifies the switch wiring a bit. The only down side to this arrangement is that it might be slightly harder for beginners to think about LOW as being the active (pushed) state than "HIGH".

Pros and Cons of using an internal vs an external pullup when wiring a switch as a sensor

Pros

  • Only requires two wires - a digital pin and ground
  • Hardware hookup is less confusing for beginners.

Cons

  • Software is slightly more confusing for beginners.
  • Switch is "active low". It will report "high" until activated, then go "low".
A schematic diagram for a momentary switch with an internal pullup resistor A conceptual schematic diagram showing the built-in pullup

OK - So how do I turn on this internal pullup resistor?

It turns out to be really simple to turn on the internal pullup. Just issue a digitalWrite(pin, HIGH) command while the pin is an input. So the code for the internal pullup is the same as as above with the addition of one line.


/*
 Button Test Sketch with internal pullup resistor (LED optional)
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 The circuit:
 * LED attached from pin 13 to ground through a 1K series resistor.
 * one side of momentary switch attached to Arduino pin 2
 * other side of momentary switch attached to GND
 * That's it. We're done!
*/

//constants
const int buttonPin = 2;     // the pushbutton pin
const int ledPin =  13;      // the LED pin

// variables 
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);  // initialize buttonPin as an input 
                              // not really required (pins default to INPUT)
  Serial.begin(9600);         // initialize Serial for debugging feedback
  digitalWrite(buttonPin, HIGH);  // turn on internal 20K pullup resistor
}

void loop()
{
  buttonState = digitalRead(buttonPin);     // read the state of buttonPin
  Serial.println(buttonState);

  if (buttonState == LOW)        // buttonState is LOW when pressed.
  {     
    digitalWrite(ledPin, HIGH);  // turn LED on: (send +5V to pin)
    Serial.println("Button is       ON ");    //  print to serial monitor
  } 
  else 
  {
    digitalWrite(ledPin, LOW);       // turn LED off: (send 0V to pin)
    Serial.println("Button is OFF"); // print text to serial monitor
  }
  delay(200);  // delay so that print to serial monitor is not too fast
}

See also

Edit - History - Recent Changes - Search
Page last modified on April 12, 2018, at 06:56 AM