Activity E3 — Buttons

Level 1 — Use a button on a breadbard

Complete the challenge

A button with labelled pins
A breadboarded circuit with an Arduino, an LED and a button
void setup() {
    pinMode(5, OUTPUT);       // set up pin 5 as an output.
    pinMode(2, INPUT_PULLUP); // set up pin 2 as an input.
}

void loop() {
    if (digitalRead(2) == LOW) {  // check if the input is LOW
        digitalWrite(5, HIGH);    // turn the LED on
    } else {
        digitalWrite(5, LOW);     // turn the LED off
    }
}

Claim your achievement