Check the 34 other Arduino tutorials

I think it is important that Bas on Tech can be used by everyone free of charge.

Help me ensure the future of Bas on Tech. Your donation will be used for hosting, videos and maintenance, among other things.

Thank you in advance!

#5 · Control LED with pushbutton on Arduino


Tutorial goals

  • Knowing how to use a pull-up / pull-down resistor
  • Inverting a switch
  • Knowing how to use a push button

Components needed

  • 1× Arduino
  • 1× Breadboard
  • 3× Jumper wire (male-male)
  • 1× Min 10K Ohm resistor
  • 1× Push button / Tactile switch

Introduction

In this Arduino tutorial for beginners I teach you how to read a push button the right way. For example, did you know that it is useful to use a pull-down resistor? New? Then read on 😃

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

Push button

A push button has different names such as push button, switch and tactile switch. They come in all shapes and sizes. We are going to use a 4-pin push button .

push button diagram
schematic representation of a push button

On the right you see a schematic representation of the push button. Pin 1 and 2 are connected to each other, just like pins 3 and 4. As soon as we press the button, all pins are connected to each other.

Normally open / normally closed.

This push button is a so-called Normally Open button. This is often abbreviated to NO. This means that the button is open by default and therefore does not connect the pins. A Normally Closed switch is the mirror image. Here all pins are connected to each other. This connection is not broken until the button is pressed.

The circuit

4-pin push button connected to the Arduino
4-pin push button connected to the Arduino

Press the push button exactly over the center gutter in the breadboard. Connect the following jumper wires

  • The 5V on the Arduino -> the pin lower left of the push button
  • The 12 on the Arduino -> the pin top right of the push button

Pulldown resistor

In principle, the push button should now be able to do all its work. As soon as we press the button, the 5V is connected to pin 12 and the Arduino sees this as HIGH. The problem now is that you have a "floating" state if you don't press the button. It may be that the push button does not block 100% current. This small current can prevent the Arduino from reading the state of the push button properly.

Therefore, in this circuit we use a pull-down resistor. This resistor ensures that the state of the push button is really 0 Volt when the button is not pressed. We put the resistor between the pin lower right of the push button and the GND of the Arduino. The resistor now "pulls" the current to the GND.

Arduino Code

We start the code by defining three variables:

1 int switchPin = 12;
2 int ledPin = LED_BUILTIN;
3 int switchState = LOW;
  • switchPin is the pin to which we connected the push button
  • We set ledPin to LED_BUILTIN with which we automatically get the correct LED pin back
  • In switchState we store what the current state of the push button is

setup()

1 void setup() {
2     Serial.begin(9600);
3     pinMode(ledPin, OUTPUT);
4     pinMode(switchPin, INPUT);
5 }

First, we initialize the serial monitor. Then we set the ledPin as the output and the switchPin as the input.

loop()

1 // repeat infinitely
2 void loop() {
3     switchState = digitalRead(switchPin);
4 
5     Serial.println(switchState);
6 
7     if (switchState == HIGH) {
8         digitalWrite(ledPin, HIGH);
9     } else {
10         digitalWrite(ledPin, LOW);
11     }    
12 }

The loop() function consists of 3 parts:

  • Read out push button
  • Print to serial monitor
  • Switch LED on/off

Explanation code

First we read the state of the push button with digialRead. After this use Serial.println to send the read value to the serial monitor.

In the if statement we determine whether the state of the push button is HIGH or LOW. Once it is HIGH, we turn on the built-in LED and vice versa.

Upload code to Arduino

Our program is now ready to be sent to the Arduino . Then open the serial monitor:

Tools ▸ Serial Monitor

You should see the number change from 0 to 1 as soon as you press the button and the built-in LED turns on or off.

#5 · Control LED with pushbutton on Arduino schakelschema

Bas van Dijk

About Bas on Tech


My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.

Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.

With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.