Go back

Serial LED Control with Arduino: Send Commands to Blink

Introduction

Welcome to the Serial LED Control project! In this tutorial, you’ll learn how to control an LED connected to an Arduino Uno by sending simple commands over the Serial Monitor. This is a classic beginner project that combines two fundamental concepts: USART (Universal Synchronous/Asynchronous Receiver-Transmitter) communication and GPIO (General Purpose Input/Output) output. By the end, you’ll be able to turn an LED on and off by typing “1” or “0” in the Serial Monitor, and even query the LED’s status with ”?”.

We’ll be using Velxio, an in-browser circuit and microcontroller simulator with SPICE-accurate analog support. No hardware needed—just your browser! Let’s dive in.

Circuit Walkthrough

Circuit diagram of serial-led-control on the Velxio simulator canvas

The circuit is incredibly simple. You only need two components:

Wiring Details

  1. LED Anode (long leg, pin A) connects to Arduino pin 13 via a green wire (#00cc00).
  2. LED Cathode (short leg, pin C) connects to Arduino GND via a black wire (#000000).

That’s it! No resistor is needed because the Arduino Uno’s pin 13 already has a built-in current-limiting resistor on the board. In a real circuit, you’d add a 220Ω resistor in series with the LED, but for simulation, this works perfectly.

How to Build in Velxio

  1. Open the Serial LED Control example in Velxio.
  2. You’ll see the Arduino Uno and LED already placed on the canvas. If you want to build from scratch, use the component picker (shown below) to add an Arduino Uno and an LED.

Velxio component picker modal browsing parts by category

  1. Click on the Arduino Uno to select it, then click on pin 13. Drag a wire to the LED’s anode (pin A).
  2. Similarly, wire the LED’s cathode (pin C) to any GND pin on the Arduino.
  3. Use the canvas toolbar to zoom and pan for a better view.

Velxio canvas toolbar above the simulation viewport

Code Walkthrough

Source code for serial-led-control in the Velxio Monaco editor

Now let’s examine the code that makes this work. Open the code editor in Velxio (you’ll see the Monaco editor with syntax highlighting).

// Serial LED Control
// Send "1" to turn LED ON, "0" to turn LED OFF.
// Demonstrates Serial input controlling hardware.

const int LED_PIN = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  Serial.println("=========================");
  Serial.println(" Serial LED Controller");
  Serial.println("=========================");
  Serial.println("Send '1' = LED ON");
  Serial.println("Send '0' = LED OFF");
  Serial.println("Send '?' = Status");
  Serial.println();
}

bool ledState = false;

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();

    switch (cmd) {
      case '1':
        digitalWrite(LED_PIN, HIGH);
        ledState = true;
        Serial.println("[OK] LED is ON");
        break;
      case '0':
        digitalWrite(LED_PIN, LOW);
        ledState = false;
        Serial.println("[OK] LED is OFF");
        break;
      case '?':
        Serial.print("[STATUS] LED is ");
        Serial.println(ledState ? "ON" : "OFF");
        Serial.print("[STATUS] Uptime: ");
        Serial.print(millis() / 1000);
        Serial.println("s");
        break;
      default:
        if (cmd >= 32) { // ignore control chars
          Serial.print("[ERR] Unknown command: '");
          Serial.print(cmd);
          Serial.println("'  (use 1, 0, or ?)");
        }
        break;
    }
  }
}

How It Works

Running the Simulation

  1. Click the Run button in the editor toolbar.

Velxio editor toolbar with compile and run controls

  1. Open the Serial Monitor (usually a button in the toolbar or a separate panel).
  2. Type 1 and press Enter – the green LED should light up.
  3. Type 0 – the LED turns off.
  4. Type ? – you’ll see the status and uptime.

Key Concepts for Students

This project teaches several important concepts:

Common Pitfalls and Debugging Tips

Suggested Extensions

Once you’ve mastered this project, try these enhancements:

  1. Add a second LED on pin 12 and control it with commands like ‘2’ and ‘3’.
  2. Use PWM to control brightness: send a number from 0 to 255 to set LED brightness via analogWrite().
  3. Add a button as input: read a digital pin and toggle the LED when pressed.
  4. Implement a command parser that accepts strings like “ON” or “OFF” instead of single characters.
  5. Log data to the Serial Monitor: print sensor readings or timestamps.

Conclusion

You’ve just built a simple yet powerful Serial LED controller! This project is the foundation for countless IoT and automation projects where you control hardware via text commands. Velxio makes it easy to experiment without any physical components.

Ready to try it yourself? Open the Serial LED Control example in Velxio, run the simulation, and start sending commands. Happy tinkering!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
I2C Scanner with Arduino: Find Every Device on the Bus
Next Post
Serial Echo with Arduino: Learn USART Communication