RGB LED Code not entering setup

I am having an issue with a script I am writing to cause an RGB LED to slowly change through the colours, I have run sims that show it should work and I was able to make it work on an arduino nano 33. I am trying to put this onto an arduino uno r3 and it just wont work, I have set up some serial writes to show where a problem might be but the code never hits a single one, never even enters setup, not sure what the issue could be. I am sure that its an issue with the code as the examples, flash led etc work perfectly fine.

int PIN_RED = 9;
int PIN_GREEN = 10;
int PIN_BLUE = 11;

void setup(){
  Serial.print("starting");
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
  Serial.print("done");
}

int red = 255;
int grn = 0;
int blu = 255;
int state = 1;

void loop()
{
  delay(10);

  if (state == 1)//blu down
  {
    blu -= 1;
    if (blu == 0)
    {
      state += 1;
    }
  }
  if (state == 2)//grn up
  {
    grn += 1;
    if (grn == 255)
    {          
      state += 1;
    }
  }

  if (state == 3)//red down
  {
    red -= 1;
    if (red == 0)
    {
      state += 1;
    }
  }

  if (state == 4)//blu up
  {
    blu += 1;
    if (blu == 255)
    {
      state += 1;
    }
  }

  if (state == 5)//grn down
  {
    grn -= 1;
    if (grn == 0)
    {
      state += 1;
    }
  }

  if (state == 6)//red up
  {
    red += 1;
    if (red == 255)
    {
      state = 1;
    }
  }

  setColour(red, grn, blu);
}

void setColour(int r, int g, int b)
{
  analogWrite(PIN_RED, r);
  Serial.write(r);
  analogWrite(PIN_GREEN, g);
  Serial.write(g);
  analogWrite(PIN_BLUE, b);
  Serial.write(b);
}

Hi,

We can start by solving the first problem...
In order to read something that you´re printing in the Serial Monitor, you must first stablish a comunication. There´s a line missing in your setup:

Serial.begin(9600); (set the serial monitor baud rate the same you´re using here)

This is because you don’t need the Serial.begin call on a Nano 33 as it generates its own virtual serial port which doesn’t use any serial interface baud rate. If you do use the call on the Nano 33 then the baud rate you give it is just ignored.

2 Likes

Ah, thank you, that has started comms, although Im not sure exactly what it started as the data is showing garbage data, so as far as I can tell something is going wrong with the actual int assignment, since there should be rgb ints and its not showing that.

Update, I have managed to lose the nonsense data, I went from Serial.write to serial.print, so as far as I can tell the code itself is solid, so I can assume its a hardware issue now since the variables seem to be setting correctly but the led is not turning on.

So next you need to post a schematic (not a Ftrtzing physical layout diagram) and a photograph of your system showing where all the wires go.

OK, schematics and photos. I apologise for the blurry ones but I just included all the ones I took, also I use two sets of leds, resistors, cables and breadboards to ensure that its not those things that are the issue. Included is the best schematic I can do, not sure of a good open source resource to make them in.

I am an idiot. The cathode isnt the LED leg I thought it was.

UPDATE. I cant make the rgb led work with the arduino. I have identified the cathode pin and verified this with a small battery and some wires, but the arduino refuses to power it, I am not sure what the issue is now as I definatly have it set up correctly.

1 Like

I do not know what the issue is, it seems that that the pins that were positive seem to be used as ground now? IF i plug the red cable into the cathode then the green and sections light up but not the red.

The same works with any of the output pins, plug in the blue to the cathode, red and green, plug in green, red and blue. I really dont get what the issue is

The question may be as simple as "is it a common Cathode or a common Anode ?"
Either way it seems that the longest pin should be the common Cathode/Anode.
If it;'s a common Cathode your circuit should work properly.
What is latest version of the sketch you are using ?

The “schematic” still doesn’t show much. Is the cathode connected to the ground? If so you have drawn it upside down. If not it needs to be.

Just write some test code where you light one LED at a time by doing a digitalWrite high then low for each pin in turn.

Just checked the data sheet of the LED I bought and its common anode. Im not sure how to progress from here as I need to switch the grounds, but I dont know how to do that programatically.

So the anode now goes to 5v
and for the pins the analogwrite() is inverted

analogWrite(PIN_RED, 255 - r);

etc.

1 Like

Thank you. Lesson learned, next time look at the data sheet first.

1 Like