Button controls an LEDTinkerPieCircuit tutorial 9 of 24 · Beginner · 10 minutes
Read a push button with INPUT_PULLUP and light an LED only while it is pressed.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- Digital inputs and INPUT_PULLUP
- Why a pressed button reads LOW
- if / else decisions in code
Parts
Steps
Build the LED
Wire
S1→ resistor → LED (+), and LED (−) →GND, just like the Blink tutorial.Add the button
Click Push button. Wire one side to pin
P10and the other side toGND.Use the pull-up
pinMode(P10, INPUT_PULLUP)keeps the pin at HIGH (3.3 V) until the button joins it to GND. A pressed button therefore reads LOW.Write the decision
In
loop(),if (digitalRead(P10) == LOW)turn the LED on, otherwise turn it off.Try it
Press ▶ Start, then press and hold the red cap of the button with your mouse or finger.
The code
function setup() {
pinMode(P10, INPUT_PULLUP);
pinMode(S1, OUTPUT);
}
function loop() {
if (digitalRead(P10) == LOW) {
digitalWrite(S1, HIGH);
} else {
digitalWrite(S1, LOW);
}
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
The board has a small resistor inside that pulls the pin up to 3.3 V. Pressing the button connects the pin to GND (0 V), so the program sees LOW.
← Blink an LED · All 24 circuit tutorials · Dim an LED with a knob →