Project 1.04 Pulse LED
This project demonstrates the full range of the analogWrite function, and how that can be used to create a pulsing effect.
Project Code:
/////////////////////////////////////////////////
// 1.04 - Pulse LED
byte LED1 = 13;
void setup() {
pinMode(LED1, OUTPUT);
}
void loop() {
byte wait = 10;
for (int i = 0; i < 255; i++) {
analogWrite(LED1, i);
delay(wait);
}
for (int i = 255; i > 0; i--) {
analogWrite(LED1, i);
delay(wait);
}
}
/////////////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
Before we jump into the code let’s take a look at what a for-loop is. A for-loop is a way to repeat a chunk of code a specified number of times. See the table below for a breakdown of a for-loop structure:
| Part | Description |
|---|---|
| for | Loop type |
| i < 255; | This is a conditional. The for-loop will continue to run while this is true. In this case, i is less than 255 |
| i++ | This increments the i variable by 1 every time the loop executes |
{
analogWrite(LED1, i);
delay(wait);
} |
This is the code that runs every time the loop executes |
for-loops can be a foreign concept. For a video explanation please visit:
https://www.youtube.com/watch?v=b4DPj0XAfSg
Here in the program a for-loop is used to count 0 – 255, slowly increasing the brightness of the LED. The index of the for-loop ( i ) is passed to the analogWrite function as the for-loop loops. A small delay is added after each analogWrite so the changes can be perceived by the human eye.
for (int i = 0; i < 255; i++) {
analogWrite(LED1, i);
delay(wait);
}
Here the program counts down 255 – 0, slowly decreasing the brightness of the LED.
for (int i = 0; i < 255; i++) {
analogWrite(LED1, i);
delay(wait);
}

