Zum Hauptteil springen

wokwi-pushbutton Reference

12mm Tactile Switch Button (momentary push button).

Pin names

NameDescription
1.l / 1.rFirst contact (left / right)
2.l / 2.rSecond contact (left / right)

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.

The following diagram illustrates the connections inside the pushbutton:

Pushbutton connection diagram

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

NameDescriptionDefault value
colorThe color of the push button"red"
labelText that appears below the button""
keyKeyboard shortcut for button
bounceSet to "0" to disable bouncing""

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. Note the the special key names are case sensitive - so "Escape" will work, "escape" won't.

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.

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. 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.

Examples

ResultAttrs
{ "color": "green" }
{ "color": "#FFFF00" }
{ "label": "Push me!" }

Automation controls

The pushbutton can be controlled using Automation Scenarios. It exposes the following controls:

ControlTypeDescription
pressedintSet to 1 to press the button, 0 to release it.

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

  - set-control:
part-id: btn1
control: pressed
value: 1
- delay: 200ms
- set-control:
part-id: btn1
control: pressed
value: 0

Simulator examples