12v RGB + Nano: LED fade effect help!

Hello! This is my first foray into LEDs so I need some assistance. I've scoured the Internet for information and I've hit a roadblock and hoping I can get some clarity. I'm making a wand prop and my goal is to have the dome at the top glow from red-->yellow-->white-->-->yellow-->red as if it's pulsing.

My Setup
I have a 12V RGB LED and I'm using an Arduino Nano. Pins D6, D10, D11 are my output to the respective MOSFETs. I'm currently doing tests with my Arduino USB to the computer. It is my understanding I should be using a buck converter for the actual project since I'll be using my 12V power supply to power my LEDs as well as my Arduino; however, the tests I am doing have the Arduino powered by USB and NOT by the 12v power supply (but the LEDs are powered by the 12V).

Initially, I tested a basic code setColor code between red, green, and blue with a delay. This worked fine so I verified that each individual pin seemed to work correctly. Then I started experimenting with different Fade codes and things went awry. I tried probably a half dozen different codes I found and the LEDs basically would just fade to random colors and would not do what I wanted. This is what I had the most success with:

int redPin = 6;
int grnPin = 10;
int bluPin =11;

void (setup){
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);

void setRGB (int red, int green, int blue){
analogWrite (redPin, red);
analogWrite (grnPin, green);
analogWrite(bluPin, blue);
}

void loop (){
int red = 255;
int green = 0;
int blue = 0;

for (int i = 0; i <= 255; i ++){
green = i;
setRGB (red, green, blue);
delay(20);
}

for (int i = 0; i<= 255; i++){
blue = i;
setRGB(red,green,blue);
delay(20);
}

for (int i = 0; i <=255; i++){
blue = 255 - i;
setRGB(red, green, blue);
delay (20);
}

for (int i = 0; i<=255; i++){
green = 255 - i;
setRGB(red, green, blue);
delay(20);
}
}

What happens is it starts red, then passes through yellow and becomes green, then becomes blue, and then it just starts glowing randomly colors. The only thing I can think of is the for (int i) phrase is doing things to the other variables that I don't understand. I also have some digital LEDs in the mail--I'm curious if those would be easier to manage than the RGBs here.

Please help!

Thank you!

If you mean the WS28xx type LEDs, I think most people find them more intuitive to use, yes. But their usage is just different from discrete LEDs and once you get the hang of the latter, it's not difficult either.

To troubleshoot your code, try inserting some Serial.print's so you can see what's happening in each of your for-loops to each of the red, green and blue variables. This will help you to locate the source of your problem.

I'd exclude a hardware problem first.

In general, I'd start with first verifying the hardware works as intended. You haven't provided a schematic for your setup and I'd recommend doing so, as well as a photograph that clearly shows the connections you've made and allows to verify if the schematic as drawn is actually representative of the setup as built.

Then, you could make a test program that pulses each separate LED channel to verify they all respond the way you envision, followed by a test program that makes all LEDs fade in and out at the same time (e.g. using a single brightness variable for all 3 LEDs) to verify there isn't something funny going on with one channel influencing another.

There are many ways to do this and it depends on the components you ultimately choose. In general, avoid stepping up and down voltage unnecessarily. If you're feeding your project with e.g. 5V, don't step it up to 12V and then have the Nano board regulate it down again to 5V for the microcontroller. It's not harmful per se, but inefficient.
However, since red, green and blue LEDs work fine at 5V to begin with, I'd suggest using 5V for the entire project and keep things simple. You don't seem to be married to the 12V RGB LED anyway, so you might as well choose a light source that fits well within the context. Which emphasizes what I said before: design the hardware first, then start solving software problems.

Good luck; these little LED projects tend to be lots of fun and yours is a great project to start with.

Thank you for the quick reply! I've attached a schematic (apologies for my very poor and limited Paint skills) and can take a photograph of my real-life person when I'm home later. I do have 5V RGB LEDs that I can hook up so I won't be experiencing the voltage issue you brought up. My external power supply are just batteries wired in series since I'm going to be storing them inside of the shaft of the wand. I don't have breadboards with those side channels so I essentially made my own mini ones on the small breadboards I have. Also, it's not clear in this little schematic but I'm using the logic level MOSFETS.

https://ibb.co/LhYMQgC

Thank you for the Serial.print suggestion. I saw those in a few codes and was very confused on what it was for and how to utilize it. I've seen Serial.begin(9600). That takes care of it? I know where to find that spot in the program itself.

