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.
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
- 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.
- 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.