Arduino

Create an LED Dimmer using Arduino

Arduino is a programmable microcontroller board that uses both hardware and software to create electrical projects. You can use Arduino to create an LED Dimmer by using a resistor. This tutorial will make you capable of doing that.

How to Create an LED Dimmer?

You can create an LED Dimmer by completing the following requirements:

  • Arduino IDE
  • Arduino Board
  • An LED
  • A Resistor (220 Ohms)

First, you should make a code for a dimmer LED in the Arduino IDE. Here is the example code. You can modify it according to your requirements. After coding, you will upload this code to the Arduino board where you would have made the circuit as described below.

const int ledPin = 9;  //defining LED pin as const int
int ledState = LOW;  // ledState will the LED initially to LOW

unsigned long previousMillis = 0;  // previousMillis is defined as unsigned long as it will store the time when LED changes state
const long interval = 1000;  //interval is selected to make the LED blink regularly

void setup() {
  //Set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
   //Check to see if it's time to blink the LED; that is if the difference
  // between the current time and the last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)

{

    //Save the last time you blinked the LED
    previousMillis = currentMillis;
    //If the LED is off turn it on and vice versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    } else
    {
      ledState = LOW;
    }
    //Set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

You can compile this code in Arduino IDE or an online compiler, and then upload it to the Arduino UNO board that will be connected to the circuit. Make connections according to the described pin connections.

Pin Connections

  1. Connect the LED input pin or LED anode that is slightly curved to the resistor, and then connect the other terminal of the resistor to pin 9 of the LED.
  2. Connect the GND pin of the LED to the pin of the Arduino UNO board that is marked GND.

Hardware Circuit Diagram

After making the required connections, you can run the circuit by uploading code to the Arduino UNO and supplying power to it. You will see the LED blinking like a dimmer.

Conclusion

It is very easy to create an LED dimmer in Arduino using a simple LED and a resistor. A timer is set in Arduino that makes the LED blink without delay, and it keeps turning ON and OFF consecutively. The code uses the millis() function to set the timer. The millis() is an inbuilt function of Arduino.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.