Arduino PWM Tutorial: Basics, Applications & Techniques

Table of Contents

Understanding PWM🔗

Pulse Width ModulationPractical Examples: Controlling LED BrightnessPractical Examples: Controlling LED BrightnessLearn to adjust LED brightness using Arduino PWM techniques. This practical guide covers hardware setup, code examples, and troubleshooting tips. (PWM) simulates analog voltage levels using rapid digital switching. The duty cycleWhat is PWM?What is PWM?Explore the fundamentals of PWM in Arduino. Discover essential theory, practical tips, and real-world applications to enhance your projects. (percentage of HIGH time per cycle) determines effective voltage outputUnderstanding Digital Signals and PinsUnderstanding Digital Signals and PinsExplore our complete Arduino guide on digital signals and pins, featuring hands-on examples and expert tips for reliable projects.:

Effective Voltage = (Duty Cycle / 255) × 5V  // For 8-bit Arduino

This energy-efficient method is ideal for controlling LEDsYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily., motors, and other analog-like behaviors.

Arduino's PWM Capabilities🔗

PWMPractical Examples: Controlling LED BrightnessPractical Examples: Controlling LED BrightnessLearn to adjust LED brightness using Arduino PWM techniques. This practical guide covers hardware setup, code examples, and troubleshooting tips. availability varies by board:

BoardPWM PinsBase FrequencyResolution
Uno/Nano3,5,6,9,10,11490Hz/976Hz8-bit
Mega2-13, 44-46490Hz8-bit
ESP32All digital pins1kHz-40MHz16-bit

Core Implementation: analogWrite()🔗

ArduinoWhat is Arduino? A Comprehensive OverviewWhat is Arduino? A Comprehensive OverviewDive into the world of Arduino with our in-depth guide covering hardware, software, and community projects ideal for students, hobbyists, and educators. abstracts PWM complexity with:

analogWrite(pin, value);  // Value: 0-255

Key Insight: This works only on PWM-capable pinsDigital Pins and LogicDigital Pins and LogicExplore our comprehensive Arduino guide on digital pins and logic. Learn configuration, wiring, troubleshooting, and practical applications. (marked with ~ on Uno/Nano).

Practical Applications🔗

LED Intensity Control

CircuitYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily.:

Breathing LED CodeYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily.:

const byte ledPin = 9;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // Fade in (0 → 255)
  for(int i=0; i<=255; i++) {
    analogWrite(ledPin, i);
    delay(10);
  }
  // Fade out (255 → 0)
  for(int i=255; i>=0; i--) {
    analogWrite(ledPin, i);
    delay(10);
  }
}

Motor Speed Regulation

Essential Components:

CircuitYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily.:

void setup() {
  pinMode(5, OUTPUT);  // 976Hz pin
}
void loop() {
  analogWrite(5, 77);  // 30% speed (77/255 ≈ 1.5V)
  delay(3000);
  analogWrite(5, 255); // Full speed
  delay(3000);
}

Advanced Techniques🔗

Frequency Customization

Modify PWM frequencyWhat is PWM?What is PWM?Explore the fundamentals of PWM in Arduino. Discover essential theory, practical tips, and real-world applications to enhance your projects. using timer registers (Uno example):

// Set Timer1 (pins 9,10) to 31.25kHz
void setup() {
  TCCR1B = (TCCR1B & 0b11111000) | 0x01;
  pinMode(9, OUTPUT);
}
void loop() {
  analogWrite(9, 128);  // 50% duty cycle
}

Applications:

H-Bridge Motor Control

Combine PWMPractical Examples: Controlling LED BrightnessPractical Examples: Controlling LED BrightnessLearn to adjust LED brightness using Arduino PWM techniques. This practical guide covers hardware setup, code examples, and troubleshooting tips. with direction control:

const byte pwmPin = 5;
const byte in1 = 7, in2 = 8;
void setup() {
  pinMode(pwmPin, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}
void loop() {
  // Forward at 70% speed
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(pwmPin, 178);  // 178 ≈ 70%
  delay(2000);
  // Reverse at 40% speed
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(pwmPin, 102);  // 102 ≈ 40%
  delay(2000);
}

Troubleshooting Guide🔗

SymptomLikely CauseSolution
No responseWrong pinUse PWM-capable pins
Flickering at low dutyLow frequencyIncrease PWM frequency
Motor vibrationInsufficient duty cycleMaintain >20% duty cycle
Timer conflictsShared timer resourcesReassign pins
Electrical noiseHigh-frequency interferenceAdd RC filter to circuit

Pro Tips:

Conclusion🔗

PWM transforms digital outputs into analog-like control systems. From creating mood lighting effects to building sophisticated robotics, mastering PWMPractical Examples: Controlling LED BrightnessPractical Examples: Controlling LED BrightnessLearn to adjust LED brightness using Arduino PWM techniques. This practical guide covers hardware setup, code examples, and troubleshooting tips. unlocks:

Whether you're dimming LEDs or controlling industrial actuators, PWM remains an essential tool in your embedded systems toolkit. Experiment with different frequencies and duty cyclesWhat is PWM?What is PWM?Explore the fundamentals of PWM in Arduino. Discover essential theory, practical tips, and real-world applications to enhance your projects. to discover its full potential!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles