A button on Arduino Uno
Steps done: 0 of 5
Guess the answer and press Run
D2LOWD8LOWtime, s0.0
Lesson steps
What will D2 read with no press?
Nobody is touching the button yet. Pick an answer before you run it.
Sketch
// Button between D2 and GND, LED on D8 through 220 Ω
const int button = 2;
const int led = 8;
int lightMs = 1000; // stays on after release, ms
int pollMs = 10; // how often to read the button, ms
unsigned long offAt = 0;
int was = HIGH;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(button);
if (state == LOW && was == HIGH) Serial.println("нажата");
if (state == LOW) offAt = millis() + lightMs;
digitalWrite(led, millis() < offAt ? HIGH : LOW);
was = state;
delay(pollMs);
}
Tap a line or an orange number
Serial monitor
Serial, 9600 baudboard time 0.0 s
About the lesson
A button on a breadboard between D2 and GND, the INPUT_PULLUP pull-up inside the board, an LED on D8 stays on for another second after release. You hold the button on the scene with a mouse or a finger. About 10 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.
Build it for real
Parts
- Arduino Uno R3
- Mini breadboard
- 6×6 mm button
- 5 mm LED
- 220 Ω resistor
- 4 male-to-male jumper wires
Wires
| From | To |
|---|---|
| D2 | button, one side |
| Button, other side | GND |
| D8 | 220 Ω resistor, then the LED anode |
| LED cathode | GND |
Put the button across the breadboard channel: then its legs land in four different columns. If the button seems pressed all the time, it is turned 90°: the legs that are connected inside ended up in different columns.
Open the schematic in the builderPinout of the lesson boardButton pinoutLED pinout
The sketch below goes to the board with the default numbers: lightMs 1000, pollMs 10. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.
The whole sketch
// Button between D2 and GND, LED on D8 through 220 Ω
const int button = 2;
const int led = 8;
int lightMs = 1000; // stays on after release, ms
int pollMs = 10; // how often to read the button, ms
unsigned long offAt = 0;
int was = HIGH;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(button);
if (state == LOW && was == HIGH) Serial.println("нажата");
if (state == LOW) offAt = millis() + lightMs;
digitalWrite(led, millis() < offAt ? HIGH : LOW);
was = state;
delay(pollMs);
}
What to keep in mind on a real board
Button contacts bounce: when pressed, they close and open for a few milliseconds. The sketch reads the button every 10 ms and rarely sees the bounce. A press counter with no pause between reads would count extra presses.
You can also add the pull-up outside: a 10 kΩ resistor from D2 to 5 V and pinMode(button, INPUT). The result is the same.
The sketch takes 2294 bytes of flash and 210 bytes of RAM.
Licenses of the lesson code
The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.
The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.
MIT license text: 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.