Board and part

Arduino Mega LED Simulator: Wiring, Code and a Live Circuit

This is a red LED with a 220 ohm resistor, wired to an Arduino Mega 2560 and blinking live in your browser. The serial monitor prints LED on and LED off as the pin changes.

  • 5 V logic
  • No library
  • Pin pin 2
  • 220 ohm resistor
Arduino Mega · LEDlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the LED to the Arduino Mega

Two connections matter: the pin to the resistor and the resistor to the LED’s anode, then the cathode to ground. The pin is pin 2 and the supply behind it is 5 V.

Every connection in the circuit above, with the board’s own pin names.

LED pinArduino Mega pinWhy
A (anode)pin 2The long leg. Current flows in here, so it takes the signal, with the resistor in the way.
C (cathode)GNDThe short leg, on the flat side of the lens, goes to ground.
220 ohm resistorIn series with the anodeLimits the current. Without it the LED, the pin or both burn out.

Power

A red LED drops about 2 V, so with 220 ohm in series a 5 V pin pushes about 14 mA through it: bright enough to see and gentle on the pin. The resistor can sit on either side of the LED; what matters is that it is in the loop.

Pins to leave alone on the Arduino Mega

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The LED uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Blinking an LED on Arduino Mega pin 2
const int LED_PIN = 2;   // pin 2, through a 220 ohm resistor to the LED

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

void loop() {
  digitalWrite(LED_PIN, HIGH);
  Serial.println("LED on");
  delay(500);
  digitalWrite(LED_PIN, LOW);
  Serial.println("LED off");
  delay(500);
}

pinMode(LED_PIN, OUTPUT) makes pin 2 drive its level instead of listening to it. digitalWrite(LED_PIN, HIGH) puts 5 V on the pin, which pushes current through the resistor and the LED to ground, and LOW takes it away.

delay(500) holds each state for half a second, so the LED blinks once a second. While delay() waits the board does nothing else; a sketch that must also do other work times itself with millis() instead.

Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.

Pitfalls

Common mistakes with the LED on the Arduino Mega

Putting the LED in backwards

An LED passes current one way only. Backwards it stays dark and nothing is damaged. If it never lights, swap its two legs: the long leg, the anode, faces the signal.

Leaving out the resistor

An LED has almost no resistance once it conducts, so the only limit on the current is the pin. At 5 V that is far more than the LED’s 20 mA rating. The simulator burns the LED out and shows it dark for good, as a real one goes.

Taking the serial or I2C pins

Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. the LED works on any of them until the sketch needs that port, so start on pin 2 and up.

FAQ

Questions and answers

How do I connect an LED to an Arduino Mega 2560?

Wire pin 2 to a 220 ohm resistor, the resistor to the LED’s long leg, and the LED’s short leg to GND. In the sketch call pinMode(2, OUTPUT) and digitalWrite(2, HIGH).

What resistor value should I use?

Subtract the LED’s drop, about 2 V for red, from the pin’s 5 V and divide by the current you want. For 10 mA that is 300 ohm; 220 ohm is the common value because it is safe for any color.

Can I run an LED without a resistor on a pin?

No. The pin has no way to limit the current, so the LED or the pin is damaged. Always put a resistor in series.

Why does my LED stay dim?

Either the resistor is too large, or the pin is being driven with PWM at a low duty. A 220 ohm resistor with digitalWrite(HIGH) gives full brightness.

Build your own LED project

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