Arduino PWM Tutorial: Step-by-Step Dimming and Control Pulse Width Modulation (PWM) is a powerful technique used in electronics to control the amount of power delivered to a device. In the world of Arduino, PWM allows you to simulate analog outputs using digital pins. This tutorial will guide you through the fundamentals of PWM and show you how to implement it for LED dimming and motor control. Understanding PWM: How It Works
Digital microcontrollers like the Arduino can only output two states: HIGH (5V or 3.3V) and LOW (0V). However, many components require varying amounts of voltage to operate smoothly, such as a dimming light or a variable-speed motor.
PWM solves this by flipping the digital pin between HIGH and LOW states at a very high frequency. This rapid switching happens so fast that the human eye or an electrical component perceives it as a steady, intermediate voltage level. Key Concepts
Duty Cycle: The percentage of time the signal remains HIGH during one complete cycle. A 50% duty cycle means the signal is on half the time and off half the time.
Average Voltage: The effective voltage delivered to the component. You calculate this by multiplying the maximum voltage by the duty cycle percentage (e.g., a 50% duty cycle at 5V yields an average of 2.5V).
Frequency: how many times the signal cycles between HIGH and LOW per second, measured in Hertz (Hz). Arduino PWM frequencies typically range from 490 Hz to 980 Hz. Identifying PWM Pins on Arduino
Not all digital pins on an Arduino board support PWM. You can easily identify PWM-capable pins by looking for a tilde symbol () printed next to the pin number on the board layout. Arduino Uno / Nano: Pins 3, 5, 6, 9, 10, and 11. Arduino Mega: Pins 2 through 13 and 44 through 46. The analogWrite() Function
To control PWM pins in your code, Arduino provides a dedicated function called analogWrite(). Unlike digitalWrite(), which only accepts HIGH or LOW, analogWrite() accepts a value ranging from 0 to 255.
analogWrite(pin, 0) represents a 0% duty cycle (Always OFF).
analogWrite(pin, 127) represents a 50% duty cycle (Half power).
analogWrite(pin, 255) represents a 100% duty cycle (Always ON). Step-by-Step Project: LED Dimming
This practical example demonstrates how to gradually fade an LED in and out using PWM. Required Components Arduino Uno or compatible board One 220-ohm resistor Breadboard and jumper wires Wiring Circuit
Connect the longer leg (anode) of the LED to Arduino Digital Pin 9 (a PWM pin).
Connect the shorter leg (cathode) of the LED to one end of the 220-ohm resistor.
Connect the other end of the resistor to the Arduino GND pin. Example Code
// Define the PWM pin connected to the LED const int ledPin = 9; void setup() { // PWM pins do not strictly require pinMode configuration for analogWrite, // but keeping it is good practice for code clarity. pinMode(ledPin, OUTPUT); } void loop() { // Fade inside from dark to bright for (int brightness = 0; brightness <= 255; brightness++) { analogWrite(ledPin, brightness); delay(10); // Small delay to make the transition visible } // Fade outside from bright to dark for (int brightness = 255; brightness >= 0; brightness–) { analogWrite(ledPin, brightness); delay(10); } } Use code with caution. Expanding Control: Motor Speed Regulation
PWM principles apply directly to DC motor control. Because DC motors draw more current than an Arduino pin can safely supply, you must use a transistor (like a TIP120) or a motor driver chip (like the L298N) as an intermediary.
By passing a PWM signal from the Arduino to the base or enable pin of your driver, you can easily control the rotation speed of a fan or robot wheel using the exact same analogWrite() logic demonstrated above. Common Troubleshooting Tips
LED Flickers Instead of Dimming: Verify that you connected the LED to a pin marked with a tilde (). Standard digital pins will simply blink rapidly instead of fading.
LED Stays Bright: Double-check your loop code variables. Ensure your math boundaries stay strictly within the 0 to 255 limit. Values outside this window wrap around and cause erratic behaviors.
If you want to take this project further, let me know if you would like to: Learn how to control the dimming with a potentiometer knob Adapt this code for an RGB LED to mix custom colors See a circuit schematic for DC motor speed control
Leave a Reply