Arduino Mega - LED Module

Big Arduino Mega builds - a CNC controller, a home alarm panel, a model railway yard - usually need a row of status lights on the front. Doing that with bare LEDs means one resistor per light and a lot of fiddly breadboard work. A 10mm LED module removes that work: three labeled pins, plug them in, and the light is ready. This tutorial instructs you how to use the Arduino Mega to switch an LED module on and off and to control its brightness. In detail, we will learn:

Arduino Mega LED module

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
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 Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

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 Module and Its Pinout

LED module pinout
image source: diyables.io

Think of the LED module as "an LED with its resistor and driver already on board". The light is a 10mm diffused round LED, mounted on a small 20mm x 20mm black PCB. Next to it sit a 330Ω current-limiting resistor, a 1KΩ resistor on the input line, and a small transistor. The LED takes its power from the VCC pin. The IN pin does not feed the LED - it only tells the transistor to open or close. Because of this, the Arduino Mega pin gives only a tiny current, and you never need to add a resistor yourself.

LED Module
Operating Voltage 3.3V to 5V DC
LED 10mm, diffused, round
Board Size 20mm x 20mm
Onboard Parts 330Ω LED resistor, 1KΩ input resistor, transistor switch
Header 3-pin, 2.54mm, right-angle male pins (presoldered)
Logic Active HIGH (IN = HIGH turns the LED on)
Colors in a Pack red, yellow, blue, green, white

The header has three pins, printed on the board in this order:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to the 5V pin of the Arduino Mega (3.3V also works)
  • IN pin: must be connected to a digital output pin of the Arduino Mega. A HIGH signal lights the LED, a LOW signal turns it off, and a PWM signal sets its brightness.

※ NOTE THAT:

The arrangement of pins on a module may differ from one manufacturer to another. Always follow the labels printed next to the header, not the position of the pins. Check them before you plug in the wires!

Wiring Diagram

Only three jumper wires are needed: GND to GND, VCC to 5V, and IN to pin 9 of the Arduino Mega. Pin 9 is one of the Mega's PWM pins, so the same wiring works for both the blink sketch and the fade sketch.

The wiring diagram between Arduino Mega LED module

This image is created using Fritzing. Click to enlarge image

LED Module Arduino Mega
GND GND
VCC 5V
IN Pin 9

How To Program For LED Module

  • The first step is to give a name to the Arduino Mega pin that is connected to the IN pin of the module:
#define LED_MODULE_PIN 9
  • Configure that pin as an output inside setup():
pinMode(LED_MODULE_PIN, OUTPUT);
  • Write HIGH to the pin to switch the LED on, and LOW to switch it off:
digitalWrite(LED_MODULE_PIN, HIGH); // LED on digitalWrite(LED_MODULE_PIN, LOW); // LED off
  • To set the brightness instead, write a PWM value from 0 (off) to 255 (full brightness):
analogWrite(LED_MODULE_PIN, 128); // about half brightness

Arduino Mega Code for LED Module - Fade with PWM

This time the sketch uses analogWrite() on pin 9. The level climbs from 0 to 255 in steps of 15, then falls back to 0, so the LED slowly glows and dims again.

/* * 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-module */ #define LED_MODULE_PIN 9 // PWM-capable Arduino Mega pin wired to the IN pin of the LED module int level = 0; // current PWM level sent to the module (0 to 255) int change = 15; // how much the level moves on each step void setup() { Serial.begin(9600); pinMode(LED_MODULE_PIN, OUTPUT); } void loop() { analogWrite(LED_MODULE_PIN, level); // a higher PWM level makes the LED brighter Serial.print("Brightness level: "); Serial.println(level); level = level + change; // move to the next step // at either end (fully off or fully bright), reverse the direction if (level <= 0 || level >= 255) { change = -change; } delay(60); // short pause so the fade is easy to see }

Detailed Instructions

  • Keep the same wiring as before.
  • Copy the fade code above into the Arduino IDE and click Upload.
  • Look at the LED module and open the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Brightness level: 0 Brightness level: 15 Brightness level: 30 Brightness level: 45 Brightness level: 60 Brightness level: 75 Brightness level: 90 ... Brightness level: 240 Brightness level: 255 Brightness level: 240 Brightness level: 225
Ln 11, Col 1
Arduino Mega Or... on COM15
2

※ NOTE THAT:

The fade only works on PWM pins. On the Arduino Mega, these are pins 2 to 13 and 44 to 46. If you move the IN wire to a pin without PWM, the LED will only switch between fully on and fully off.

Additional Knowledge

How does the LED module compare with a bare LED on the Arduino Mega?

Bare LED LED Module
Series resistor You must add one (e.g. 220Ω) Already on the board (330Ω)
Polarity You must find the anode and cathode legs Pins are labeled GND, VCC, IN
Current from the Mega pin The whole LED current Only a tiny control current
Connection Legs on a breadboard 3-pin header, jumper wires

It is evident that the LED module is the faster and safer choice for indicators, while a bare LED is cheaper when you need many lights in a tight space.

Because each module draws almost no current from its IN pin, the Arduino Mega can drive many of them at once - for example, one module per machine state on a large control panel. Just keep the total current of all LEDs, taken from the 5V pin, within what your USB port or power supply can give.

Video Tutorial

※ 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!