Arduino Mega - LED - Blink

This guide shows you how to use an LED with the Arduino Mega. You will learn to write a simple program for the Arduino Mega to turn an LED on and off, and to make it blink.

Arduino Mega Blink LED

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED

Pinout

An LED has two pins:

  • Negative pin: connect to ground (0V)
  • Positive pin: controls whether the LED is on or off
LED Pinout

How It Works

After you connect the negative side (cathode) to the ground (GND):

  • If GND is connected to the LED's positive side, the LED will be off.
  • If VCC is connected to the LED's positive side, the LED will be on.
How LED Works

Also, by sending a PWM signal to the LED’s positive side, you can adjust how bright the LED is based on the PWM value. Details are in this tutorial: this tutorial.

※ NOTE THAT:

Most LED lights need a resistor. You can connect the resistor to the positive side (the +) and the power supply, or to the negative side (the −) and the ground. The resistor value depends on the LED’s specifications. Some LEDs already have a resistor built in. For these LEDs, you might not need an extra resistor.

Arduino Mega - LED

Set a pin on the Arduino Mega to be a digital output, then you can choose to send ground or power from that pin. To drive an LED, connect the Arduino Mega pin to the LED's positive leg through a resistor.

Wiring Diagram

The wiring diagram between Arduino Mega LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Set a pin on the Arduino Mega as a digital output using the pinMode() function. For example, pin 9:
pinMode(9, OUTPUT);
  • Set the pin to ground to turn off the LED using the digitalWrite() function.
digitalWrite(9, LOW);
  • Set the pin high (to VCC) to turn on the LED with digitalWrite().
digitalWrite(9, HIGH);

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-led-blink */ #define LED_PIN 9 // The Arduino UNO R4 pin connected to the LED // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 9 as an output. pinMode(LED_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(500); // wait for 500 milliseconds digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW delay(500); // wait for 500 milliseconds }

Detailed Instructions

Follow these steps one by one.

  • Connect the LED to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Select the board Arduino Mega and the correct port (COM port).
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
Arduino IDE - How to Upload Code
  • Look at the LED status.

Code Explanation

The simple explanation is in the comments of the Arduino code shown above.

※ NOTE THAT:

  • The code above uses the delay() function. Delay stops the Arduino Mega from doing other tasks. If your project needs to run several tasks at the same time, you should avoid stopping the Arduino Mega. Instead, use a method that doesn't stop other tasks for Arduino Mega.
  • This guide has easy-to-understand information to help you learn how it works. To control an LED easily, you can use the Arduino Mega - LED library

Video Tutorial

Additional Knowledge

Which pins on the Arduino Mega can be used as outputs to drive an LED?

  • Pins 0 to 13
  • Pins A0 to A5

※ NOTE THAT:

Only use each pin for one job at a time. For example, if you already set a pin to read a digital input or to do PWM, don’t use the same pin to drive an LED. Also, don’t use pins 0 and 1 for other tasks if you are using the Serial.println() function, because these pins are reserved for Serial communication.

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!