Built-in project · Arduino Uno R3

Set the LED on pin 11 from the serial monitor, on an Arduino Uno

Type a number from 0 to 255 into the serial monitor and the LED on pin 11 takes that brightness: Serial.parseInt() reads it and analogWrite() applies it. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading numbers from the serial monitor
  • Dimming with analogWrite
  • Keeping a value in range with constrain
  • PWM and brightness
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

The LED is wired from pin 11 through a 220 Ω resistor to ground. Pin 11 supports PWM, which switches the pin on and off very fast. The longer it is on in each cycle, the brighter the LED looks.

You type a number from 0 to 255 in the serial monitor and send it. The sketch reads the number, clamps it to that range, and uses it as the brightness. Then it prints "UNO received:" with the value. Zero is off and 255 is full.

Parts list

4 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
LED, red pin CArduino Uno R3 pin GND
Resistor, 220 Ω pin 1Arduino Uno R3 pin 11
LED, red pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Brightness by message: type 0 to 255 to set the LED on pin 11.
//
//   pin 11 -> 220 ohm -> LED -> GND
//
// Serial.parseInt() reads digits until something that is not a digit, and
// returns the number. constrain() keeps it between 0 and 255, the range
// analogWrite() takes.

const int LED_PIN = 11;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("UNO is ready! Type a number from 0 to 255.");
}

void loop() {
  if (Serial.available() > 0) {
    int value = Serial.parseInt();
    while (Serial.available() > 0 && (Serial.peek() == '\n' || Serial.peek() == '\r')) Serial.read();
    value = constrain(value, 0, 255);
    analogWrite(LED_PIN, value);
    Serial.print("UNO received: ");
    Serial.println(value);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes LED_PIN (pin 11) an output, starts serial at 9600 baud, and prints a prompt asking for a number from 0 to 255.

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("UNO is ready! Type a number from 0 to 255.");
}

loop sketch.ino

Only acts when something has arrived. Serial.parseInt() reads the digits as a number, and a small loop throws away any newline characters after it. constrain limits it to 0 to 255, analogWrite sets the brightness, and the value is printed back.

void loop() {
  if (Serial.available() > 0) {
    int value = Serial.parseInt();
    while (Serial.available() > 0 && (Serial.peek() == '\n' || Serial.peek() == '\r')) Serial.read();
    value = constrain(value, 0, 255);
    analogWrite(LED_PIN, value);
    Serial.print("UNO received: ");
    Serial.println(value);
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing happens when you type This project
    Make sure the serial monitor is at 9600 baud, and that you press send.
  • A big number like 500 gives full brightness only This project
    That is by design. constrain caps the value at 255.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.