LED Pin Controller Help

I have a LED Strip that has 12V, R, G, B connections and I want to switch purely between those colors based on a pin that I connect to a 3.3V power source. (Ex: I bring pin 2 high with 3.3V, and then the red lights turn on) I am only getting odd colors though and am having a large delay. Please see my code below:

const int in = 2;     // the number of the input pin
const int in1 = 4;     // the number of the input pin
const int in2= 6;     // the number of the input pin
const int ledPin =  8;      // the number of the LED pin
const int ledPin1 =  10;      // the number of the LED pin
const int ledPin2 =  12;      // the number of the LED pin

// variables will change:
int inState = 0;         // variable for reading the status
int inState1 = 0;  
int inState2 = 0;  

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // initialize input:
  pinMode(in, INPUT);
  pinMode(in1, INPUT);
  pinMode(in2, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  inState = digitalRead(in);
  inState1 = digitalRead(in1);
  inState2 = digitalRead(in2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (inState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  } 
  else if (inState1 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
  } 
  else if (inState2 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hello caldugger

change all

else if

to

 if
1 Like

I can't tell what type of LED strip you're using. It appears to have a +12V line and lines for R, G, and B. Without knowing exactly what you've got, we can't help.

I don't think supplying 3V3 to any of those pins will do what you think they should do.

1 Like

RGB LED Weatherproof flexi-strip 60 LED/m : ID 346 : $125.00 : Adafruit Industries, Unique & fun DIY electronics and kits

This is a link to the lights I have configured. I am wanting to bring a digital pin high using the 3.3V and as a result of that pin being brought high, a certain color will be lit up. Ex: If Pin 2 is high, then turn the color to red, etc. I am using an Arduino Uno R3.

Unformatted code is harder to read and follow. Improperly posted code is harder to work with.
Please read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

1 Like

Thanks for the link.

The 12V power is available to all LEDs and ground the pins associated with the colors you want to light. To light the red LEDs, ground the R pin. The other colors work the same way. To control the intensity of the individual colors, you can dim them using PWM.

** WARNING ** You may destroy your Arduino if you connect it directly to any of these pins as you were planning.

Suggestion - connect a good 12V power supply to the 12V input (the LED strip can draw up to 1.2 Amps). Then connect the R, G, and B lines to ground via an NPN transistor or an N-Channel MOSFET. You can control the transistor / MOSFET via a PWM signal from your Arduino.

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