Analog Write with 12 LEDs on an Arduino Mega

Fade 12 LEDs on and off, one by one, using an Arduino Mega board.

This example fades 12 LEDs up and the down, one by one, on an Arduino Mega board, taking advantage of the increased number of PWM enabled digital pins of this board.

Hardware Required

  • Arduino Mega Board

  • 12 Red LEDs

  • 12 220 ohm resistors

  • hook-up wires

  • breadboard

Circuit

circuit

Connect the longer, positive legs (anodes) of 12 LEDs to digital pins 2-13 through 220 ohm current limiting resistors. Connect the shorter, negative legs (cathodes) to ground.

Schematic

schematic

Code

In the

setup()
function of the code below, a
for()
loop is used to assign digital pins 2-13 of the Mega as outputs.

Next, in the

loop()
function of the program below, a trio of nested
for()
loops are used.

The first of these loops,

for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)

moves through each of the LEDS one by one, from the lowest pin to the highest. Before this loop is allowed to move from one pin to the next, two things must be accomplished. First, you brighten the individual LED through these lines of code:

for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}

With each pass through the loop above, the variable brightness increases by one point, and that value is written to the pin currently selected to the main loop. One that pin reaches the maximum PWM value (255), the following loop kicks in:

for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}

This loop subtracts a point from the brightness variable, dimming the LED back down to 0. Once zero is reached, the main

for()
loop kicks in, and the program moves on to the next LED pin, repeating all the steps mentioned above.

1/*
2
3 Mega analogWrite() test
4
5 This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
6
7 This sketch was written for the Arduino Mega, and will not work on other boards.
8
9 The circuit:
10
11 - LEDs attached from pins 2 through 13 to ground.
12
13 created 8 Feb 2009
14
15 by Tom Igoe
16
17 This example code is in the public domain.
18
19 https://www.arduino.cc/en/Tutorial/AnalogWriteMega
20
21*/
22
23// These constants won't change. They're used to give names to the pins used:
24
25const int lowestPin = 2;
26
27const int highestPin = 13;
28
29void setup() {
30
31 // set pins 2 through 13 as outputs:
32
33 for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
34
35 pinMode(thisPin, OUTPUT);
36
37 }
38}
39
40void loop() {
41
42 // iterate over the pins:
43
44 for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
45
46 // fade the LED on thisPin from off to brightest:
47
48 for (int brightness = 0; brightness < 255; brightness++) {
49
50 analogWrite(thisPin, brightness);
51
52 delay(2);
53
54 }
55
56 // fade the LED on thisPin from brightest to off:
57
58 for (int brightness = 255; brightness >= 0; brightness--) {
59
60 analogWrite(thisPin, brightness);
61
62 delay(2);
63
64 }
65
66 // pause between LEDs:
67
68 delay(100);
69
70 }
71}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/28 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.