That is a good suggestion on the pulsing each individually. I think my first question is would be is the fading code I have in there seem correct. I am very confused how to read/interpret what the if (i=0; i>=255; i++){ and the following information even means,

This starts the Serial interface.

To output information to it, you need to insert lines like:

Serial.print("Red: ");
Serial.print(red);
Serial.print("Green: ");
Serial.print(green);
Serial.print("Blue: ");
Serial.print(blue);
Serial.print("\n");

You could put the above code block before each "setRGB" line in your code to output the values of the red, green and blue variables.
You could precede it with an identifier that tells you in which for-loop you are:

Serial.print("Loop #1:");

Paint is fine, but please don't confuse a wiring diagram with a schematic. What you've posted is a wiring diagram, and it's a little less easy to read than a schematic.
In a schematic, always indicate the component numbers; e.g. this would tell us which specific MOSFET type you're using. Others can then infer from this what its capabilities and limitations are.

There are many examples and tutorials on C++ code. Don't hesitate to use Google! In fact, it's best to try that before asking.
https://www.google.com/search?q=c%2B%2B+how+do+for-loops+work
Also, I'd encourage you to write code you actually understand. It's fine/great to take inspiration from existing examples, but only work them into your own code/project if you really understand what's going on.

You may benefit from some kind of beginner's book on C++ coding and/or Arduino projects. Sadly, I'm not at home in those; perhaps others can recommend you some resources.

Thanks for your help so far! So I've tinkered a bit, here's my code now for just testing a fade to yellow:

int RedPin = 6;
int GrnPin = 10;
int BluPin = 11;

int red= 255;
int blue = 0;
int green = 0;

void setup(){
pinMode(RedPin, OUTPUT);
pinMode(GrnPin, OUTPUT);
pinMode(BluPin,OUTPUT);
Serial.begin(9600);
}

void loop(){
analogWrite(RedPin, red);
analogWrite(GrnPin, green);
analogWrite(BluPin, blue);

for (int i = 0; i<255; i++){
green = i;
Serial.print(red);
Serial.print("Red: ");
Serial.print(green);
Serial.print("Green: ");
Serial.print(blue);
Serial.print("Blue: ");
Serial.print("\n");
}
 
for (int i = 255; i>0; i--){
green = i;
Serial.print(red);
Serial.print("Red: ");
Serial.print(green);
Serial.print("Green: ");
Serial.print(blue);
Serial.print("Blue: ");
Serial.print("\n");
}
delay(1000);
}

In the serial print, it's showing the increase and decrease in green correctly, but the LED does not change from red. I tested change int red, int blue, and int green at the top of the code to 255 and the colors change correctly on the LED. If I set them all to zero and change red = i, green = i, and blue = i, nothing happens.

If the hardware was not correct, changing int green = 255 would not work, but it does. But for some reason the for codes are changing the value in the serial monitor, but that's not translating to the actual LED.

Here is an example of my serial monitor, but the LED stays red.

If I change int green = 255 then red=i below in the for statement, the LED stays green.

My messy setup . . .

Indeed. In neither of the for-loops, you call the function that does the analogWrite bit.

They are the same, but the setup is a little more detailed.

Rather than trying to move and mix colors all at one time, start with the RGBLED OFF. Then move toward RED. Then stop.

int redPin = 6;
int grnPin = 10;
int bluPin = 11;

void setRGB (int red, int green, int blue) {
  analogWrite (redPin, red);
  analogWrite (grnPin, green);
  analogWrite(bluPin, blue);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);

  int red = 0;
  int green = 0;
  int blue = 0;

  for (int i = 0; i < 256; i ++) { // count from 0 to 255
    setRGB (i, green, blue); // change red
    delay(20); // time to see the color
  }
}

void loop() {} // leave blank

Then, try starting with red, and moving to yellow...

int redPin = 6;
int grnPin = 10;
int bluPin = 11;

void setRGB (int red, int green, int blue) {
  analogWrite (redPin, red);
  analogWrite (grnPin, green);
  analogWrite(bluPin, blue);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);

  int red = 255;
  int green = 0;
  int blue = 0;

  for (int i = 0; i < 256; i ++) { // count from 0 to 255
    setRGB (red, i, blue); // change green to make yellow
    delay(20);
  }
}

void loop() {} // leave blank

Then start with yellow and move to white...

int redPin = 6;
int grnPin = 10;
int bluPin = 11;

void setRGB (int red, int green, int blue) {
  analogWrite (redPin, red);
  analogWrite (grnPin, green);
  analogWrite(bluPin, blue);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);

  int red = 255;
  int green = 255;
  int blue = 0;

  for (int i = 0; i < 256; i ++) { // count from 0 to 255
    setRGB (red, green, i); // change blue to make white
    delay(20);
  }
}

void loop() {} // leave blank

Try re-writing your first sketch and posting it in working order...

Thank you everyone for your input! I've managed to get it to do what I'd like with everyone's guidance.

I do have a follow-up question. When the LED scales up to 255 green, it ends being more green than red when both are 255. the color is yellow when green is about 70. I'm wondering if this is just a consequence of the type of LEDs I'm using?

1 Like

Yes, that's to be expected. It has to do with factors like the efficiency of the LEDs used, how much current is actually run through them and the spectral sensitivity of the human eye in relation to the wavelengths of the LEDs. Don't worry about this too much; if 255 red + 70 green gives you a good yellow, then simply use 70 as the maximum value for green. You may want to work out a similar ratio for the blue LED so that your white is the kind of white you like it to be.

Hi, @jdganther

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

This tells us very little.
image

No MOSFET type, lead designators etc etc.
Sorry but drawing it by hand will be easier and produce a better result.

And place it in a new post.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.