Turn on Multiple LED Arduino Using Serial Monitor

Posted on

Turn on LED Through Serial Monitor

Turn On Multiple LED Arduino using Serial Monitor – Today we will learn how to turn on multiple LEDs using an Arduino serial monitor. This article is a continuation of the post Turning LEDs on Using an Arduino Serial Monitor.

Serial is used for communication between Arduino and a computer or other device. All Arduino boards have at least one serial port (also known as a UART or USART), and some have more than one serial.

LED Wiring

Turn on Multiple LED Arduino

Final Condition

In this experiment we need 7 LEDs that will be turned on alternately with the following conditions:

  • If we enter the number 1 will turn on the LED to 1 and turn off the LED other than this
  • If we enter the number 2 it will turn on the 2nd LED and turn off the other LEDs
  • If we enter the number 3 it will turn on the 3rd LED and turn off the other LEDs
  • If we enter the number 4 it will turn on the 4th LED and turn off the other LEDs
  • If we enter the number 5 it will turn on the 5th LED and turn off the other LEDs
  • If we enter the number 6 it will turn on the 6th LED and turn off the other LEDs
  • If we enter the number 7 it will turn on the 7th LED and turn off the other LEDs

I will simulate this experiment with the help of Arduino simulation software called SimulIDE. If you don’t have this software, you can download it here.

Program Code

The following is the code used so that your arduino can run according to the above conditions. Please upload it to your Arduino.

int led[] = {1, 2, 3, 4, 5, 6, 7};    //Atur Pin untuk LED     
char  i;
int index;
int mati;

void setup() {
  Serial.begin(9600);
  for (index = 0; index <= 7; index++) {
    pinMode (led[index], OUTPUT);
  }
}

void loop() {
  if (Serial.available() > 0) {
    i = Serial.read();
    Serial.println(i);

    if ((i == '1') || (i == '2') || (i == '3') || (i == '4') || (i == '5') || (i == '6') || (i == '7')) {
      for (index = 0; index <= 7; index++) {
        digitalWrite (led[index], LOW);
      }

      if (i == '1') {index = 0;}
      if (i == '2') {index = 1;}
      if (i == '3') {index = 2;}
      if (i == '4') {index = 3;}
      if (i == '5') {index = 4;}
      if (i == '6') {index = 5;}
      if (i == '7') {index = 6;}

      digitalWrite(led[index], HIGH);
    }
  }
}

I hope this Turn on Multiple LED Arduino using Serial tutorial is useful.

Leave a Reply

Your email address will not be published.