Arduino Fading LED (Analog PWM Output)

In this project tutorial, we’ll create an Arduino Fading LED Project using PWM (analog output). You’ll learn how PWM works, and how to create a LED Fading effect with Arduino (Fade-in and Fade-out effects). And we’ll simulate and run the project code example to test its functionality. Without further ado, let’s get right into it!

Table of Contents

  1. LED Fading With Arduino
  2. Fading LED Project Wiring Diagram
  3. Arduino Code – LED Fading
  4. Fading LED Project Simulation
  5. LED Fading Project Testing
  6. Project Wrap Up

LED Fading With Arduino

To create the Arduino Fading LED project, we need the following components:

  • Arduino Board
  • LED
  • Resistor (330Ω)
Arduino-Fading-LED

The Arduino Fading LED Effect is based on the Arduino PWM output signal that’s used to control the LED brightness and gradually increase and decrease its duty cycle to achieve the LED Fading effect.

Arduino boards have several PWM output pins usually. Those pins are designated with a (~) mark next to the pin number on the board. Before discussing how to use the PWM output pins, let’s first define what is the PWM technique and what are the properties of a PWM signal.

Pulse Width Modulation (PWM) is a technique for generating a continuous HIGH/LOW alternating digital signal and programmatically controlling its pulse width and frequency. Certain loads like (LEDs, Motors, etc) will respond to the average voltage of the signal which gets higher as the PWM signal’s pulse width is increased.

As you can see, the LED gets brighter as the pulse width (duty cycle) increases, and it gets dimmer as the pulse width decreases. And this is typically what we use the PWM output for.

Check the tutorial below to learn more about Arduino PWM. It’s a prerequisite for this project to help you understand the topic in more detail.

???? Also Read
Arduino PWM analogWrite Tutorial

This tutorial will provide you with more in-depth information about Arduino PWM output. Starting from the basics of PWM signal, its frequency, duty cycle, and resolution, and will discuss in detail how it works and how to use it in various Arduino control projects.


Fading LED Project Wiring Diagram

Here is the wiring diagram for this example showing how to connect the LED output and the oscilloscope channel.

Arduino-PWM-LED-Fading-Example-Wiring


Arduino Code – LED Fading

In this example project, we’ll control an LED brightness with an Arduino PWM output pin. Using two for loops, we’ll gradually increase the duty cycle from 0% up to 100%. Then, gradually decrease it from 100% down to 0%. And keep repeating!

Here is the full code listing for this example.

Code Explanation

First of all, we define the IO pin used for the LED output. It has to be a PWM-enabled IO pin (for UNO: 3, 5, 6, 9, 10, or 11).

setup()

in the setup() function, we’ll set the pinMode to be output.

loop()

in the loop() function, we’ll create 2 for loops. The first for loop will gradually increase the output PWM’s duty cycle with a small 5ms delay time after every PWM duty cycle update operation. And that’s the Fade-in Effect implementation.

The second loop will gradually decrease the output PWM’s duty cycle from 100% down to 0%. Which is the Fade-out Effect implementation. And everything will keep repeating forever.


Fading LED Project Simulation

We can test this project’s code example using any available Arduino simulator environment. Here I’ll show you the simulation results for this project on both TinkerCAD and Proteus (ISIS).

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator. You can run it as is, or make a copy and add your own code and start running the simulation to see how it’s going to behave.

You can check this simulation project on TinkerCAD using this link.

Proteus (ISIS) Simulation

Another extremely powerful simulation environment for Arduino is the Proteus (ISIS) with Arduino add-on library. It can definitely run our test projects for this tutorial. But you won’t feel its power until you need some virtual test equipment like an oscilloscope, function generator, power supply, and advanced SPICE simulation for auxiliary electronic circuitry that you may build around an Arduino microcontroller.

Here is the Arduino project simulation result from the Proteus environment.

Check out the tutorial below to help you get started with simulating your Arduino projects in the Proteus simulation environment.

???? Also Read
Arduino Proteus Library Simulation Guide

This article will provide you with more in-depth information about using proteus ISIS for Arduino projects simulation.


LED Fading Project Testing

Here is a demo video for testing this project on my Arduino UNO board.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Project Wrap Up

To conclude this project tutorial, we can say that you can easily perform Fading LED Effects with Arduino PWM output using the analogWrite() function and a PWM output pin. To learn more about Arduino PWM, it’s highly recommended that you check out this Arduino PWM Tutorial. And check the Arduino projects page for more project ideas and code examples.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

For more projects & ideas, check this Arduino Projects resource page.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

How to make Arduino Fading LED?

To make an Arduino Fading LED project, you need to use a PWM output pin.
1- Set it as an output pin using the pinMode function.
2- Make a for() loop to gradually increase the PWM’s duty cycle “Fade-in Effect”.
3- Make another for() loop to gradually decrease the PWM’s duty cycle “Fade-out Effect”.

How to fade multiple LEDs With Arduino?

To fade multiple LEDs with Arduino PWM output, you’ll need to use a switching device (like a BJT transistor or a MOSFET). This will help you drive a lot of LEDs all at once with the same PWM output pin from the Arduino. Note that you can’t drive multiple LEDs using only 1 IO pin from the Arduino. Because the pin can’t deliver more than 40mA and it’s going to be damaged if you over-drive it, and that’s why we need to use a transistor to drive multiple LEDs.

Why is my LED light so dim Arduino?

The LED light can be so dim due to using a high current-limiting resistor. You typically need to use a 220Ω or 330Ω resistors for this purpose. Another reason can be you’re connecting the LED to the wrong pin on the Arduino board, so double-check your connections. Sometimes it can be due to the fact that the LED’s ground lead is not properly connected, so make sure that the wiring is correct. Finally, you can test the LED without using Arduino IO pins to make sure that it’s not a damaged LED.

How Do You Fade LEDs Using PWM?

To Fade LEDs using PWM, you’ll have to gradually increase the PWM output’s duty cycle (Fade in effect). Then, gradually decrease the PWM’s duty cycle (Fade out effect). You can use two loops in Arduino alongside the analogWrite() function to achieve the LED Fading Effect.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment