Project 4: Switch a LED on when Switch is Closed (Button)

Created on: 2 August 2012
Updated on: 16 January 2023

Tutorial 10 – Pages

A switch (push button) is connected to the Arduino. When the switch is closed, the LED switches on, when the switch is opened, the LED switches off.

Similar to the DigitalReadSerial project (project 2), but instead of sending the state of the switch to the serial monitor, the state of the switch is displayed on an LED. The Arduino on-board LED is used so no external LED is necessary.

Parts Needed

Besides an Arduino Uno, USB cable, breadboard and wire links, you will need the following electronics components.

  • 1 × 10k resistor (1/4W, 5%)
  • 1 × push button or toggle switch (or just a wire link will do)

Project 4 Video: Close a Switch to Turn the Arduino LED On

The following video shows the Arduino Button project in action. When the external button is pushed, the Arduino Uno on-board LED switches on.

Arduino Website Page for the Button Tutorial

The Button project on the Arduino website shows the circuit diagram and breadboard circuit.

In the Arduino IDE

Use the top menu in the Arduino IDE to find the Button sketch as follows.

File → Examples → 2.Digital → Button

Build the Arduino Button Circuit

Connect the switch to the Arduino as done in project 2 (DigitalReadSerial), or see the connection diagram for the Button project on the Arduino website. Connect the switch or button to Arduino digital pin 2, as the circuit on the Arduino website page shows.

Programming and Operating the Circuit

In the Arduino IDE, select File → Examples → 2.Digital → Button to open the Button sketch. Verify and upload the sketch.

Closing the switch that is connected to Arduino pin 2 will switch the on-board "L" LED on the Arduino board on. Opening the switch will switch the LED off again.