All Articles

Interruptible LED fade

Assignment: Create a fading LED attached to a microcontroller. Your LED should be interruptible by either a pushbutton, analog input, or serial input. Your fade speed and fade curve are yours to determine.

Inspired by the camera flash light, I made a digital simulation of the capacitor discharge fade curve. The following image shows the formula and graph. RC is the time constant of the discharge, and after 3RC time it almost discharged completely. By tweeking the value of RC, I can get different discharge curve and hopeful find something similar to a camera flash light.

Capacitor discharge graph

Image from http://hyperphysics.phy-astr.gsu.edu/hbase/electric/capdis.html

I can’t find a breadboard-friendly push button on floor, so I used the button on a rotery encoder instead.

Circuit on breadboard

Here is a video showing how it behaves.

Code

It first counts the elapsed time by second and calculates the voltage by the previous formula.

/*
   Flash fade
   Simulate a capacitor discharge circuit
   in a camera flash light

   Fade curve reference:
   http://hyperphysics.phy-astr.gsu.edu/hbase/electric/capdis.html
*/

const int ledPin = 2;
const int buttonPin = 21;
const float timeConstant = 0.2;

unsigned long lastFlashStart;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    lastFlashStart = millis();
  }

  float elapsedTime = float(millis() - lastFlashStart) / 1000;

  // calculate the brightness level with the following formula:
  // Vc = V0 * e ^ (-t / RC)
  float level = exp(-elapsedTime / timeConstant) * 255;

  Serial.println(level);
  analogWrite(ledPin, level);
  delay(10);
}

Flashlight fade curve

Lighting Moment

  • January 29, 2022
  • 1:55 PM
  • Brooklyn, NY

The storm just passed and it was snowing outside which reflected tremendous light to the room. It created a very bright white reflection at the left side of the kettle, the soda, and the pot. And on the right side of these objects, two lines of warm color room light were shown. With a vertical curve on the kettle, it had a more organic shape of reflection while with the pot cooker it was just straight lines.

Kitchen