Digital input — Button
Load _02_Button.ino — this sketch uses the ThinkerShield button (pin 7)
to control the D12 LED. Press the button: the LED comes on. Let go: it goes
off. The button is a digital input — it's only ever on or off (a binary input).
New ideas in this PRP:
if/elsestatements let the program make a decision based on a condition (here, "is the button pressed?"). Think of a night light: the processor decides to turn the light on if it's dark.- Comparison operators:
==equal to,!=not equal to,<less than,>greater than,<=/>=less-than/greater-than-or-equal. Note: a single=assigns a value; a double==compares two values. - The
intcommand (_03_int.ino) lets you give a pin a name — e.g.int LED = 12;— so the code reads clearly and you only have to change a pin in one place. The bit beforevoid setup()is the declarations section. You can't reuse a word Arduino already knows (if it changes colour, pick another).
Then: write pseudocode for "turn the LED on when the button is pressed, off when released"; draw the flowchart; answer the "making connections" questions; and tackle the challenges:
- reverse the action — the LED is always on unless the button is pressed;
- make the LED stay on for 3 seconds after the button is released;
- turn the button into a toggle switch — press once for on, press again for
off (tricky — you'll need a variable to remember the LED's state and a short
delay()to stop the button "bouncing").
Get a partner or your teacher to tick off each challenge, then complete the evaluation questions.
What you'll learn
Use a digital input — the ThinkerShield button — to control the LED with an `if`/`else` statement. Learn to give pins friendly names with the `int` command.
What you'll make
- I can press the button and turn the LED on; release it and turn the LED off.
- I can explain what each line of
_02_Button.inodoes. - I can flip the action so the LED is on unless the button is pressed.
- I can use
int buttonPin = 7;to give a pin a name.
What you need
Arduino Uno + MAAS ThinkerShield + USB cable, a laptop with the Arduino IDE.
Sample code
int buttonPin = 7; // the pushbutton is on pin D7 of the ThinkerShield
int ledPin = 12; // the LED is on pin D12
int buttonState = 0; // variable for reading the pushbutton's state
void setup() {
pinMode(ledPin, OUTPUT); // the LED pin is an output
pinMode(buttonPin, INPUT); // the button pin is an input
}
void loop() {
buttonState = digitalRead(buttonPin); // read the button
if (buttonState == HIGH) { // is it pressed?
digitalWrite(ledPin, HIGH); // yes -> LED on
} else {
digitalWrite(ledPin, LOW); // no -> LED off
}
}
int LED = 12; // call pin 12 "LED" for the rest of this sketch
void setup() {
pinMode(LED, OUTPUT); // make the LED pin an output
}
void loop() { // runs over and over again, forever
digitalWrite(LED, HIGH); // turn the LED on
delay(1000); // wait for one second
digitalWrite(LED, LOW); // turn the LED off
delay(1000); // wait for one second
}
Steps
- Open
_02_Button.inofrom the Sample code list above and upload it. - Press the button on the ThinkerShield (pin D7). The D12 LED should turn on. Release. LED off.
- Complete the IPO chart in your workbook.
- Challenges:
- Reverse the action — change
==to make the LED stay on unless the button is pressed. - Stay on for 3 seconds — add a
delay(3000);after the LED-on line. - Toggle switch — make the LED stay on after one press, and turn off on the next press (you'll need an
int ledState = 0;variable).
- Reverse the action — change
- Get it signed off.
- Reflect.
IPO chart
Inputs
- Pushbutton on the ThinkerShield, pin D7 (digital — reads HIGH or LOW)
Processing
- void setup() sets the LED pin as OUTPUT and the button pin as INPUT
- void loop() reads the button: buttonState = digitalRead(buttonPin)
- if buttonState == HIGH → turn the LED on; else → turn the LED off
Outputs
- LED on the ThinkerShield, pin D12
Stuck? Common things to check Tap to open
=vs==— single=assigns; double==compares. The IDE won't always warn you.- Pin 2 vs pin 7 — some printed copies of the workbook use pin 2; on the ThinkerShield the button is pin 7. Use 7.
Take it slower
Stick with the basic Button sketch and the Reverse the action challenge. Pair up.
Push further
Use _03_int.ino as a model — give every pin a friendly name in your code.
Done when…
Working button sketch + reversed-action challenge + IPO chart + reflection.
Reflect
What's the difference between a digital input and an analog input?