Project 1.04 Pulse LED

In this project you’ll learn how to use the analogWrite function to create a “pulsing” effect on an 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:

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);
}
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.