Home > Tutorials > Week #02 – Fade a LED

Week #02 – Fade a LED

One simple Arduino project every week!

(1) Objectives

– Know the concepts of PWM.

– Apply it to change brightness of the LED.

– Make LED fade in and fade out.

(2) Requirements

– An Arduino board, of course.

– A LED.

– Adapter cable to connect Arduino and PC.

– Some wires and breadboard if needed.

(3) Setup

– Plug the LED to the board.

– Connect the GND with short leg.

– Connect the long leg with pin 3.

(sorry, I don’t have Fritzing at the moment, but in the next coming projects I will try to use it).

Setup

Setup

 

(4) Explanation

Q:  Why pin 3?

A: Please get the concept of PWM (Pulse Width Modulation) first; then check your board to know which pins are PWM. On my Arduino UNO, the PWM pins are: 3, 5, 6, 9, 10 and 11.

You can get more information about PWM here: http://arduino-info.wikispaces.com/Arduino-PWM-Frequency

(5) Source

/**
 * @author		Pete Houston
 * @Objective	fade a LED in and out
 */

// use port 3
const int PORT_LED = 3;

void setup()
{
	// nothing to do here
}

void loop()
{
	// set init brightness value
	int val = 0;

	// start fade in
	for(val = 0; val <= 255; val += 10) {
		analogWrite(PORT_LED, val);
		// wait for a while
		delay(50);
	}

	// reset to max
	if(val >= 255) { val = 255; }

	// start fade out
	for( ; val >= 0; val -= 10) {
		analogWrite(PORT_LED, val);
		// wait for a while
		delay(50);
	}
}

(6) Execution

– The source code is easy enough to understand.

– On your column breadboard, you can plug several LEDs in a line to see the fading effect. I plug three, and they’re fading fine.

Result

Result

(7) Final Words

– Just try to find out, what you’ve learned and known from this project? and what can you do further with this?

 

Cheers,

Pete Houston

Categories: Tutorials Tags: , , , ,
  1. March 16, 2012 at 2:16 pm

    Chào bạn,

    Bạn cho phép mình sưu tập tutorial của bạn cho trang web công đồng Arduino được ko? Mình sẽ để tác giả bài viết dưới tên của bạn và trích dẫn nguồn từ đây.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: