Go back

Pico Serial LED Control: Build Your First Command Interface

Introduction

Welcome to your first serial-controlled project on the Raspberry Pi Pico! In this tutorial, we’ll build a simple circuit that lets you turn an LED on and off by typing commands into the serial monitor. You’ll learn the basics of serial communication, digital output, and how to use the Velxio simulator to test your code without any hardware.

This project is perfect for beginners who want to understand how microcontrollers talk to a computer. By the end, you’ll be able to extend the concept to control multiple LEDs, read sensors, or even build a simple menu system.

Circuit Walkthrough

Let’s look at the circuit we’ll build. Open the example in Velxio and you’ll see the following components:

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

Wiring Details

  1. From Pico D2 to LED Anode: A green wire connects pin D2 of the Pico to the anode (A) of the LED. This is the control signal.
  2. From LED Cathode to Resistor: A grey wire connects the cathode (C) of the LED to one end of the resistor (pin 1).
  3. From Resistor to Pico GND: A black wire connects the other end of the resistor (pin 2) to the Pico’s ground (GND.1).

That’s it! The circuit is simple but effective. The resistor is essential to avoid burning out the LED. With a 220Ω resistor and a typical LED forward voltage of about 2V, the current will be around (3.3V - 2V) / 220Ω ≈ 6mA, which is safe.

Code Walkthrough

Now let’s dive into the code. Open the editor in Velxio and you’ll see the following Arduino sketch:

// Raspberry Pi Pico — Serial LED Control
// Send '1' to turn LED ON, '0' to turn OFF, '?' for status

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  delay(500);
  Serial.println("=== Pico LED Control ===");
  Serial.println("Commands: 1=ON  0=OFF  ?=status");
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    switch (cmd) {
      case '1':
        digitalWrite(LED_BUILTIN, HIGH);
        Serial.println("LED: ON");
        break;
      case '0':
        digitalWrite(LED_BUILTIN, LOW);
        Serial.println("LED: OFF");
        break;
      case '?':
        Serial.print("LED: ");
        Serial.println(digitalRead(LED_BUILTIN) ? "ON" : "OFF");
        break;
      default:
        if (cmd >= ' ') {
          Serial.print("Unknown command: ");
          Serial.println(cmd);
        }
        break;
    }
  }
}

How It Works

Notice that we use LED_BUILTIN which is a predefined constant for the Pico’s onboard LED (usually on pin 25). In our circuit, we’ve connected an external LED to pin D2, but the code still uses the built-in LED. If you want to control the external LED, change LED_BUILTIN to 2 (or define a constant). For this example, the built-in LED works fine.

Key Concepts

Common Pitfalls & Debugging Tips

Suggested Extensions

Once you have the basic project working, try these enhancements:

  1. Control an External LED: Change LED_BUILTIN to 2 and wire an external LED to pin D2 as shown in the circuit diagram.
  2. Add More LEDs: Use multiple pins and commands like ‘r’ for red, ‘g’ for green, ‘b’ for blue.
  3. PWM Dimming: Use analogWrite() to set brightness levels. Send values like ‘0’ to ‘9’ for 0% to 100% brightness.
  4. Two-Way Communication: Have the Pico send sensor data (e.g., temperature) when you send a command like ‘t’.
  5. Menu System: Print a menu of options and use a loop to wait for valid input.

Call to Action

Ready to try it yourself? Open the live example in Velxio and start experimenting: Pico Serial LED Control on Velxio. No downloads, no setup—just your browser and your curiosity. Happy hacking!


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
Pico I2C Scanner: Find Devices on the Bus
Next Post
Pico Serial Echo: Test UART on RP2040 with Velxio