This post shows how to use Pulse Width Modulation PWM on an Arduino microcontroller to regulate the brightness of an LED.
PWM is a technology that successfully controls the average output voltage by varying the square waves duty cycle.
We can alter the brightness of the LED by varying the duty cycle, which modifies the amount of power sent to the light source.
Coding with Explanation:
#define LED_PIN 9
#define BUTTON1_PIN 2
#define BUTTON2_PIN 3
int brightness = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
}
void loop() {
if (digitalRead(BUTTON1_PIN) == LOW)
{
brightness++;
if (brightness > 255) {
brightness = 255;
}
}
if (digitalRead(BUTTON2_PIN) == LOW) {
brightness--;
if (brightness < 0) {
brightness = 0;
}
}
analogWrite(LED_PIN, brightness);
delay(10);
}
- The Arduino pins linked to the LED and the two push buttons are identified by the constants LED_PIN, BUTTON1_PIN and BUTTON2_PIN.
- Brightness in the LEDs current brightness level is stored in this variable.
- setup() function sets the button pins as inputs with built -in pull-up resistors and the LED pin as an output.
- loop() method keeps an eye on the push buttons conditions all the time.
- The brightness increases when BUTTON1 is pressed and decreases when BUTTON2 is pressed.
- The PWM duty cycle on the LED pin is set using the analog Write() method, which regulates the LEDs brightness.
Circuit Working:
Parts List:
Component | Quantity |
---|---|
Arduino Uno board | 1 |
LED 5mm 20mA (any color) | 1 |
100µF 25V capacitor | 1 |
220Ω resistor | 1 |
10k resistor | 2 |
Tactile switches | 2 |
The LED should be connected correctly, as per the above the circuit diagram.
The push buttons might not be stable, but it wont pass any errors.
It is easy to use PWM on Arduino setting up PWM on ATMEGA is harder, as you need to adjust many settings.
Arduino already has these settings ready, so you can use PWM easily by calling the right functions.
On the LED pin, the Arduino microcontroller produces a square wave signal.
The resistors and capacitor filter the square wave to create a DC voltage.
The average DC voltage, which in turn regulates the LEDs brightness, is found in the duty cycle of the square wave.
The LED brightness may be adjusted by increasing or decreasing the duty cycle using the push buttons.
When we press button1 the LED illumination increases and when button 2 is pressed the LED illumination decreases
How to Build:
To build a Simple Arduino LED Dimmer Circuit using PWM follow the below mentioned steps:
- Gather all the components mentioned in the above circuit diagram.
- Connect a regulated IC1 7809 to provide a regulated 9V DC to the Arduino board
- Connect capacitor 100μF positive side between resistor 220Ω and red LED and negative side to the GND.
- Connect 220Ω resistor and Red LED in series from pin 9 on the Arduino board and cathode of LED to ground.
- Connect Tactile switch button1 one leg from pin 2 on the Arduino board and other leg to ground.
- Connect Tactile switch button2 one leg from pin 3 on the Arduino board and other leg to ground.
- Connect one 10k resistor between pin 2 and positive 5V on Arduino board.
- Connect other 10k resistor between pin 3 and positive 5V on Arduino board.
Conclusion:
This article shows how to use PWM on an Arduino to regulate an LEDs brightness in a simple yet efficient manner.
You may develop a variety of lighting control applications by comprehending the PWM concepts and the code implementation.