Why Start With Blink on the Mega?
Blinking an LED is the first project almost everyone builds, and for good reason: when the light flashes, you know the whole chain works. The IDE is installed, the USB cable carries data, the right board is selected, the bootloader accepts your code, and a pin can switch real current. Every later problem, from a silent sensor to a blank display, gets easier to track down because you already know the basics are sound.
The Arduino Mega 2560 is the big sibling of the Uno. It runs the same Arduino language and the same Blink sketch, but it has far more pins, memory and serial ports, which is why it is the board of choice for robots, 3D printers, large displays and projects with many sensors. Learning it on a simple LED means you get to know its quirks before the wiring gets complicated.
In this guide you will set up the board, run the built-in Blink example, wire an external LED with a correctly chosen resistor, blink without freezing the program using millis(), and finally move the LED to one of the Mega's extra pins. You need no previous electronics experience, and the whole thing takes about half an hour.
Meet the Arduino Mega 2560
The Mega 2560 is built around the ATmega2560, an 8-bit AVR microcontroller. Compared with the Uno it offers roughly four times the pins and memory.
| Feature | Mega 2560 value |
|---|---|
| Microcontroller | ATmega2560, 8-bit AVR |
| Clock speed | 16 MHz |
| Operating voltage | 5 V |
| Digital I/O pins | 54, of which 15 provide PWM output |
| Analog inputs | 16 |
| Flash memory | 256 KB, of which 8 KB is used by the bootloader |
| SRAM | 8 KB |
| EEPROM | 4 KB |
| Hardware serial ports (UARTs) | 4 |
| Recommended input voltage | 7 to 12 V (limits 6 to 20 V) |
| DC current per I/O pin | 20 mA |
| DC current for the 3.3 V pin | 50 mA |
Two details matter for this project. First, the board has a small yellow "L" LED wired to digital pin 13, and the Arduino software gives that pin the name LED_BUILTIN. You can blink it without any extra parts. Second, the Mega keeps the familiar header layout of the Uno on one side (pins 0 to 13 on top, analog inputs on the bottom) and adds a long double-row header with digital pins 22 to 53 at the end.
💡 Tip
The 3.3 V pin on the Mega can supply only about 50 mA, because it comes from a small on-board regulator. A 3.3 V module that draws more makes the voltage sag and the board unstable. Feed hungry 3.3 V parts from a separate regulator.
Power and Pin Limits You Should Know
Three numbers prevent most beginner damage:
- Per-pin current: a digital pin is rated for 20 mA in normal use, and the chip's absolute maximum is 40 mA. Treat 20 mA as your design limit and aim lower.
- Supply: USB provides up to 500 mA, which covers the board plus a few LEDs and sensors but not motors or long LED strips. For the barrel jack, 7 to 12 V is recommended. The board tolerates 6 to 20 V, but above 12 V the regulator runs hot.
- Logic level: every pin works at 5 V. Connecting a 3.3 V-only device directly to a Mega pin can damage it.
Our LED will draw about 14 mA, comfortably inside these limits.
Components
6 itemsNeed these parts? Get this build sheet by email
Parts list with buy links, plus new builds as they're published. By subscribing you agree to our Privacy Policy. Unsubscribe anytime.
Why Does the LED Need a Resistor?
Once an LED starts conducting it has almost no resistance of its own. Connect it straight between a 5 V pin and ground and it draws far more current than it can survive, and the pin driver inside the ATmega2560 can be damaged as well. A resistor in series limits the current to a safe value.
The calculation is Ohm's law applied to the voltage left over after the LED takes its share. A standard red LED drops about 2.0 V when lit, and the Mega pin outputs 5 V:
R = (V_supply - V_led) / I
R = (5.0 - 2.0) / 0.015 = 200 Ω (for 15 mA)The nearest standard value is 220 Ω, which gives about 14 mA. The LED is slightly dimmer than at its maximum but still clearly bright, and the pin has plenty of safety margin. Other colours drop different voltages, so the ideal value shifts a little:
| LED colour | Typical forward voltage | Resistor for about 15 mA |
|---|---|---|
| Red | about 2.0 V | 220 Ω |
| Yellow / green | about 2.1 to 2.2 V | 180 to 220 Ω |
| Blue / white | about 3.0 to 3.3 V | 100 to 120 Ω |
These are typical figures and vary between parts, so check the datasheet for the LED you actually have. The resistor can sit on either side of the LED, because it only needs to be somewhere in the series path. To learn more, read the LED and Ohm's Law lessons, or see how to read resistor colour codes.
Set Up the Arduino IDE
- Install Arduino IDE 2 from arduino.cc and open it.
- Plug the Mega into your computer with the USB cable. The green power LED on the board should light.
- Choose Tools → Board → Arduino AVR Boards → Arduino Mega or Mega 2560.
- Choose Tools → Processor → ATmega2560 (Mega 2560). This menu appears only for Mega boards, and the wrong choice is the most common cause of upload errors.
- Choose the new entry under Tools → Port.
Genuine boards appear automatically. Many low-cost clones use a CH340 USB chip instead, and some computers need its driver installed before a port shows up.
Step 1: Run the Built-In Blink
Before wiring anything, prove the board works. Open File → Examples → 01.Basics → Blink and click Upload. The sketch looks like this:
// Built-in Blink: flashes the on-board "L" LED (pin 13 on the Mega)
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // make the LED pin an output
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // LED on
delay(1000); // wait one second
digitalWrite(LED_BUILTIN, LOW); // LED off
delay(1000); // wait one second
}During the upload the TX and RX LEDs flicker, and then the L LED starts flashing once per second. If it does, your IDE, driver, cable, board selection and bootloader are all working. If not, jump to the troubleshooting table below before going any further.
Step 2: Wire an External LED
The built-in LED proves the board works, but real projects drive their own parts. We will connect an LED and resistor to pin 13, which has a GND pin right next to it on the Mega's top header, so the wiring is short and tidy.
- With the board unplugged, push the LED into the breadboard. The longer leg is the anode (+) and the shorter leg, next to the flat edge of the case, is the cathode (−).
- Place the 220 Ω resistor from the anode row to a free row.
- Run a jumper from that free resistor row to digital pin 13.
- Run a second jumper from the LED's cathode row to the GND pin beside pin 13.
| Connection | From | To |
|---|---|---|
| Signal | Pin 13 | 220 Ω resistor |
| Current limit | 220 Ω resistor | LED anode (long leg) |
| Return | LED cathode (short leg) | GND |
The Sketch
Plug the board back in and upload this version. It is the same idea as the built-in example, but with named settings at the top so you can change the pin or the timing in one place.
// Arduino Mega 2560 - external LED blink
// LED anode -> 220 ohm resistor -> pin 13, cathode -> GND
#define LED_PIN 13 // digital pin connected to the LED circuit
#define ON_MS 500 // how long the LED stays on (milliseconds)
#define OFF_MS 500 // how long the LED stays off (milliseconds)
void setup() {
pinMode(LED_PIN, OUTPUT); // make the pin drive current
Serial.begin(9600); // optional: lets you watch it in the Serial Monitor
Serial.println("Blink sketch started");
}
void loop() {
digitalWrite(LED_PIN, HIGH); // pin to 5 V, current flows, LED lights
Serial.println("LED ON");
delay(ON_MS);
digitalWrite(LED_PIN, LOW); // pin to 0 V, no current, LED off
Serial.println("LED OFF");
delay(OFF_MS);
}What Each Line Does
#define LED_PIN 13gives the pin a name. Changing it in one place updates the whole sketch, and readers seeLED_PINinstead of an unexplained number.pinMode(LED_PIN, OUTPUT)configures the pin as an output. Pins start as inputs, so forgetting this is a classic reason for an LED that glows faintly or not at all.digitalWrite(pin, HIGH)drives the pin to 5 V andLOWdrives it to 0 V.delay(ms)pauses the program for that many milliseconds.Serial.printlnsends text to the Serial Monitor at 9600 baud. It is a handy way to confirm the sketch is running even if the LED wiring is wrong.
Click Verify first, then Upload. When the LED on pin 13 and the L LED on the board flash together, open the Serial Monitor at 9600 baud to see the ON and OFF messages.
Step 3: Blink Without Freezing the Program
delay() stops everything while it waits. That is fine for Blink, but a robot that has to read a sensor while the LED flashes cannot afford to sit idle. The standard fix is to check the clock instead of waiting, using millis(), which returns the number of milliseconds since the board started.
// Non-blocking blink using millis()
#define LED_PIN 13
const unsigned long INTERVAL_MS = 500; // time between toggles
unsigned long previousMillis = 0;
bool ledState = LOW;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long now = millis();
if (now - previousMillis >= INTERVAL_MS) {
previousMillis = now; // remember when we toggled
ledState = !ledState; // flip the state
digitalWrite(LED_PIN, ledState);
}
// Other work can run here without being delayed by the blink
}The loop now runs thousands of times per second. Each pass it asks whether 500 ms have gone by since the last toggle, and only then flips the LED. Subtracting the old time from the new one keeps working even when millis() rolls over after about 50 days, because the arithmetic uses unsigned numbers.
Step 4: Use the Mega's Extra Pins
This is what makes the Mega different. Move the jumper from pin 13 to pin 22, the first pin of the double-row header, and change one line:
#define LED_PIN 22Upload again and the LED blinks exactly as before. Nothing else changes, because every digital pin works the same way. That is the real advantage of the board: you can add dozens of LEDs, buttons and sensors without running out of pins.
As a small extension, here is a four-LED chaser on pins 22 to 25. Each LED needs its own 220 Ω resistor and a connection to GND.
// Four-LED chaser on the Mega's double-row header
const int LED_PINS[] = {22, 23, 24, 25};
const int LED_COUNT = sizeof(LED_PINS) / sizeof(LED_PINS[0]);
void setup() {
for (int i = 0; i < LED_COUNT; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(LED_PINS[i], HIGH);
delay(150);
digitalWrite(LED_PINS[i], LOW);
}
}Steps
- 1Install Arduino IDE 2 and plug in the Mega with a data-capable USB cable
- 2Select Tools → Board → Arduino Mega or Mega 2560, set Processor to ATmega2560, and choose the new Port
- 3Upload the built-in Blink example and confirm the L LED flashes
- 4Calculate or confirm the resistor: 220 Ω for a red LED on a 5 V pin
- 5Wire the LED anode through the resistor to pin 13 and the cathode to the GND pin beside it
- 6Upload the external LED sketch and check the LED blinks and the Serial Monitor prints ON and OFF
- 7Change ON_MS to 100 and OFF_MS to 900 to see a short flash, then upload the millis() version
- 8Move the LED to pin 22 and try the four-LED chaser
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No port appears under Tools → Port | Charge-only cable, or a clone that needs the CH340 driver | Use a data cable and install the CH340 driver |
avrdude: stk500v2_ReceivePacket(): timeout | Wrong board or processor selected | Choose Arduino Mega or Mega 2560 and set Processor to ATmega2560 |
| Upload works but the external LED stays dark | LED backwards, or resistor and LED sharing one breadboard row | Flip the LED and give each leg its own row |
| LED glows very dimly | pinMode missing, so the pin is still an input | Add pinMode(LED_PIN, OUTPUT) in setup() |
| LED always on, never blinks | Wired to 5 V instead of pin 13 | Move the anode wire to digital pin 13 |
| Upload fails when pins 0 or 1 are wired | Those pins are shared with the USB serial link | Disconnect pins 0 and 1 while uploading |
| Built-in LED blinks, external one does not | Wrong GND pin, or a loose breadboard connection | Re-seat the jumpers and check the cathode goes to GND |
| LED lights but looks very faint | Resistor value too high | Use 220 Ω or less for a red LED |
Frequently Asked Questions
Why is the LED on pin 13? The board designers connected a small LED to pin 13 so every sketch can show a sign of life without extra parts. The same pin is also free to drive an external LED.
Can I use Uno code on the Mega? Yes for simple sketches like this one, because the language and the first 14 pins are the same. Differences appear with pin numbers above 13, serial port names and memory size.
Can I run the LED without a resistor? No. Without a resistor the LED draws far more current than the pin is rated for and can damage the LED or the chip.
How many LEDs can one pin drive? One. A pin should supply no more than 20 mA, which is about one LED with its own resistor. For more LEDs use more pins, or add a transistor or driver chip.
Does the Mega blink faster than the Uno? No. Both run at 16 MHz, so delay() and millis() behave the same.
Experiments to Try Next
Once the LED blinks reliably, you have proven the whole development chain. Here are some small experiments that build on it:
- Morse code: spell your name with short and long flashes by changing
ON_MSandOFF_MS. - Fade with PWM: pin 13 supports PWM, so try
analogWrite(13, value)in a loop to fade the LED. - Button control: read a push button with
digitalRead()andINPUT_PULLUPso the LED toggles on each press. - Traffic light: use three LEDs on pins 22, 23 and 24 and a timed sequence.
The circuit and code are identical on an Uno, so you can preview the logic in the SolderHub simulator before wiring it. To go further, read the Arduino Mega 2560 pinout guide, compare it with the Uno in Arduino Mega vs Uno, learn about PWM on Arduino, and see Arduino power options.
