Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

The LED Fade Sketch for the Arduino

In this Arduino sketch, you make an LED fade on and off. You need some extra hardware to make the LED fade on and off. For this project you need:

  • An Arduino Uno

  • A breadboard

  • An LED

  • A resistor (greater than 120 ohm)

  • Jump wires

It’s always important to make sure that your circuit is not powered while you’re making changes to it. You can easily make incorrect connections, potentially damaging the components.

This makes a simple circuit like the one used for the Blink sketch, using pin 9 instead of pin 13. The reason for using pin 9 instead of 13 is that 9 is capable of Pulse Width Modulation (PWM), which is necessary to fade the LED.

However, note that pin 9 requires a resistor to limit the amount of current supplied to the LED. On pin 13, this resistor is already included on the Arduino board itself, so you didn’t need to worry about this.

image0.jpg

This schematic shows you the simple circuit connection. Your digital pin, pin 9, is connected to the long leg of the LED; the short leg connects to the resistor and that goes on to ground, GND. In this circuit, the resistor can be either before or after the LED, as long as it is in the circuit.

image1.jpg

It’s always a good idea to color code your circuits — that is, use various colors to distinguish one type of circuit from another. Doing so greatly helps keep things clear and can make problem solving much easier. The most important areas to color code are power and ground. These are nearly always colored red and black, respectively, but you might occasionally see them as white and black as well.

The other type of connection is usually referred to as a signal wire, which is a wire that sends or receives an electrical signal between the Arduino and a component. Signal wires can be any color that is not the same as the power or ground color.

After you assemble your circuit, you need the appropriate software to use it. From the Arduino menu, choose File→Examples→01.Basics →Fade to call up the Fade sketch. The complete code for the Fade sketch is as follows:

/*
 Fade
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 This example code is in the public domain.
 */
int led = 9;   // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
 // declare pin 9 to be an output:
 pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
 // set the brightness of pin 9:
 analogWrite(led, brightness);
 // change the brightness for next time through the loop:
 brightness = brightness + fadeAmount;
 // reverse the direction of the fading at the ends of the fade:
 if (brightness == 0 || brightness == 255) {
 fadeAmount = -fadeAmount ;
 }
 // wait for 30 milliseconds to see the dimming effect
 delay(30);      
}

Upload this sketch to your board. If everything has uploaded successfully, the LED fades from off to full brightness and then back off again.

If you don’t see any fading, double-check your wiring:

  • Make sure that you’re using the correct pin number.

  • Check that your LED is correctly situated, with the long leg connected by a wire to pin 9 and the short leg connected via the resistor and a wire to GND (ground).

  • Check the connections on the breadboard. If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!