Analog Write with 12 LEDs on an Arduino Mega

This example fades 12 LEDs up and the down, one by one, on an Arduino Mega board.

Circuit

Arduino Analog Write with 12 LEDs

image developed using Fritzing. For more circuit examples, see the Fritzing project page

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

Schematic

Arduino Analog Write with 12 LEDs 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);
}

[box color=”#985D00″ bg=”#FFF8CB” font=”verdana” fontsize=”14 ” radius=”20 ” border=”#985D12″ float=”right” head=”Major Components in Project” headbg=”#FFEB70″ headcolor=”#985D00″]

Hardware Required

  • Arduino Mega Board
  • (12) LEDs
  • (12) 220 ohm resistors
  • hook up wire
  • breadboard

[/box]


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top