• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
DarkBlueBit.com

DarkBlueBit.com

DIY projects with Arduino and Raspberry Pi

  • Arduino
  • Raspberry Pi
  • About
    • Contact
    • About Me
    • Amazon Affiliate Disclosure
    • Privacy Policy
You are here: Home / Arduino / Pushbutton and Fading LED

Pushbutton and Fading LED

This is a simple demonstration of using an Arduino input (external pushbutton) to control an analog PWM output (LED).

Function: each time the button is pressed, the LED slowly fades in/out (depending on its previous state).

https://youtu.be/8ua3U3xwqJA

Watch video directly on YouTube

youtube-subscribe-button

Circuit

Required hardware:

  • Pushbutton + 10k resistor
  • LED + 220 ohm resistor

pushbutton-LED_bb

The pushbutton is connected to the Arduino pin 2 used as an input. When the button is not pressed, the pin 2 is connected to the ground (GND pin) through the pull-down 10k resistor which results in a LOW value reading. When the button is pressed, the pin 2 gets connected directly to 5 V which gives us a HIGH value reading. For a detailed schematic of the button circuit visit Arduino.cc Button example.

The external LED is connected to pin 9 as an output in series with 220 ohm resistor (to limit a current flowing through the LED). One end of the resistor is connected to the Arduino pin 9 and the other end to LED anode (long leg). The LED cathode (short leg) is connected to the Arduino GND pin.

Analog output (PWM)

Instead of writing digital values (LOW/HIGH) we will control the output using PWM – Pulse Width Modulation. This is a technique of simulating voltage between HIGH (5 V) and LOW (0 V) by quickly changing them (creating pulses with a certain width). For an LED these changes are invisible to human eyes, so the brightness seems to be steady as if we provided a stable voltage between 0 and 5 V.

A function analogWrite() is used to control PWM. It takes a value in the range between 0 – 255, where 255 is equal to HIGH voltage level (5 V) and therefore a maximum LED brightness.

Code

/*
 Project name: FadingLED

 Description: A demonstration of using Arduino input 
(external pushbutton on pin 2) to control an output
(LED on pin 9).  Created by Pavel in 2016 <https://darkbluebit.com>    */ // constants won't change their value

// the number of the pushbutton pin const int buttonPin = 2;    
// the number of the LED pin const int ledPin = 9;       
// the speed of LED fading (higher value = slower) const int fadingDelay = 50;  // variables will change

// variable for reading the pushbutton status int buttonState = 0;        
// determines whether the LED needs to be turned on or off boolean fadingState = false; void setup() {   // initialize the LED pin as an output:   pinMode(ledPin, OUTPUT);   // initialize the pushbutton pin as an input:   pinMode(buttonPin, INPUT); } void loop() {   // read the state of the pushbutton value:   buttonState = digitalRead(buttonPin);   // check whether the button was pressed   if (buttonState == HIGH) {     // check the current LED status (on/off)     if (fadingState == false) {       // turn on the LED       for (int i = 0; i <= 255; i += 5) {         analogWrite(ledPin, i);         delay(fadingDelay);       }     } else {       // turn off the LED       for (int i = 255; i >= 0; i -= 5) {         analogWrite(ledPin, i);         delay(fadingDelay);       }           }
// save the current LED state (on/off)     fadingState = !fadingState;   } }
Category: Arduino

Disclosure

Articles on this website contain paid links leading to Amazon.com.

As an Amazon Associate I earn from qualifying purchases.

Read more

Reader Interactions

Comments

  1. jairaj

    March 27, 2018 at 21:37

    marvellous program, nicely explained. I just wanted to know if more than one LED can be controlled with push buttons ? like three button for three led , individually controlled?
    another point is how do you make the led stop at a desired brightness? like press once for 15% brightness increase, push again for 30% and so on …

    Reply
  2. chacalpowers

    September 14, 2018 at 03:59

    Interessing your question about 3 leds. I modified this code for PWM use in PIC16F684 and works great. Thanks to author.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

3 − three =

About

About Me

Contact

Privacy Policy

Amazon Affiliate Disclosure

Social Profiles

  • Facebook
  • Twitter
  • YouTube

Copyright © 2023 · DarkBlueBit.com · All Rights Reserved · A redistribution or reproduction of the contents in any form is prohibited except with our explicit written permission.