GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Beginner Kit · Lesson 03 of 12

Press to Play

So far your circuit talks. Today it learns to listen. Add a button and take command.

Unlock your lessons

Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.

Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.

LICENSED TO , PERSONAL USE · © GAMERIFT

BRIEF Machines that listen

Until now your circuit has been a performer: it does its show whether you watch or not. Real machines are different. A console waits for the jump button. A kettle waits for the switch. Today you add the kit's most humble part, a little button, and suddenly your circuit has an input. It reacts to you. This is the half of electronics that makes games possible.

Time: 45–60 minutes · Difficulty: 2/5 · You need: Lessons 01 and 02 done.

KIT What you need

Concept · Inputs and outputs

Every pin on your board can be a mouth or an ear. OUTPUT means the pin speaks, sending volts out, like it does to your LED. INPUT means the pin listens, reporting what voltage arrives. All of computing is this: read inputs, decide, set outputs. You are about to write your first program that decides.

Concept · The floating pin problem

Here is a strange truth: an input pin connected to nothing does not read "off". It reads static from the air, flickering randomly between HIGH and LOW like a radio between stations. Engineers call this a floating pin. The fix is to gently pull the pin to a known voltage when the button is not pressed. Your board has tiny built-in pull-up resistors for exactly this, switched on with the magic word INPUT_PULLUP.

Concept · Upside-down logic

With INPUT_PULLUP, the resting pin reads HIGH, and pressing the button connects it to ground, so it reads LOW. Yes, pressed = LOW. It feels upside down the first time. Every engineer trips on this once; you are tripping on it now so your code never has to.

BUILD Wire it

Unplug the USB first. The button's four legs come in two joined pairs; the legs on the same side of the little gap are connected to each other. Push the button in so it straddles the middle channel of the breadboard, it clicks in flat.

FromToWhy
R3 pin 4Button leg (top-left)The listening wire
Button leg (bottom-left)R3 GNDPressing connects pin 4 to ground
R3 pin 13Resistor → LED long legSame faithful light as Lesson 01
LED short legR3 GNDLoop complete

Two legs of the button do all the work. If your button seems to be "always pressed", you have used two legs from the same joined pair, move one wire to the other side of the gap.

CODE Program it

Type it, don't paste it. Meet the most important word in programming: if.

// GAMERIFT Beginner Kit, Lesson 03: Press to Play
// Your first machine that listens and decides.

const int buttonPin = 4;
const int lightPin  = 13;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // listen, with the built-in pull-up on
  pinMode(lightPin, OUTPUT);         // speak
}

void loop() {
  int state = digitalRead(buttonPin);  // what does the pin hear right now?

  if (state == LOW) {            // LOW means PRESSED (upside-down logic!)
    digitalWrite(lightPin, HIGH);  // light on while held
  } else {
    digitalWrite(lightPin, LOW);   // light off when released
  }
}
  1. Upload it.
  2. Hold the button: light on. Let go: light off.
  3. Notice there is no delay() anywhere, loop() runs thousands of times a second, checking the button each time. Your board is very attentive.

BREAK IT Now break it on purpose

  1. Pull the button's GND wire out and wave your hand near the wires. With the pull-up on, the light should stay politely off. Now change INPUT_PULLUP to INPUT in the code and upload. Watch the light flicker to ghost presses, that is a floating pin. Change it back and respect the pull-up forever.
  2. Swap the if. Make it if (state == HIGH). Now the light is on until you press. You just made a "release detector". Some machines really work this way (fridge door light, for one).
  3. Press really, really fast. Can you beat loop()? (You cannot. It checks the pin about 100,000 times per second.)

REMIX Make it yours

LOG IT Show and tell

Film 15 seconds of your remix and save it for your lesson log, the final build (Lesson 12) submission loves a good journey. Stuck? Send a photo of your wiring via contact us, a real person will spot the crossed leg.

Next · Lesson 04: The Dimmer

A button knows two words: yes and no. Next lesson you meet a part that knows hundreds, the twisty knob, and use it to dim a light like a cinema.

All lessons