Arduino: Fade and pulse a LED with just using a DigitalPort

2009 May 17
by tobi

This is a nice experiment I did to fade and pulse a LED by just using a digital port. Digital? On/Off, 1/0 – how can this work? Well it does, check this out:

YouTube Preview Image

The key is, I’m switching the LED on & off very fast which appears the human eye as it’s on all the time (similar to a LED Matrix). Now, I change the time period between switching the LED on and off. Is the off period time longer, the LED lights low, is the off period time short, the LED lights high. Fading the period time, makes the LED pulse… nice!

Check this (still quite ugly) code:

int ledPin = 13;      // LED connected to digital pin 13
int value = LOW;    // previous value of the LED
long cnt = 0;         // will store last time LED was updated
long low = 0;         // interval at which to blink (milliseconds)
long high = 1000; // interval at which to blink (milliseconds)
int op = 3;
long a = 0;

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

void loop()
{
  a += op;
  blinkl( a+30, 10 );
  if( a > 200 || a < 0 ) op *= -1;
}

void blinkl(long low, long high )
{
  int c = 5;
  while ( c > 0 ) {
   blink( low, high );
   c-=1;
 }
}

void blink( long low, long high )
{
  long period = 4000;
  long pt = period * high / (low + high );
  int value = LOW;
  digitalWrite(ledPin, value);

  while( period > 0 ) {
    if (period < pt && value == LOW ) {
      value = HIGH;
      digitalWrite(ledPin, value);
    }
    period -= 1;
  }
}
7 Responses leave one →
  1. 2009 November 5

    What line would I modify to make the LED completely turn off then fade on? Or is that even possible w/ the digital channels? Thanks though this code is the shizzle fo rizzle…

  2. 2009 November 5
    tobi permalink

    well sure! I admit, this code was first version and is kind of crap, I should come up with an updated one. But to solve your problem turing off the LED, just change the low/high interval ratio to a maximum of low. Try playing around with the values in line 17, e.g. change them into: blinkl( a+100, 200 – a );

  3. 2009 December 31
    BiLL permalink

    Nice work m8, im try to do samething for a project for my university. i work with pic 18F1320 ( c programming ) . Could u want to send me a code in this pic or something like that in order to fix it to work . Im already thank u for this idea a read.

  4. 2010 February 12
    jason permalink

    What about having one LED fade into another? For example, a red LED fading into a blue LED?

  5. 2010 March 10

    Excellent solution for the digital write. I’ve been trying to find a good solution for button input to LED lights using the analogwrite, but the delay function would always null my button input until the loop was complete. This works very well.
    Thanks for giving me new ideas.

  6. 2010 July 24

    Simple example, using a shift register – makes all 8 LEDs pulsate together (from complete brightness, to nothing):

    int data = 2;
    int clock = 3;
    int latch = 4;

    boolean Pins[8];

    int Count = 0;

    int Brightness = 0;
    int BrightnessInc = 1;

    void setup() {
    pinMode(data, OUTPUT);
    pinMode(clock, OUTPUT);
    pinMode(latch, OUTPUT);
    }

    void loop() {
    digitalWrite(latch, 0);
    digitalWrite(clock, 0);
    digitalWrite(data, 0);

    for(int i = 0;i < 8;i++) {
    if(Count = 50) {
    Count = 0;
    Brightness += BrightnessInc;
    }

    if(Brightness > 50) {
    BrightnessInc = -1;
    }else
    if(Brightness < 0) {
    BrightnessInc = 1;
    }

    for(int i = 0;i < 8;i++) {
    digitalWrite(clock, 0);

    digitalWrite(data, Pins[i]);

    digitalWrite(clock, 1);
    digitalWrite(data, 0);
    }

    digitalWrite(clock, 0);
    digitalWrite(latch, 1);
    }

  7. 2010 July 24

    Comment system totally butchered my code. Is using htmlspecialchars really that hard? Replace ~ with less than sign.

    int data = 2;
    int clock = 3;
    int latch = 4;

    boolean Pins[8];

    int Count = 0;

    int Brightness = 0;
    int BrightnessInc = 1;

    void setup() {
    pinMode(data, OUTPUT);
    pinMode(clock, OUTPUT);
    pinMode(latch, OUTPUT);
    }

    void loop() {
    digitalWrite(latch, 0);
    digitalWrite(clock, 0);
    digitalWrite(data, 0);

    for(int i = 0;i ~ 8;i++) {
    if(Count ~ Brightness) {
    Pins[i] = 1;
    }else{
    Pins[i] = 0;
    }
    }

    Count++;

    if(Count >= 50) {
    Count = 0;
    Brightness += BrightnessInc;
    }

    if(Brightness > 50) {
    BrightnessInc = -1;
    }else
    if(Brightness ~ 0) {
    BrightnessInc = 1;
    }

    for(int i = 0;i ~ 8;i++) {
    digitalWrite(clock, 0);

    digitalWrite(data, Pins[i]);

    digitalWrite(clock, 1);
    digitalWrite(data, 0);
    }

    digitalWrite(clock, 0);
    digitalWrite(latch, 1);
    }

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS