Project showcase
Fade LED without using PWM

Fade LED without using PWM © GPL3+

Demonstration of fading an LED that is not connected to a PWM pin.

  • 3,530 views
  • 0 comments
  • 3 respects

Components and supplies

Apps and online services

About this project

I wanted to be able to control the brightness of the LED on pin 13 knowing it is not a PWM connected pin.  On some of my boards, this LED is quite bright and I would rather have it fade instead of blink.

Code

FadeWithoutPWMArduino
Example of fading an LED without it being connected to a PWM pin
/* fade w/o pwm
 
Fades an LED that is not connected to a PWM pin
This was modified from the Blink without Delay code
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 */

// constants won't change. Used here to  set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMicros = 0;        // will store the last time the blink cycle occured

// the follow variables is a long because the time, measured in microseconds,
// will quickly become a bigger number than can be stored in an int.
long intervalLength = 500;     // interval at which to blink (microseconds)
long onInterval = 20;          // on or HIGH interval length
long currentInterval;          // length of the current interval
int  fadeLength = 200;
int  fadeCount = 0;
int  intervalDirection = 1;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{

  unsigned long currentMicros = micros();

  currentInterval = currentMicros - previousMicros;

  if (currentInterval <= onInterval) {
    ledState = HIGH;
  }
  else {
    ledState = LOW;
  }

  if(currentInterval > intervalLength) {
    // save the last time you blinked the LED 
    previousMicros = currentMicros;
    fadeCount++;
    if (fadeCount > fadeLength) {
      fadeCount = 0;
      onInterval += (intervalDirection * 20);
      if (onInterval <= 20 || onInterval >= intervalLength) {
        intervalDirection *= -1;  // change the direction of the fade ( positive - brighter / negative - dimmer)
      }
    }
  }

  // set the LED with the ledState of the variable:
  digitalWrite(ledPin, ledState);
}

Schematics

FadeWithoutPWM
Arduino board with built-in LED on pin 13.
FadeWithoutPWM.fzz

Comments

Add projectSign up / Login