• nithya7rns

Fading LED - IN & OUT using Arduino UNO

Light Dimmer circuits are nowadays used in Home #Automation systems to allow Fading IN and OUT of lights based on movements in a particular place. The movements are tracked by motion sensors, that is, when a person moves around a particular place, the motion sensors pick up the reading of entry and exit, and based on that, the LED brightens up or gets dimmed according to the conditions given.

In this article, we will be discussing #Fading LED - IN and OUT using Arduino UNO and writing code for the same. Let's start with the list of Hardware required for the project.

Hardware Required

  1. Arduino UNO

Arduino UNO

Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be that from light-detecting sensors, flame sensors, temperature sensors, etc. The output function done on the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.

Buy Arduino UNO from here.

2. LED


Light Emitting Diode is a commonly used light source. It is a semiconductor. LED emits light when current flows through it.

Buy LED from here.

3. Breadboard


The breadboard is the basic component of any circuit building process. All components, be it input sensors or output display devices are connected to the #Arduino or any other microcontroller using wired connections through a breadboard. The holes in the breadboard are in series. There are various sizes like full-sized, half-sized, and mini breadboards.

Buy Breadboard from here.

4. Jumper Wires

Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.

Buy Jumper Wires from here.

Circuit Connections

The given circuit diagram shows the necessary connections.

Circuit Diagram

The connection for this circuit is simple. The positive terminal of the LED is connected to the PIN 9 of the Arduino UNO board. The red Jumper wire shows the same. The negative terminal of the LED is connected to the Ground(GND) pin of the Arduino UNO board. The second connection is shown by the Black Jumper wire.

The Arduino #UNO board is then connected to the system which has the IDE and the code is written for the circuit to work.


In this code, we will be declaring variables as #Universal constants which means that the value assigned to those variables will remain the same throughout.

int led = 9; 

First, the variable used to denote the LED pin is initialized.

int brightness = 0; 

The brightness level is initialized to 0. This denotes how bright the LED's current's brightness is.

int fadeAmount = 5; 

The brightness level is increased in terms of fadeAmount/ points and so the variable name. The value given here is 5 which means in increments of 5, the Fade IN and Fade OUT is going to take place.

void setup() {
  pinMode(led, OUTPUT);

The setup( ) function is going to assign the led connected as OUTPUT. The pinMode function is used to assign the LED connected as output.

void loop() {
  analogWrite(led, brightness);

The loop( ) function is going to repeat the lines of code inside it more than one time until the condition to stop the loop is given. The line "analogWrite(led, brightness);" is actually using a digital pin as one of its parameters, that is 'led', for an #analog function. This means that digital pins can also be used for analog input/output.

The parameter led and brightness as passed to the analogWrite( ) functions which denote the LED PIN and the brightness level respectively.

brightness = brightness + fadeAmount;

The value of brightness is incremented every time the loop runs by adding the value of fadeAmount which is initially 5 along with the current brightness level.

if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;

An "if" condition is written to check the value of the brightness. If the brightness values are equal to 0 OR 255, the fadeAmount is replaced by the negative value of it. For example, if the fadeAmount is 255, it is replaced by -255 and the loop continues.


A delay .03 seconds is given and the loop( ) function ends. But the loop( ) function is executed until the given condition runs out of the values specified.


int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
  pinMode(led, OUTPUT);

void loop() {
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;

For the complete code for this program, download the following document.

Download TXT • 410B

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.


The working of the circuit is easy to understand.

Circuit Switched OFF

When the connections are done and the system is connected to the circuit, the LED is initially OFF as it is initialized with '0' brightness. The LED is set to Output mode. The positive terminal of the LED receives Supply (voltage) from the Arduino board and starts working.

Circuit Switched ON

As per the delay given, after every particular amount of time, the LED continuously Fades IN and Fades out. As explained in the code, the LED brightness reaches the maximum value of 255 and is returned to the initial value. It again Fades IN in increments of 5 points and Fades OUT in the same way.

Do watch the video to know the practical construction and working of the circuit.

Project video By - Abhishek Goud

This device is implemented on larger scales with more independence. It means that the controlling of the LED brightness need not be the same for all conditions. The involvement of IoT and #AI has increased its application in various fields. Try giving different conditions, attach sensors to take input values, and control the LED brightness depending upon the same. Start Exploring!!


  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
Contact Us
© Copyright - LearnElectronics 2020.