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:

- Raspberry Pi Pico (nano-rp2040): The brain of the operation. We’ll use pin D2 as our control pin.
- LED (led-status): A green LED that we’ll turn on and off. The anode (A) connects to the Pico, and the cathode (C) connects to a current-limiting resistor.
- Resistor (r1): A 220Ω resistor to limit current through the LED and prevent damage.
Wiring Details
- 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.
- From LED Cathode to Resistor: A grey wire connects the cathode (C) of the LED to one end of the resistor (pin 1).
- 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
setup(): We set the built-in LED pin as an output, start serial communication at 115200 baud, wait 500ms for the serial connection to stabilize, then print a welcome message and instructions.loop(): We check if a character is available in the serial buffer. If so, we read it and use aswitchstatement to handle three commands:'1': Turn the LED on (HIGH) and print confirmation.'0': Turn the LED off (LOW) and print confirmation.'?': Read the current state of the LED pin and print “ON” or “OFF”.- Any other printable character (ASCII >= 32) prints an “Unknown command” message. Non-printable characters are ignored.
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
- Serial Communication: The Pico talks to your computer via USB serial.
Serial.begin(115200)sets the baud rate—make sure your serial monitor matches this speed. - Digital Output:
pinMode(pin, OUTPUT)anddigitalWrite(pin, HIGH/LOW)are the basics of controlling pins. - Switch Statement: A clean way to handle multiple commands. It’s more efficient than a chain of
if-elsestatements. - Input Validation: The code ignores non-printable characters and warns about unknown commands. This is good practice for robust programs.
Common Pitfalls & Debugging Tips
- Serial Monitor Baud Rate: If you see garbage characters, your serial monitor baud rate doesn’t match 115200. In Velxio, the serial monitor is built-in and automatically uses the correct rate.
- LED Not Lighting: Check that the LED is oriented correctly (anode to D2, cathode to resistor). Also verify the resistor value—too high and the LED will be dim; too low and it might burn out.
- Commands Not Working: Make sure you’re sending exactly ‘1’, ‘0’, or ’?’ (without quotes). The code reads a single character, so sending “1\n” will work because the ‘1’ is read first and the newline is ignored (since it’s non-printable).
- Using the Simulator: Velxio’s toolbar lets you compile and run your code.
Click the “Run” button to start the simulation. The canvas toolbar
gives you controls like pause and reset. If you need to add components, use the component picker
.
Suggested Extensions
Once you have the basic project working, try these enhancements:
- Control an External LED: Change
LED_BUILTINto2and wire an external LED to pin D2 as shown in the circuit diagram. - Add More LEDs: Use multiple pins and commands like ‘r’ for red, ‘g’ for green, ‘b’ for blue.
- PWM Dimming: Use
analogWrite()to set brightness levels. Send values like ‘0’ to ‘9’ for 0% to 100% brightness. - Two-Way Communication: Have the Pico send sensor data (e.g., temperature) when you send a command like ‘t’.
- 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!