Learn how to make an LED blink rapidly at first and gradually slow down using Arduino.
In our first Arduino project, Blink an LED, we learned how to turn an LED ON and OFF using digitalWrite() and control the timing with delay().
In our second project, Fade an LED, we learned how to use PWM to gradually change an LED's brightness.
Now, in this project, we are going to use another simple programming concept:
You will need:
Connect the components as follows:
The complete connection is:
D9 → 220 Ω resistor → LED anode → LED cathode → GND
For a typical through-hole LED:
Important: Unlike the previous Fade an LED project, this project does not require a PWM-capable pin. We are controlling the timing of the LED rather than its brightness, so D9 is simply being used as a digital output.
The idea is simple: the LED starts by blinking quickly, but the blinking gradually becomes slower.
We achieve this by increasing the amount of time the Arduino waits between turning the LED ON and OFF.
For example:
It is similar to a person who starts running quickly but gradually slows down as they become tired.
digitalWrite()
to turn the LED ON and OFF, and:
delay()
to control how long the Arduino waits.
We will use the same basic concepts here.
The difference is that instead of keeping the delay constant, we will store it in a variable and increase that variable while the program is running.
For example:
100 ms → 200 ms → 300 ms → 400 ms → 500 ms → ...
As the delay becomes longer, the LED blinks more slowly.
We will use:
The following code prevents the delay from continuing to increase indefinitely:
The blinking should then gradually become slower as the delay increases.
After reaching the maximum delay, the program resets the delay and the LED starts blinking rapidly again.
The complete pattern is:
Fast → Gradually slower → Slowest → Reset → Fast → Repeat
You learned:
In the previous project, we changed the LED's apparent brightness using PWM.
In this project, we change the time interval between LED states.
However, delay() pauses the execution of the program while it waits. In larger Arduino projects, you may want the Arduino to perform other tasks while keeping track of time.
A useful next step is learning how to use the millis() function to create timing without blocking the rest of the program.
This technique becomes especially useful when working with sensors, buttons, motors, displays, and multiple LEDs.
The LED begins by blinking quickly and gradually slows down as the program increases the delay between its ON and OFF states. Once the maximum delay is reached, the program resets the delay and starts the cycle again.
Although the project is simple, it introduces an important programming concept:
A variable can be changed while a program is running to control the behavior of physical hardware.
This concept will become increasingly useful as we move toward more advanced Arduino projects.
Follow RRRTechSolv for more Arduino, electronics, robotics, and embedded-systems projects.
In our first Arduino project, Blink an LED, we learned how to turn an LED ON and OFF using digitalWrite() and control the timing with delay().
In our second project, Fade an LED, we learned how to use PWM to gradually change an LED's brightness.
Now, in this project, we are going to use another simple programming concept:
changing the timing of a program while it is running.
In this project, the LED will initially blink quickly. After each blink, the waiting time will gradually increase, causing the LED to blink more slowly - as though the LED is becoming tired.
In this project, the LED will initially blink quickly. After each blink, the waiting time will gradually increase, causing the LED to blink more slowly - as though the LED is becoming tired.
What You Will Learn
After completing this project, you will understand:- How to control an LED's blinking speed.
- How delay() controls the timing of an Arduino program.
- How a variable can store a changing delay value.
- How to increase a variable during program execution.
- How the same LED circuit can be controlled using different programming techniques.
- The difference between controlling LED brightness and controlling LED timing.
Supplies
The hardware components are the same as those used in Blink an LED Arduino Project.You will need:
- Arduino Uno R3
- 1 × LED
- 1 × 220 Ω resistor
- Solderless breadboard
- Jumper wires
- USB cable
- Computer with the Arduino IDE
Setting Up Your Hardware
The hardware connection for this project is the same as the Blink LED project.Connect the components as follows:
- Arduino D9 (~) → 220 Ω resistor
- 220 Ω resistor → LED Anode (+)
- LED Cathode (-) → Arduino GND
D9 → 220 Ω resistor → LED anode → LED cathode → GND
LED Polarity
Remember that an LED has polarity.For a typical through-hole LED:
- Longer leg → Anode (+)
- Shorter leg → Cathode (−)
- Flat edge of the LED body → Usually indicates the cathode
Important: Unlike the previous Fade an LED project, this project does not require a PWM-capable pin. We are controlling the timing of the LED rather than its brightness, so D9 is simply being used as a digital output.
What Is a "Tired LED"?
So, what do I mean by a Tired LED?The idea is simple: the LED starts by blinking quickly, but the blinking gradually becomes slower.
We achieve this by increasing the amount of time the Arduino waits between turning the LED ON and OFF.
For example:
Short delay
↓
Fast blinking
↓
Increase delay
↓
Slower blinking
↓
Increase delay
↓
Even slower blinking
It is similar to a person who starts running quickly but gradually slows down as they become tired.
How Does It Work?
In the Blink project, we used:digitalWrite()
to turn the LED ON and OFF, and:
delay()
to control how long the Arduino waits.
We will use the same basic concepts here.
The difference is that instead of keeping the delay constant, we will store it in a variable and increase that variable while the program is running.
For example:
100 ms → 200 ms → 300 ms → 400 ms → 500 ms → ...
As the delay becomes longer, the LED blinks more slowly.
Coding
Unlike the Fade project, we do not need PWM for this effect.We will use:
- digitalWrite() to turn the LED ON and OFF.
- delay() to control how long the program waits.
- A variable to store and increase the delay time.
Complete Code
const int ledPin = 9;
int blinkDelay = 100;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);delay(blinkDelay);digitalWrite(ledPin, LOW);delay(blinkDelay);blinkDelay = blinkDelay + 100;if (blinkDelay > 2000) {
blinkDelay = 100;
}
}
Understanding the Code
Let's look at the program step by step.1. Defining the LED Pin
const int ledPin = 9;
This tells the Arduino that the LED is connected to digital pin D9.
The name ledPin makes the code easier to understand.
This tells the Arduino that the LED is connected to digital pin D9.
The name ledPin makes the code easier to understand.
2. Creating the Delay Variable
int blinkDelay = 100;
The variable blinkDelay stores the amount of time the Arduino will wait between changes in the LED's state.
The initial value is:
100 milliseconds
This means the LED begins blinking relatively quickly.
The variable blinkDelay stores the amount of time the Arduino will wait between changes in the LED's state.
The initial value is:
100 milliseconds
This means the LED begins blinking relatively quickly.
3. Configuring the LED Pin
void setup() {
pinMode(ledPin, OUTPUT);
}
The setup() function runs once when the Arduino starts or resets.
The pinMode() function configures D9 as an output so that the Arduino can control the LED.
The setup() function runs once when the Arduino starts or resets.
The pinMode() function configures D9 as an output so that the Arduino can control the LED.
4. Turning the LED ON
digitalWrite(ledPin, HIGH);
This sets the LED pin to HIGH, turning the LED ON.
The program then waits:
This sets the LED pin to HIGH, turning the LED ON.
The program then waits:
delay(blinkDelay);
At the beginning of the program, blinkDelay is 100, so the Arduino waits approximately 100 milliseconds.
At the beginning of the program, blinkDelay is 100, so the Arduino waits approximately 100 milliseconds.
5. Turning the LED OFF
digitalWrite(ledPin, LOW);
This turns the LED OFF.
The program waits for the same amount of time:
This turns the LED OFF.
The program waits for the same amount of time:
delay(blinkDelay);
The LED therefore starts with a short ON period and a short OFF period.
The LED therefore starts with a short ON period and a short OFF period.
6. Increasing the Delay
This is the key part of the project:blinkDelay = blinkDelay + 100;
Each time the LED completes one ON/OFF cycle, the program increases blinkDelay by 100 milliseconds.
The sequence becomes:
Each time the LED completes one ON/OFF cycle, the program increases blinkDelay by 100 milliseconds.
The sequence becomes:
100 ms
200 ms
300 ms
400 ms
500 ms
...
As the delay increases, the LED blinks more slowly.
As the delay increases, the LED blinks more slowly.
7. Resetting the Cycle
Eventually, the delay reaches two seconds.The following code prevents the delay from continuing to increase indefinitely:
if (blinkDelay > 2000) {
blinkDelay = 100;
}
When blinkDelay becomes greater than 2000 milliseconds, the program resets it to 100 milliseconds.
The LED therefore becomes fast again and starts another "tired" cycle.
When blinkDelay becomes greater than 2000 milliseconds, the program resets it to 100 milliseconds.
The LED therefore becomes fast again and starts another "tired" cycle.
How the Program Works
The overall process is:Start
↓
blinkDelay = 100 ms
↓
LED ON
↓
Wait
↓
LED OFF
↓
Wait
↓
Increase blinkDelay by 100 ms
↓
Repeat
↓
Delay reaches 2000 ms
↓
Reset delay to 100 ms
↓
Start again
Here is a video showing how the program works:
Uploading the Program
After entering the code into the Arduino IDE:- Connect your Arduino Uno R3 to your computer.
- Select the correct board.
- Select the correct port.
- Click Verify to compile the sketch.
- If there are no errors, click Upload.
- Wait for the upload to complete.
- Observe the LED connected to D9.
Expected Result
After the program has been uploaded, the LED should initially blink rapidly.The blinking should then gradually become slower as the delay increases.
After reaching the maximum delay, the program resets the delay and the LED starts blinking rapidly again.
The complete pattern is:
Fast → Gradually slower → Slowest → Reset → Fast → Repeat
Experiment 1: Make the LED Tire More Quickly
Try changing:blinkDelay = blinkDelay + 100;
to:
to:
blinkDelay = blinkDelay + 200;
The delay will increase more quickly, so the LED will become "tired" faster.
The delay will increase more quickly, so the LED will become "tired" faster.
Experiment 2: Make the LED Tire More Slowly
Try changing:blinkDelay = blinkDelay + 100;
to:
to:
blinkDelay = blinkDelay + 25;
The delay will increase more gradually.
Observe how this changes the effect.
The delay will increase more gradually.
Observe how this changes the effect.
Experiment 3: Change the Maximum Delay
The program currently contains:if (blinkDelay > 2000) {
blinkDelay = 100;
}
Try changing 2000 to: 5000
The LED will take much longer to reach its slowest blinking speed.
Try changing 2000 to: 5000
The LED will take much longer to reach its slowest blinking speed.
Challenge
Can you modify the program so that the LED:- Starts blinking quickly.
- Gradually slows down.
- Remains stopped for two seconds.
- Starts blinking quickly again.
- Repeats the entire sequence.
Troubleshooting
The LED does not blink
Check:- The LED polarity.
- The connection between D9 and the resistor.
- The connection between the LED cathode and GND.
- The USB connection.
- The selected Arduino board.
- The selected port.
- Whether the sketch uploaded successfully.
The LED stays ON
Check that your program contains:digitalWrite(ledPin, LOW);
and that the LED is connected correctly.
and that the LED is connected correctly.
The LED blinks but does not slow down
Make sure the program contains:blinkDelay = blinkDelay + 100;
and that blinkDelay is being used in both delay() statements.
What You Learned
In this project, you learned how a variable can be used to control the timing of an Arduino program.You learned:
- How digitalWrite() controls the LED.
- How delay() controls timing.
- How a variable can store a changing delay value.
- How to increase a variable during program execution.
- How an if statement can reset a variable.
- How changing timing can create different physical effects.
In the previous project, we changed the LED's apparent brightness using PWM.
In this project, we change the time interval between LED states.
Going Further
The program in this project uses delay() because it is simple and easy to understand for beginners.However, delay() pauses the execution of the program while it waits. In larger Arduino projects, you may want the Arduino to perform other tasks while keeping track of time.
A useful next step is learning how to use the millis() function to create timing without blocking the rest of the program.
This technique becomes especially useful when working with sensors, buttons, motors, displays, and multiple LEDs.
Conclusion
In this project, we created a simple Tired LED effect.The LED begins by blinking quickly and gradually slows down as the program increases the delay between its ON and OFF states. Once the maximum delay is reached, the program resets the delay and starts the cycle again.
Although the project is simple, it introduces an important programming concept:
A variable can be changed while a program is running to control the behavior of physical hardware.
This concept will become increasingly useful as we move toward more advanced Arduino projects.
What's Next?
We've now learned how to control an LED's:- State - Blink an LED
- Brightness - Fade an LED
- Timing - Tired LED
Follow RRRTechSolv for more Arduino, electronics, robotics, and embedded-systems projects.


Comments
Post a Comment