Saturday, July 16, 2016

Filled Under: , , , ,

Analog Write with 12 LEDs on an Arduino Mega

Arduino Arduino UNO R3 Basic LED library

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.

Step 1: What You Need?

1 x Arduino Mega board 
12 x LED (Red)
12 x 220 Ohm Resistor 
1 x Breadboard
Male-to-Male Jumper Wires

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your 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.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.
2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> AnalogWriteMega

Click press the "upload" button (see the button with right arrow mark).


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.





Download: 



Unknown

Author & Editor

My Robot Education Sdn. Bhd. (Robotedu.my) was founded in 2015 as the first robotics education centre in Malaysia to provide Arduino-based robotics courses for youths. Our vision is to be able to provide robotics education to every youth in Malaysia.

0 comments:

Post a Comment

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.