Master Pulse Width Modulation and analog output control
🎛️ Learn to control brightness, speed, and analog devices
Pulse Width Modulation (PWM) is a technique used to simulate analog output using digital signals. Instead of providing a continuous voltage like true analog output, PWM rapidly switches between HIGH (5V) and LOW (0V) states.
Arduino Uno has 6 PWM-capable pins: 3, 5, 6, 9, 10, and 11 (marked with ~ symbol). These pins can output PWM signals with values from 0 (0% duty cycle) to 255 (100% duty cycle).
The analogWrite() function generates PWM signals on compatible pins. It takes two parameters: the pin number and the PWM value (0-255).
analogWrite(pinNumber, pwmValue) Generates PWM signal on specified pin (0-255 range)
One of the most common uses of PWM is controlling LED brightness. Here's a complete example that gradually fades an LED in and out:
Wiring: Connect the LED's long leg (anode) to pin 9 through a 220Ω resistor, and the short leg (cathode) to GND.
// LED Fading Example
const int ledPin = 9; // PWM pin
int brightness = 0; // LED brightness
int fadeAmount = 5; // How much to change brightness
void setup() {
Serial.begin(9600);
}
void loop() {
// Set LED brightness
analogWrite(ledPin, brightness);
// Print current brightness
Serial.print("Brightness: ");
Serial.println(brightness);
// Change brightness for next iteration
brightness = brightness + fadeAmount;
// Reverse direction at the ends
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30); // Small delay for smooth fading
} RGB LEDs contain three separate LEDs (Red, Green, Blue) that can be controlled independently using PWM. By mixing different intensities of these colors, you can create millions of colors.
Before running the RGB LED code, you need to connect an RGB LED properly:
If your expansion board has a built-in RGB LED, check its pin connections and modify the pin numbers in the code accordingly.
RGB LED Wiring: Red → Pin 9, Green → Pin 10, Blue → Pin 11 (each with 220Ω resistor)
Here's an example that cycles through different colors on an RGB LED:
// RGB LED Color Cycling
const int redPin = 9; // Red LED pin
const int greenPin = 10; // Green LED pin
const int bluePin = 11; // Blue LED pin
void setup() {
Serial.begin(9600);
Serial.println("RGB LED Color Demo");
}
void loop() {
// Red
setColor(255, 0, 0);
Serial.println("Red");
delay(1000);
// Green
setColor(0, 255, 0);
Serial.println("Green");
delay(1000);
// Blue
setColor(0, 0, 255);
Serial.println("Blue");
delay(1000);
// Purple
setColor(255, 0, 255);
Serial.println("Purple");
delay(1000);
// Off
setColor(0, 0, 0);
Serial.println("Off");
delay(1000);
}
// Function to set RGB color
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
} Create a program that demonstrates PWM control using the Serial Monitor as a control interface. This kit-independent activity will help you understand PWM principles through simulation and code.