Sharma IP
Sharma IP

Reputation: 21

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?

Upvotes: 1

Views: 875

Answers (2)

alitayyeb
alitayyeb

Reputation: 81

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

Upvotes: 1

Marcii
Marcii

Reputation: 43

Use digitalWrite() to the appropriate GPIO and the appropriate gpio state (HIGH/LOW) in your setup() function. This will set your LEDs initially once at startup. By the way, it is enough to call Serial.begin(BaudRate); only once in your setup() function.

Upvotes: 0

Related Questions