Exertion / Mechanical

Wires as Button

Wires made from electrically conductive material can be used as rudimentary switches. No components needed – just wire!

This provides not only a cheap and quick way to rig up a simple button for testing, but also opens up a world of possibilities:

  • Lightweight metal foil attached to the wires using metal paper clips
  • Bare metal wire mesh squashed against another
  • Soap bubbles or water making momentary contact between two wires

Wiring & Code

Select a microcontroller platform in the tabs below to view the wiring and code meant for the platform:

  • Breadboard diagram

    Wires as Button & LED circuit

    Wires as Button & LED circuit

    Download Wires as Button Fritzing file

    Code

    For a refresher on how to use the code in this recipe, click here.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
    // Wires as Button sensing code for the Arduino UNO
    // Touch the sensing wire to Ground and the LED lights up
    
    // update these definitions below if you move the pins:
    #define BTN_PIN 2                   // digital input pin 2; in this case it's just a wire!
    #define LED_PIN 11                  // digital/PWM pin 11
    int btnVal = 0;                     // this is a variable; we use this to keep track of the button state (see line 16)
    
    void setup() {                      // runs once at startup
        pinMode(LED_PIN, OUTPUT);
        digitalWrite(LED_PIN, LOW);     // turn off the LED at startup
        pinMode(BTN_PIN, INPUT_PULLUP); // a special command to 'stabilise' the button state
    }
    
    void loop() {                       // runs forever
        btnVal = !digitalRead(BTN_PIN); // read the state of the button (0 = pressed; 1 = released)
        digitalWrite(LED_PIN, btnVal);  // this writes out an ON (1) or OFF (0) state to the LED for visual confirmation
    }
    
    /*
        Please note that the code provided here is licensed under the MIT license.
    
        The MIT License (MIT)
        Copyright © 2024 Chuan Khoo
    
        Permission is hereby granted, free of charge, to any person obtaining
        a copy of this software and associated documentation files (the
        "Software"), to deal in the Software without restriction, including
        without limitation the rights to use, copy, modify, merge, publish,
        distribute, sublicense, and/or sell copies of the Software, and to
        permit persons to whom the Software is furnished to do so, subject to
        the following conditions:
    
        The above copyright notice and this permission notice shall be included
        in all copies or substantial portions of the Software.
    
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
        OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
        OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
        ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
        OTHER DEALINGS IN THE SOFTWARE.
    */
    
  • COMING SOON…

    More code samples for this component for other platforms will be added here over time.


This page was last updated: 23 Sep 2024