Back to Step by Step Course by Robojax

Lesson 09-b: Using Switch and Push Buttons with Arduino

We learn how a how to turn ON a Light Emmiting Diode, LED using a push button switch. The LED will turn on when you push the switch. When you release the push button switch, the LED will stay ON for the time set in variable delayLED. This value is in milliseconds. It is set to 3000ms which is 3 seconds.

 * Arduino Step by Step Course by Robojax
 * Lesson 09-b. LED with Push button
 * This code is turning on an LED when a push button is pressed. When the push button is relased, the LED will turn OFF after delayLED time 
 * set in the code. 
 * Watch full details and demonstraiton:
 * Start a full Arduino Step By Step course:

 * What pars we need:
 * -Of course Arduino board (UNO , Nano, Mega or any)
 * -Push button switch 
 * -LED eitehr 5mm or 3mm
 * -200ohm to 1000 ohrm resistor for LED
 * -Some dupont wires to connec 
 * -If don't want to solder, then use breadboard
This video is part of Arduino Step by Step Course which starts here:

If you found this tutorial helpful, please support me so I can continue creating 
content like this. Make a donation using PayPal by credit card
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <>.
 * written by Ahmad Shamshiri on Jan 27, 2022
 * in Ajax, Ontario, Canada
const int pushButtonPin = 2;//define a pin number for push button
const int LEDPin = 3;//define a pin that is connected to LED
const int delayLED = 3000;//how long the LED should stay ON

void setup() {
  pinMode(pushButtonPin, INPUT_PULLUP);//define a pin for push button switch
  pinMode(LEDPin, OUTPUT);//define a pin for LED

  Serial.begin(9600);//initialize the serial monitor
  Serial.println("Robojax LED with push button");//print this text on Serial Monitor


void loop() {
  //if push button is pressed, we will read LOW value
  if( digitalRead(pushButtonPin) == LOW)
    digitalWrite(LEDPin, HIGH);//turn LEDpin HIGH to turn the LED ON
    Serial.println("LED ON");
    delay(delayLED);//keep it on for the delayLED time

    digitalWrite(LEDPin, LOW);//else, turn the LED OFF
// Watch full details and demonstraiton:

The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side