Button

Introduction

Use a button on a digital pin to control an LED.

A breadboard with a button that turns on an LED

Step 1

Add a ||input:on event|| to handle a “button click” on pin D0.

input.buttonD0.onEvent(ButtonEvent.Click, function () {
})

Step 2

Add ||pins:digital write|| to set pin D1 HIGH when D0 is clicked.

input.buttonD0.onEvent(ButtonEvent.Click, function () {
    pins.D1.digitalWrite(true)
})

Step 3

Look at the simulator and notice that a breadboard and a button has been added to the board. Try pressing on the button and the LED should light up.

A simulated boardboard turning on a LED

Step 4

Add ||loops:pause|| to wait some time, then another ||pins:digital write|| to turn pin D1 to low.

input.buttonD0.onEvent(ButtonEvent.Click, function () {
    pins.D1.digitalWrite(true)
    pause(1000)
    pins.D1.digitalWrite(false)    
})

Step 5

Try your program in the simulator. If it works as expected, click on the |Download| button and follow the instructions to get it on your board.

Step 6

Click on the wrench icon under the simulator to print detailed breadboard wiring instructions.

Wrench icon

feature=uf2
feature=pind1
feature=buttond0