How to wire an LED to an Arduino Mega
To wire an LED to an Arduino Mega, connect A to pin 2 and C to GND. The Arduino Mega’s pin is 5 V and a 220 ohm resistor in series limits the current through the LED to about 14 mA, so nothing else is needed. The sketch below uses no library beyond what the board includes. It blinks the LED, half a second on and half a second off, printing LED on and LED off.
- 5 V logic
- Library: none
- 2 wires, 1 helper parts
LED to Arduino Mega wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| LED pin | Arduino Mega pin | Wire color | Why |
|---|---|---|---|
| A (anode) | pin 2, through R1 (220 Ω) | amber | The long leg. Current flows in here, so it takes the signal, with the resistor in the way. |
| C (cathode) | GND | blue | The short leg, on the flat side of the lens, goes to ground. |
| 220 ohm resistor | In series with the anode | n/a | Limits the current. Without it the LED, the pin or both burn out. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (220 Ω) pin 1 | 2 | green |
Power and logic levels for LED and Arduino Mega
Logic level
The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
This pair
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
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.
The working code for the Arduino Mega
Library: none, the sketch drives the pin with digitalWrite(). This is the sketch the circuit above runs.
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);
}
What you should see
The LED lights for half a second and goes dark for half a second. The serial monitor, at 9600 baud, prints LED on and LED off as the pin changes.
An LED that never lights is usually in backwards: the long leg, the anode, faces the resistor and the pin.
The serial monitor prints this, recorded from the circuit above running for two and a half seconds:
LED on
LED off
LED on
LED off
LED onCommon mistakes wiring an LED to an 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.
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.