Ready-to-teach lab
Arduino button and LED lab: digital input
In this Arduino button and LED lab a sketch reads a push button on pin 2 and lights an LED on pin 13 while it is held, which is the first time a program reacts to the world.
The lesson
Objective and parts
By the end, students can
- Read a digital input with digitalRead.
- Explain what INPUT_PULLUP does and why a pressed button reads LOW.
- Turn a reading into a decision, first in one line and then with if and else.
Parts, already on the bench
- An Arduino Uno
- One push button, from pin 2 to ground
- One LED with a 220 ohm resistor on pin 13
Nothing to hand out or collect: the circuit opens built and wired.
Steps
What students do
Each step is short enough to read out. Students work in the simulator beside this page.
- Open the lab and press Run. Hold the button down with the mouse: the LED lights while it is held.
- Open the code and find the line that reads the button, and the line that sets the LED from it.
- Swap HIGH and LOW in the digitalWrite line. Predict what the button does now, then run it.
- Put it back. Rewrite that line as an if and an else that do the same job.
- Now make the LED blink quickly while the button is held, and stay off otherwise.
- Harder: make one press turn the LED on and the next press turn it off.
Questions
Questions for students
Hand these out as they are. The expected answers are under the list, for you.
- The button is not pressed. What does digitalRead(2) return, and why?
- Why does pressing the button make the pin read LOW?
- What would the pin read with plain INPUT and no resistor at all?
- For the toggle, why is it not enough to check whether the button is down?
Answer key for the teacher
- HIGH. INPUT_PULLUP connects the pin to 5 V through a resistor inside the chip, so an open button reads HIGH.
- The button connects the pin straight to ground, which wins over the weak pull-up.
- Nothing reliable. The pin would float and pick up noise, reading HIGH or LOW at random.
- loop() runs thousands of times during one press. The sketch has to remember the last reading and act only when it changes from up to down.
Go further
For students who finish early
- Count the presses and print the count to the serial monitor.
- Add a second button that turns the LED off.
Your class
Give this lab to every student
Assign to my class puts this circuit on each student’s class page as a starter. They change their own copy and hand it in, and the work comes back to you in one list.
The class tools are free for 30 days for one class of up to 20 students, with no card. Opening and running the lab needs no account and stays free.
More labs
The other labs in the set
Ohm's lawCircuits. 40 min.Series and parallelCircuits. 45 min.Voltage dividerCircuits. 40 min.LED and resistorCircuits. 30 min.RC circuitElectronics. 50 min.Transistor switchElectronics. 45 min.Logic gatesElectronics. 40 min.Arduino blinkArduino. 35 min.Traffic lightArduino. 45 min.Ultrasonic distanceArduino. 45 min.Servo sweepArduino. 40 min.All labsThe whole set on one page.