> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/wokwi/wokwi-docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Pushbutton (12mm)

> 12mm Tactile Switch Button - momentary push button

12mm Tactile Switch Button (momentary push button).

## Pin names

<ParamField path="1.l" type="Contact">
  First contact (left)
</ParamField>

<ParamField path="1.r" type="Contact">
  First contact (right)
</ParamField>

<ParamField path="2.l" type="Contact">
  Second contact (left)
</ParamField>

<ParamField path="2.r" type="Contact">
  Second contact (right)
</ParamField>

The push button has two set of pins (contacts), 1 and 2.
When the push button is pressed, it connects these two contacts, thus closing an electrical circuit.

Each contact has a pin of the left side of the push button, and another pin on the right side of the push button.
So pin `1.l` is the left pin for first contact, and `1.r` is the right pin for the first contact. Since both belong
to the same contact, they are always connected, even when the button is not pressed.

When working with Arduino, you'd usually connect one contact (e.g. `1.r` or `1.l`) to a digital pin and configure
that pin as `INPUT_PULLUP`, and the other contact (e.g. `2.r` or `2.l`) to the ground. The digital pin will read
`LOW` when you press the button, and `HIGH` when the button is not pressed.

## Attributes

<ParamField path="color" type="string" default="red">
  The color of the push button
</ParamField>

<ParamField path="xray" type="string" default="">
  Show internal wiring: "1" or ""
</ParamField>

<ParamField path="label" type="string" default="">
  Text that appears below the button
</ParamField>

<ParamField path="key" type="string">
  Keyboard shortcut for button
</ParamField>

<ParamField path="bounce" type="string" default="">
  Set to "0" to disable bouncing
</ParamField>

## Defining a keyboard shortcut

You can use the "key" attribute to define a keyboard key that will control the button.
The key is only active when the simulation is running and the diagram has focus.

For example, suppose you defined "key" to "Q". Then, when you run the simulation,
pressing *Q* in the keyboard will press the push button. The button will be kept
in pressed state as long as you keep pressing *Q*, and once you release the key,
the button will also be released.

You can define any alphanumerical keyboard shortcut (so English letters and numbers), and for letters,
the value of "key" is case insensitive (so "q" and "Q" mean the same).

You can also target some special keys, such as "Escape", "ArrowUp", "F8", " " (space), or "PageDown", but some keys
could be blocked by the browser (e.g. "F5" that refreshes the page).
The full list of key names can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values).
Note the the special key names are case sensitive - so "Escape" will work, "escape" won't.

<Note>
  Firefox users: if the keyboard shortcuts don't work for you, please make sure that the "Search for text when you start typing" setting is disabled.
</Note>

## Bouncing

When you press physical pushbutton, the circuit opens and closes tens or hundreds of times.
This phenomenon is called Bouncing. This happens because of the mechanical nature of pushbuttons:
when the metal contacts come together, there's a brief period when the contact isn't perfect, which
causes a series of rapid open/close transitions.

Wokwi simulates button bouncing by default. You can disable bouncing simulation by setting the
"bounce" attr to "0":

`{ "bounce": "0" }`

The bouncing simulation follows the behaviour described in "The Art of electronics" by Horowitz & Hill:

> When the switch is closed, the two contacts actually separate and reconnect, typically 10 to 100
> times over a period of about 1ms.

For example, [this project shows the difference between bouncing and non bouncing button](https://wokwi.com/projects/288681423014986248). It has two buttons connected to the same Arduino input pin:

* The blue button does not simulate bouncing. Pressing on it once will only print a single pair of "pressed" and "released" messages.
* The red button simulates bouncing. Pressing on it once will print multiple "pressed" and "released" messages.

## Stickiness

If you want the button to stay pressed, Ctrl-click it (Cmd-click on Mac). It will cause the button to stay pressed until the next click.
This is useful when you need multiple buttons pressed at the same time.

## Automation controls

The pushbutton can be controlled using [Automation Scenarios](/wokwi-ci/automation-scenarios). It exposes the following controls:

<ParamField path="pressed" type="int">
  Set to 1 to press the button, 0 to release it.
</ParamField>

The following example simulates a button press on "btn1" for 200ms:

```yaml theme={null}
- set-control:
    part-id: btn1
    control: pressed
    value: 1
- delay: 200ms
- set-control:
    part-id: btn1
    control: pressed
    value: 0
```

## Simulator examples

* [Basic example: Arduino and pushbutton, active-high](https://wokwi.com/projects/397990618860958721)
* [Basic example: Arduino and pushbutton, active-low](https://wokwi.com/projects/397990611031240705)
* [Bounce vs non-bounce](https://wokwi.com/projects/288681423014986248)
* [Simon Game](https://wokwi.com/projects/344891334169985618) - A memory game with 4 push buttons
* [Diatonic Piano](https://wokwi.com/projects/291958456169005577) - A 8-note piano, use keys 1-8 to press the buttons and play the notes.
