jordin

My first original project! An LED Light Scale 💡

January 04, 2022

Initially, this blog post was going to be pretty dull. I was going to describe some of the things I've been learning: the difference between energy and current, how pulse-width modulation works, the nuances behind how a multimeter functions, and many more juicy tidbits that are SO fun to write about, and even MORE fun to read about.

jk that definitely would've been super boring - I made a timelapse of me making something instead!

This is the first idea that I've come up with on my own. I'm definitely not the first person to do this, and I'm sure it's nothing to write home about if you've passed the "light up an LED" stage of learning, but I'm proud of my little original creation (even if it's in the same way that a toddler is proud of the crappy sandcastle he just made).

In summary: Twist the little knob --> light fades back and forth

I think this concept could easily be expanded to create..

  • a music synthesizer
  • cool lighting patterns for a keyboard or a desk
  • LED-based representations of literally kind of scale (health in a video game, volume on a computer, etc)

Even though I originally built this because I wanted to learn how to use potentiometers, it's effortless (literally < 2 mins of effort) to make the lights fade back and forth through other types of input as well (e.g. programmatic):

It was really fun to make a timelapse, even if I'm painfully aware of the rough patches (a new years resolution of mine is to just ship things and not worry so much about sweating the small stuff). A few lessons learned for if I want to do something like this again:

  1. I'll need to spend some more time editing to remove the dull parts, like parts where I'm JUST coding for multiple minutes or nothing otherwise significant is happening.
  2. I need to rig a way for my phone to film in landscape instead of portrait mode. I used to have a little tripod built for cell phones that would've been perfect for this, but I have no idea what happened to it (probably lost in our most recent move). I ended up propping my phone against a little wireless charger that I usually have sitting on my desk, and the angle was just barely good enough for me to justify filming myself.
  3. I screwed up the settings for the screen recording software I used to record myself coding, and it only recorded like a quarter of my screen. Easy fix, rookie mistake.
  4. Find some less cheesy timelapse music.

Here is the insanely messy whiteboarding I did to figure out how I wanted to jump through the hoops involved in converting from the voltage input value (a 10-bit number) to the output values (an 8-bit number) on a per-LED basis:

Messy whiteboard for figuring out conversion math lol ignore my manic unrelated notes scattered throughout + the scribbles from my kids in the bottom-right corner

The code:

#define LED_1 11
#define LED_2 10
#define LED_3 9
#define LED_4 6

#define POTENTIOMETER_INPUT_PIN A0

const int MAX_POTENTIOMETER_VALUE = pow(2, 10) - 1;
const int MAX_ANALOG_WRITE_VALUE = pow(2, 8) - 1;

void setup() {
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(POTENTIOMETER_INPUT_PIN, INPUT);
}

void loop() {
  float input = analogRead(POTENTIOMETER_INPUT_PIN);
  float inputStandardized = input / MAX_POTENTIOMETER_VALUE;

  handleLEDBrightness(inputStandardized, LED_1, 0, 0.4);
  handleLEDBrightness(inputStandardized, LED_2, .4, 0.2);
  handleLEDBrightness(inputStandardized, LED_3, .6, 0.2);
  handleLEDBrightness(inputStandardized, LED_4, 1, 0.4);
}

void handleLEDBrightness(float inputValue, int outputPin, float peakBrightnessValue, float deviationRange) {
  float peakDelta = abs(peakBrightnessValue - inputValue);

  float outputValueRaw = 1 - peakDelta / deviationRange;
  float outputValue = max(outputValueRaw, 0) * MAX_ANALOG_WRITE_VALUE;

  analogWrite(outputPin, outputValue);
}

Previous I built an LED binary counterNext Using VSCode for Arduino Development