How to add LED brightness control to this code

Alright well that limits me to one LED on each then, not two.

Might have to go the WS2811/2812 route then. It'll make for a simpler PCB as well

Alright I'm driving the 4 LED's through 3 2N3904 transistors, one for each color. It works great with external power. I think I might just forego the brightness control honestly, it looks to be a nightmare to implement with fading - at least with the RGB color cycling code I'm coming across

Two (or more) RGB LEDs is ok, as long as total Arduino pin current is no more than 20mA.

If you want two RGB LEDs, then calculate the six current limiting resistors for 10mA LED current.
20mA LED current is the absolute max LED curent, not a requirement.
Most modern LEDs are bright enough with 1-10mA.
Leo..

Sounds like an opportunity to write your own. The 2N3904 can safely drive many more than one LED. Not as many as a power MOSFET. I assume you have limiting resistors in the collector circuit. What is the LED supply voltage? Even with simple circuits, you can often maximize efficiency by putting LEDs in series so the resistor doesn't have to dissipate so much energy. With a common anode or collector package though, you can't put them in series. So a low supply voltage is the best choice.

Ok I have three 2N3904, one controlling each color, connected to the Arduino via 1k resistors. Works great.

I also just figured out the brightness control with the FastLED library, found the solution here: https://www.reddit.com/r/FastLED/comments/awisfs/adjusting_brightness_when_using_analog_functions/

Here's the completed code which functions 100% as I want it to:

#include <FastLED.h>

#define REDPIN 3                // Red LED pin connected to digital output 6
#define GREENPIN 5              // Green LED pin connected to digital output 5
#define BLUEPIN 6               // Blue LED pin connected to digital output 3

const int brtPin = A0;          // Brightness pot connected to A0
const int spdPin = A1;          // Speed pot hooked to A1
const int lghtPot = A2;         // Light setting pot hooked to A2
const int lghtPin = A3;         // Light sensor hooked to A3

const int numReadings = 150;    // Number of readings to average
int readings[numReadings];      // The readings from the analog input
int readIndex = 0;              // The index of the current reading
unsigned long total = 0;        // The running total
int average = 0;                // The average

int spdOutVal = 0;
int spdVal = 0;

int lghtPotVal = 0;
int lightPot = 0;

int lghtOutVal = 0;
int lghtVal = 0;

int brtPotVal = 0;
int brtVal = 0;
int brtOutVal = 0;

// This function takes the incoming RGB values and outputs the values
// on three analog PWM output pins to the r, g, and b values respectively.
void showAnalogRGB(const CRGB& rgb)
{
  analogWrite(REDPIN, scale8(rgb.r, brtOutVal));
  analogWrite(GREENPIN, scale8(rgb.g, brtOutVal));
  analogWrite(BLUEPIN, scale8(rgb.b, brtOutVal));
}
void setup() {
}

void loop() {
  brtVal = analogRead(brtPotVal);               // Read the value of the brightness pot
  brtOutVal = map(brtVal, 0, 1023, 255, 10);    // Map the pot value to max PWM
  lghtPotVal = analogRead(lghtPot);             // Read the value of the light pot
  lghtVal = analogRead(lghtPin);                // Read the value of the light sensor
  spdOutVal = analogRead(spdPin);
  spdVal = map(spdOutVal, 0, 1023, 10, 200);

  // Averaging functions
  total = total - readings[readIndex];          // Subtract the last reading
  readings[readIndex] = analogRead(lghtVal);    // Read from the light sensor
  total = total + readings[readIndex];          // Add the reading to the total
  readIndex = readIndex + 1;                    // Advance to the next position in the array
  if (readIndex >= numReadings)  {              // If we're at the end of the array
    readIndex = 0;                              // Wrap around to the beginning
  }
  average = total / numReadings;                // Calculate the average
  if (average >= lghtPotVal) {                  // If the pot value is lower than the light sensor reading
    static uint8_t hue;
    hue = hue + 1;
    showAnalogRGB(CHSV(hue, 255, 255));         // Use FastLED automatic HSV->RGB conversion
    delay(spdVal);
   }
    else{                                       // Otherwise turn LED's Off
      analogWrite(REDPIN, 0);
      analogWrite(BLUEPIN, 0);
      analogWrite(GREENPIN, 0);
  }
}

That's good. I do wonder, if you like to use the FastLed library to control the LEDs, why use low level code like

      analogWrite(REDPIN, 0);
      analogWrite(BLUEPIN, 0);
      analogWrite(GREENPIN, 0);

?
Isn't there some library function that would do the same thing, but more concisely?

What resistors are in the collector circuit?

Possibly 🤷

220

1 Like

Figured I'd update this thread with the final outcome of the project: