Example Code for Arduino-Fading Light
Last revision 2026/01/08
This article guides readers through creating a fading LED effect using Arduino, providing detailed instructions on hardware and software setup, wiring diagrams, and sample code utilizing the analogWrite function for controlling LED brightness on pin 10.
Wiring Diagram
The connection diagram is the same with Blinking a LED tutorial.

Sample Code
/*
Fading Light
This example shows how to fade an LED on pin 10 using the analogWrite() function.
*/
int ledPin = 10; // the pin that the LED is attached to
void setup() {
// declare pin 9 to be an output:
pinMode(ledPin,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop(){
fadeOn(1000,5);
fadeOff(1000,5);
}
void fadeOn(unsigned int time,int increament){
//change the brightness by FOR statement
for (byte value = 0 ; value < 255; value +=increament){
// print out the value:
Serial.println(value);
// set the brightness of pin 10:
analogWrite(ledPin, value);
delay(time/(255/5));
}
}
void fadeOff(unsigned int time,int decreament){
//change the brightness by FOR statement
for (byte value = 255; value >0; value-=decreament){
Serial.println(value);
analogWrite(ledPin, value);
delay(time/(255/5));
}
}
Was this article helpful?
