I'm trying to build a dimming control for a 24V RGBW-LED strip using potentiometers as analog inputs. It's pretty similar to the analogWrite() example on the arduino website (arduino.cc/en/Reference/AnalogWrite), but with four inputs and outputs instead of one.

I'm using the Arduino IDE with the optiboot bootloader (optiboot_atmega328_pro_8MHz.hex) to compile and I flash the chips using avrdude from a Raspberry Pi's GPIO pins. The fuses are set to:

efuse: 0x05
hfuse: 0xd6
lfuse: 0xe2


Fritzing diagram of my circuit


int RGBW_LED[4] = {10, 9, 5, 6};
int RGBW_POT[4] = {A5, A4, A3, A2};
int RGBW_VAL[4] = {0, 0, 0, 0};

void setup()
  for (int i = 0; i < 4; ++i)
    pinMode(RGBW_LED[i], OUTPUT);
    pinMode(RGBW_POT[i], INPUT);

void loop()
  for (int i = 0; i < 4; ++i)
    RGBW_VAL[i] = 0;
    analogRead(RGBW_POT[i]);     // read once and discard
    for (int j = 0; j < 5; ++j)  // then calculate mean to avoid flickering
      RGBW_VAL[i] += analogRead(RGBW_POT[i]);
    RGBW_VAL[i] /= 20;  // divide by 5 for mean, then by 4 to scale to output range [0, 255]
    analogWrite(RGBW_LED[i], RGBW_VAL[i]);


  1. Random flickering on (different) outputs when turning potentiometers
  2. Controller randomly "crashes" and needs to be reset when simultaneously turning the pots for red/green and blue/white. This does not seem to happen in other combinations.
  3. The controller heats up (I think it's on the side of the output pins) the more potentiometers I turn "on". This also leads to the microcontroller crashing and needing to be reset.

What I have done so far

The setup seems to work fine as long as I am only turning one potentiometer. I have also more or less "solved" problem 1 by calculating the mean in code and writing that to the outputs pins. It seems more like a workaround to me, but at least it's functional. If there is a proper solution, I'm all ears.

Problem 2 seems to be related to the same PWM clocks running on pins 5/6 and 9/10 since simultaneous dimming on different pin combinations seems to work. Since I need four PWM controls for RGBW and the Atmega328p only has three independent PWM clocks I can't "cheat" this one though.

I have no idea what's going on with problem 3. I have tried using a separate power source (VCC from the Raspberry Pi) for the controller (thus dropping the voltage regulator and separating the circuits), that didn't change anything. I have tried removing the 24V source and the LED strip - same result. The only thing that did stop the pins from getting hot was removing the transistors, but I'm pretty sure that they are wired up correctly and since it also happens without the 24V source I can't imagine the current being out of spec.

I have tried modifying the code by adding delays, regulating the maximum analogWrite() value, and switching the power to the potentiometers before and after reading by connecting them to a digital output instead of VCC - all to no avail.

I've also tried using a different Atmega328p chip (got a few lying around) just in case, and it's definitely not because of a faulty chip.

Any help would be appreciated! Thank you so much!


I have included the circuit diagram below. Note that I omitted the voltage regulator and used separate 5V and 24V power sources. I have tested the circuit with both configurations and there is no difference. As stated above, I can even unplug the 24V source and the LED strip (cut the hand-drawn line in the diagram) and heating still occurs.

Circuit diagram

  • 1
    \$\begingroup\$ Hi! Sadly, it's very hard to guess the actual connection of the transistors from your wiring diagram. Would you mind exporting it as a proper schematic, so that we can unambigously read it? It's even more important with the Atmega, because, imagine, we usually don't know all the pin function - to - pin number mappings from heart, so your wiring diagram really makes it hard to understand what's happening here. A schematic makes things easier to understand for electronics experts! \$\endgroup\$ Jul 30, 2017 at 11:02
  • 1
    \$\begingroup\$ (and, most importantly, all the actual types of components are important here, because they determine the actual behaviour under load, so without a proper schematic with annotated components, this is simply not answerable in any meaningful way) \$\endgroup\$ Jul 30, 2017 at 11:05
  • \$\begingroup\$ Alright, sorry, I'm working on it :) \$\endgroup\$
    – mrbaozi
    Jul 30, 2017 at 11:08
  • 2
    \$\begingroup\$ You've connected those NPN BJTs as if they're MOSFETs. They're not. Add series base resistors!!!!! This is why your poor micro is overheating! \$\endgroup\$
    – brhans
    Jul 30, 2017 at 12:20
  • 1
    \$\begingroup\$ @brhans Do you want to know why? Because I was using MOSFETs initially!!! Thanks for pointing it out, everything is working fine now :) \$\endgroup\$
    – mrbaozi
    Jul 30, 2017 at 13:13

3 Answers 3


Random flickering on (different) outputs when turning potentiometers

You are continuously writing the PWM output registers. Don't do this.
Instead, add a low pass filter and read analogs at a regular basis. Every 10 milliseconds for example. Put this in the filter, and use the output of the filter for your PWM.

The best time to update the PWM values is on timer overflow (reload). See if you can get this interrupt.

Controller randomly "crashes" and needs to be reset when simultaneously turning the pots for red/green and blue/white. This does not seem to happen in other combinations.

The controller heats up (I think it's on the side of the output pins) the more potentiometers I turn "on". This also leads to the microcontroller crashing and needing to be reset.

Those are related.
You forgot the base resistors on the transistors, thus you are probably overloading the output pins.

Also, I see no capacitors at all. This can make significant switching noise from the PWM of the LED's to make the chip behave erratically. You've omitted this part, but are you sure this is all correct?

  • 1
    \$\begingroup\$ Oh crap, it was the transistors! Adding series resistors to the output pins fixes everything. I will look into implementing a better PWM control, but for now, the resistors were the real problem. Thank you so much! \$\endgroup\$
    – mrbaozi
    Jul 30, 2017 at 13:11
  1. If you are using a but as a switch make sure there is a base resistor.

  2. Make sure that the switch is adequate for the load.

  3. In your code make sure that you have sufficient time for the pwm to fully execte. For example insert a delay there. Or to use center aligned pwm. Or you run into glitches.

  4. Learn to draw a schematic.

  • \$\begingroup\$ I have added the circuit diagram. I have tried adding delays of various lengths (as much as 100ms) before and it did not seem to make a difference. Will try again. \$\endgroup\$
    – mrbaozi
    Jul 30, 2017 at 12:05

I afraid you have killed your micro. Atmega does not have to heat up at all. If it is it means that atmega is damaged. I even have one (I have killed it by too large current flow and it is the case here as well I think) which heats up uto about 90-100C then it stops, cools down a bit - starts again (continuing the original program), stops again ......

  • \$\begingroup\$ How do you know it is killed? What if he just draws too much current from the pins? As he has used BJT transistor in a wrong way. \$\endgroup\$ Aug 13, 2017 at 6:46
  • \$\begingroup\$ Not difficult to guess. To heat up -> more than double current has to flow through the uC max for the atmega is 200mA. So lets quess 500mA. This will rise temperature about 10 degrees. 5mA for atmega itself . 495mA for 4 outputs - 124mA / pin - three time over the maximum allowed. This is killing for the uC. It may work but is 100% damaged \$\endgroup\$ Aug 13, 2017 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.