Beginner
...


Learning Objectives

  • Emphasis: Equal
  • Hardware: Multiple LEDs, buttons
  • Software: Digital input, conditional statements

Materials

  • Arduino Uno
  • Breadboard and wires
  • 2 different-colored LEDs
  • 2 push-button switches
  • 2-330 ohm resistors
  • 2-10 kohm resistors
Reference Files

Breadboard Layout

  1. LEDs: This project is going to require 4 pins on the Arduino - 2 for the buttons and 2 for the LEDs. For the LEDs, we're going to use digital pins 10-11. Connect a circuit from each pin to a 330 ohm resistor, the long leg of the LED, and back to ground.
  2. Buttons: We're going to provide a constant power supply to one side of the buttons and then check the other side to see whether it's getting voltage. If the button is pressed down, the circuit will be completed and the digital pin will read HIGH. If the button is up, the circuit will be broken, so the digital pin will read LOW. First, connect the 5V power supply to one side of each button. Connect the other side to a 10k resistor and then back to ground.
  3. Button input: To read input from the buttons, connect a wire from digital pins 8 and 9 to the middle of your button circuit, in between the buttons and the resistors. This will monitor whether or not the circuit is completed.
  4. Button troubleshooting: A common mistake with buttons is to accidentally short the two sides together so it looks like it's always pressed down. To check if yours is wired correctly, you can put an LED after the button and see whether it lights up only when the button is pressed or always. If it's always lit, rotate your button 90 degrees and try again!
  5. You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
...

...

Software

  1. Getting started: Open the Arduino program (it should open automatically once your Arduino is plugged in) and create a new sketch. If you need reference, check out some of the example programs, especially Basics>>Blink.
  2. Initialize variables: Declare ints to store your button pins 1-2 and your leds 1-2.
  3. Setup: In your first required method, set your LEDs to be output and your buttons to be input. Also, initalize the Serial monitor with Serial.begin(9600).
  4. Read button input: What we're going to be doing is checking to see if a particular button is pressed and, if it is, turn on the corresponding light and print the button to the Serial monitor. First, we need to create variables to store the state of the buttons. Create your variables with int buttonStateN = digitalRead(N), where N is buttons 1/2.
  5. Turn on LEDs: We're going to use an if-else block to turn on our LEDs. Each if statement will check if a particular button is on. If it is, it will turn the corresponding light on and print the button to the Serial monitor. It will also make sure that the other lights are off. To do this, write:

    if(buttonState1 == HIGH) {

    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);

    }

    Make sure you also include the default case with your final else that will turn all the LEDs off in case no buttons are pressed. If you're confused, check out the code to the left.
  6. Write to Serial monitor: In every if/else-if block, add another statement that will write the button number to the Serial monitor. This will look like Serial.println("Button 1 pressed"). These statements will let you know who pressed their button first.
  7. Debounce the buttons: The Arduino will check the buttons multiple times a second, which means that even if you press down for a very short amount of time, the Serial monitor will think you've pressed down 20 or 30 times. To prevent this, we're going to do something called debouncing the button. Basically, we add a delay statement in our if-else loops so it only reads one press instead of multiple. Add delay(200) in each of your loop blocks that includes a print statement.
  8. Running the program: Once you upload your program to the Arduino, open the Serial monitor and try pressing each button to make sure it lights up the LED and prints to the screen. If everything works, you're done! Grab a couple friends and get ready for a fierce Jeopardy competition!
  9. Experiment: What if you wanted to play with two friends or four? Can you add a buzzer when a button is pressed? What about a button for the referee to tell when the timer is started/stopped?
Reference Code

The Finished Product




<-- Previous: Blinking LED Next: Dim the Lights -->