Controlling LED via Serial with Arduino

The Arduino microcontroller is a versatile device that can be used to control a wide variety of electronic components. One of the simplest components to control with an Arduino is an LED, which can be easily turned on and off using digital output pins. However, in many cases, it is not always practical to have physical switches or buttons to control these components. This is where Serial communication comes in handy.

Serial communication allows the Arduino to communicate with a computer or other device via a virtual serial port. By sending commands or data over this serial connection, it is possible to control electronic components connected to the Arduino. Here it is illustrated how one can control an LED (🔗) connected to an Arduino using Serial communication via serial monitor.

Controlling LED via Serial with Arduino

The code 📋 written and uploaded using Visual Studio Code PlatformIO below shows a simple Arduino sketch that controls an LED connected to digital pin 9 via Serial communication. Using this code, we can control the LED connected to the Arduino via the Serial Monitor in the Arduino IDE or any other serial terminal software. For example, typing 'h' in the Serial Monitor will turn on the LED, while typing 'l' will turn it off. Typing a digit between '1' and '9' will blink the LED with a delay between 100ms and 900ms.

   
int led = 9;

  void setup() {
    pinMode(led, OUTPUT);
    Serial.begin(9600);
  }

  void loop() {
    if (Serial.available()) {
      char com = Serial.read();
      if (com == 'l') {
        digitalWrite(led, LOW);
      }
      else if (com == 'h') {
        digitalWrite(led, HIGH);
      }
      else if (com >= '1' && com <= '9') {
        int delayTime = (com - '0') * 100;
        while (true) {
          if (Serial.available() && Serial.read() == 'l') {
            digitalWrite(led, LOW);
            break;
          }
          digitalWrite(led, HIGH);
          delay(delayTime);
          digitalWrite(led, LOW);
          delay(delayTime);
        }
      }
    }
  }
 

This Arduino code sets up a simple communication protocol over the serial port to control an LED connected to pin 9. The program waits for incoming serial data and processes the input based on the received command.

The setup() function is executed once at the beginning of the program and sets the mode of the LED pin to OUTPUT. It also initializes the serial communication with a baud rate of 9600 using Serial.begin(9600).

The loop() function is executed repeatedly and checks if any data has been received from the serial port using Serial.available(). If data is available, it reads the first character of the incoming data using Serial.read() and stores it in the com variable.

If the received command is 'l', the LED is turned off using digitalWrite(led, LOW). If the received command is 'h', the LED is turned on using digitalWrite(led, HIGH).

If the received command is a digit between '1' and '9', the program enters a while loop that repeatedly blinks the LED with a delay time proportional to the digit value. For example, if the received command is '5', the LED will blink with a delay of 500 ms. The delayTime variable is set to the digit value multiplied by 100 using the expression (com - '0') * 100.

Inside the while loop, the program checks if the 'l' command has been received using Serial.available() and Serial.read(). If the command is received, the LED is turned off and the loop is exited using the break statement.

Otherwise, the LED is turned on using digitalWrite(led, HIGH), delayed for delayTime milliseconds using delay(delayTime), turned off using digitalWrite(led, LOW), and delayed again for delayTime milliseconds using delay(delayTime). This process is repeated until the 'l' command is received. The command to control LED in this tutorial was sent using PlatformIO serial monitor.

In summary, this code sets up a simple communication protocol over the serial port to control an LED, allowing the user to turn it on and off, and to blink it with a delay time proportional to a received digit value.

📹Video Demonstration



This basic concept can be extended to control other electronic components such as switches, motors, and sensors. By sending commands over the Serial port, we can turn on or off switches, control the speed and direction of motors, and read sensor data from the Arduino. This opens up a wide range of possibilities for creating interactive projects and automation systems.

🔼In conclusion, controlling electronic components via serial communication is a powerful tool for Arduino projects. By using simple commands and data over the Serial port, we can control and interact with a wide range of electronic components. This simple LED control example demonstrates the potential of Serial communication, and with a bit of creativity and experimentation, the possibilities are endless.

Post a Comment

Previous Post Next Post