Search code examples
arduinorgbled

How to make LED off initially in Arduino?


I am working with LED strip. I am new to Arduino. I want the initial value of all LED color to be low. I have used characters as "0, 1, 2, 3" for turning the led on and off for Red, green and blue colors. This is the image of RGB LED Strip. This is the circuit connection.

The following is the code:

void setup()
{
  pinMode(redLED, OUTPUT);
  Serial.begin(BaudRate);

   pinMode(blueLED, OUTPUT);
  Serial.begin(BaudRate);

pinMode(greenLED, OUTPUT);
  Serial.begin(BaudRate);
}
void loop()
{
 incomingOption = Serial.read();
 switch(incomingOption){
    case '1':
      // Turn ON LED
      digitalWrite(redLED, HIGH);
      
       
      break;
    case '2':
     
      digitalWrite(redLED, LOW);          
      break;

      case '3':
      // Turn ON LED .. and so on
     
     }
}

I want each color to be off initially. How can I do that?


Solution

  • You can do that with the digitalWrite function. You should also call Serial.begin once. if your RGB LED strip is common cathode the following code is suggested:

    void setup() {
        pinMode(redLED, OUTPUT);
        pinMode(blueLED, OUTPUT);
        pinMode(greenLED, OUTPUT);
        digitalWrite(redLED, LOW);
        digitalWrite(blueLED, LOW);
        digitalWrite(greenLED, LOW);
        Serial.begin(BaudRate);
    }
    
    void loop() {
       incomingOption = Serial.read();
       switch(incomingOption){
          case '1':
             // Turn ON LED
             digitalWrite(redLED, HIGH); 
             break;
          case '2':
             digitalWrite(redLED, LOW);          
             break;
          case '3':
             // Turn ON LED .. and so on
          default:
             break;
       }
    }
    
    

    NOTE You should use digitalWrite(pin1, HIGH); to turn off the line that connected to pin1, if your RGB LED strip is common anode