Programming: Controlling a LED with PWM
- Daniel Van Nattan
- Sep 22, 2023
- 3 min read
Pulse Width Modulation (PWM) is a technique used in electronics and microcontroller programming to control the intensity or brightness of an LED (Light Emitting Diode) or the speed of a motor. In this tutorial, we'll focus on using PWM to control the brightness of an LED using a popular microcontroller platform, the Arduino. However, the concepts discussed here are applicable to other microcontrollers as well.
Prerequisites
Before we get started, make sure you have the following:
1. An Arduino board (e.g., Arduino Uno, Arduino Nano, etc.).
2. A single LED (any color).
3. A current-limiting resistor (usually 220-330 ohms) for the LED.
4. Jumper wires.
5. Arduino IDE installed on your computer.
Understanding PWM
PWM works by rapidly switching a digital output on and off at varying duty cycles to simulate an analog voltage. This technique allows us to control the average voltage supplied to an LED, which, in turn, controls its brightness. The higher the duty cycle (percentage of time the signal is high), the brighter the LED will be.
Wiring the Components
1. Connect the anode (longer lead) of the LED to one end of the current-limiting resistor.
2. Connect the other end of the resistor to one of the digital pins on your Arduino board (e.g., Pin 9).
3. Connect the cathode (shorter lead) of the LED to the Arduino's ground (GND) pin.
4. Finally, power up your Arduino board by connecting it to your computer via USB.
Writing the Arduino Code
Now, let's write the Arduino code to control the LED's brightness using PWM. Follow these steps:
1. Open the Arduino IDE on your computer.
2. Start a new sketch by selecting `File > New`.
3. Copy and paste the following code into the Arduino IDE:
const int ledPin = 9; // Define the pin connected to the LED
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Fade in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Delay for smoother fade
}
// Fade out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Delay for smoother fade
}
}4. Save your sketch by selecting `File > Save` and give it a name, e.g., "LED_PWM_Control."
5. Verify your code for any errors by clicking the checkmark icon in the Arduino IDE.
6. Once verified, upload the code to your Arduino board by clicking the right arrow icon.
Understanding the Code
Here's what the code does:
- We define `ledPin` as the digital pin to which the LED is connected.
- In the `setup` function, we set `ledPin` as an output.
- In the `loop` function, we create two loops. The first loop increases the LED brightness from 0 to 255 (PWM range), and the second loop decreases the brightness from 255 back to 0.
- We use the `analogWrite` function to set the LED brightness, which is a PWM signal.
- We add a small delay (`delay(10)`) to create a smoother fade effect.
Uploading and Testing
After uploading the code to your Arduino, you should see your LED fading in and out continuously. The LED will gradually get brighter and then dimmer in a repeating pattern.
Congratulations! You've successfully controlled an LED's brightness using PWM with an Arduino.
Feel free to experiment by changing the pin number, LED colors, or the timing in the code to create different effects or control multiple LEDs simultaneously. PWM is a powerful tool for controlling various components in your projects, and this tutorial provides a solid foundation to start exploring its capabilities further. For more information view this tutorial.
Comments