Master PWM in Arduino

Master PWM in Arduino

This chapter introduces Pulse Width Modulation  PWM in Arduino, a technique used in Arduino to control the power delivered to devices like LEDs and motors. We will cover how to use analogWrite() to generate PWM signals, manage frequency, duty cycle, and which pins support PWM.

Syntax Table: PWM in Arduino

Topic Syntax Simple Example
analogWrite() analogWrite(pin, value); analogWrite(9, 128); (50% duty cycle)
PWM Frequency analogWrite(pin, value); analogWrite(9, 255); (100% duty cycle)
PWM Duty Cycle analogWrite(pin, value); analogWrite(9, 64); (25% duty cycle)
PWM Pins analogWrite(pin, value); analogWrite(9, 128); (Use PWM pins)

analogWrite() – How to Write PWM Value to a Pin in Arduino

 analogWrite()?
analogWrite() is a function in Arduino that allows you to output a Pulse Width Modulation (PWM) signal on digital pins. This is commonly used to control devices like LEDs, motors, and other components that require variable power. It mimics an analog output using a digital pin, which can be useful for tasks like controlling the brightness of LEDs or adjusting the speed of motors.

Use purpose
The analogWrite() function is typically used for:

  • Controlling LED brightness: Dimming or brightening an LED with smooth transitions.
  • Adjusting motor speed: Gradually increasing or decreasing the speed of DC motors or similar devices.
  • Simulating analog output: Creating varying power levels to control components.

It works by sending a PWM signal, which controls the amount of time the pin stays “on” versus “off” in a fast cycle.

Arduino Syntax use
analogWrite(pin, value);

Arduino Syntax Explanation

  • pin: The pin number where the PWM signal is output. Ensure the pin supports PWM.
  • value: A number between 0 and 255, where 0 means 0% power (off), and 255 means 100% power (fully on).

Arduino code Example

int ledPin = 9; // Pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set pin 9 as output
}
void loop() {
  analogWrite(ledPin, 128); // Set LED to 50% brightness
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 255); // Set LED to full brightness
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 0);   // Turn off the LED
  delay(1000);              // Wait for 1 second
}

Code Explanation

  • pinMode(ledPin, OUTPUT);: Configures pin 9 as an output pin, allowing it to control the LED.
  • analogWrite(ledPin, 128);: Sends a PWM signal to pin 9, setting the LED brightness to 50%.
  • analogWrite(ledPin, 255);: Sets the LED brightness to full power (100%).
  • analogWrite(ledPin, 0);: Turns off the LED completely.
  • delay(1000);: Pauses the program for 1 second before adjusting the brightness again.

Notes

  • The PWM signal is a digital signal that simulates different levels of power by rapidly turning the pin on and off. The value (0-255) controls how much of each cycle the pin is on.
  • Make sure the pin supports PWM output. PWM-enabled pins are typically marked with a “~” on the Arduino board.
  • The frequency of the PWM signal is typically around 490 Hz on most pins, but this may vary based on the specific pin and Arduino model you are using.

PWM Frequency – Frequency of the PWM Signal in Arduino

 PWM Frequency?
PWM Frequency refers to the number of cycles per second that the Pulse Width Modulation (PWM) signal oscillates between its high and low states. The frequency of the PWM signal determines how fast the signal switches on and off. In Arduino, the default PWM frequency varies depending on the pin and board being used.

Use purpose
The PWM frequency is crucial for:

  • Controlling the behavior of components: High-frequency PWM signals are essential for smooth operation in applications like motor speed control and LED brightness control.
  • Preventing noise in motors: Lower PWM frequencies can produce audible noise in motors, so adjusting the frequency can help.
  • Avoiding flicker in LEDs: A higher PWM frequency helps prevent visible flicker in LEDs, ensuring smooth dimming effects.

Arduino Syntax use
analogWrite(pin, value);

Arduino Syntax Explanation

  • pin: The pin number where the PWM signal is generated. Only certain pins support PWM.
  • value: An integer between 0 and 255. The frequency is fixed for each PWM pin, but the duty cycle (the percentage of the signal’s time spent in the high state) is determined by the value.

Arduino code Example

int ledPin = 9; // Pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set pin 9 as output
}
void loop() {
  analogWrite(ledPin, 128); // Set PWM duty cycle to 50%
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 255); // Set PWM duty cycle to 100%
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 0);   // Set PWM duty cycle to 0% (off)
  delay(1000);              // Wait for 1 second
}

Code Explanation

  • pinMode(ledPin, OUTPUT);: Configures pin 9 as an output pin to control the LED.
  • analogWrite(ledPin, 128);: Sends a PWM signal with a 50% duty cycle, which means the pin is on half of the time.
  • analogWrite(ledPin, 255);: Sends a PWM signal with a 100% duty cycle, meaning the pin is fully on.
  • analogWrite(ledPin, 0);: Sends a PWM signal with a 0% duty cycle, turning off the pin completely.
  • delay(1000);: Waits for 1 second before changing the PWM signal.

Notes

  • The default PWM frequency on most Arduino boards is about 490 Hz for pins 3, 9, 10, and 11, while pins 5 and 6 run at 980 Hz.
  • Changing the frequency of PWM requires modifying the timer settings for the specific pins, which can be advanced for beginners.
  • Be cautious when changing the PWM frequency, as it might affect the behavior of connected components like motors and LEDs.
  • Not all Arduino pins support PWM; check your specific board documentation for details.

PWM Duty Cycle – Percentage of Time the Signal is High in Arduino

 PWM Duty Cycle?
The PWM Duty Cycle refers to the percentage of time the Pulse Width Modulation (PWM) signal stays in the “high” state (on) during each cycle. For example, a 50% duty cycle means the signal is on half the time and off half the time, while a 100% duty cycle means the signal is always on. The duty cycle directly affects the output, such as the brightness of an LED or the speed of a motor.

Use purpose
The PWM duty cycle is important for:

  • Controlling power: Adjusting the power output to components like LEDs and motors.
  • Adjusting brightness: By controlling the duty cycle, you can smoothly dim or brighten an LED.
  • Regulating motor speed: Varying the duty cycle controls the speed of DC motors.

Arduino Syntax use
analogWrite(pin, value);

Arduino Syntax Explanation

  • pin: The Arduino pin number where you want to output the PWM signal.
  • value: A number between 0 and 255, representing the duty cycle. 0 corresponds to a 0% duty cycle (always off), while 255 corresponds to a 100% duty cycle (always on).

Arduino code Example

int ledPin = 9; // Pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set pin 9 as output
}
void loop() {
  analogWrite(ledPin, 64);  // Set PWM duty cycle to 25%
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 128); // Set PWM duty cycle to 50%
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 192); // Set PWM duty cycle to 75%
  delay(1000);              // Wait for 1 second
  analogWrite(ledPin, 255); // Set PWM duty cycle to 100% (fully on)
  delay(1000);              // Wait for 1 second
}

Code Explanation

  • pinMode(ledPin, OUTPUT);: Configures pin 9 as an output pin to control the LED.
  • analogWrite(ledPin, 64);: Sends a PWM signal with a 25% duty cycle, keeping the LED on 25% of the time (dimmed).
  • analogWrite(ledPin, 128);: Sends a PWM signal with a 50% duty cycle, meaning the LED is on half the time and off half the time (moderate brightness).
  • analogWrite(ledPin, 192);: Sends a PWM signal with a 75% duty cycle, making the LED stay on 75% of the time (brighter).
  • analogWrite(ledPin, 255);: Sends a PWM signal with a 100% duty cycle, keeping the LED fully on (maximum brightness).
  • delay(1000);: Waits for 1 second between each change in duty cycle to observe the difference.

Notes

  • The duty cycle determines how long the PWM signal stays “on” during each cycle. A higher duty cycle means more power is delivered to the device.
  • For LEDs, a higher duty cycle results in a brighter light, while for motors, it increases speed.
  • The value in analogWrite() sets the duty cycle, where 0 represents 0% (off), and 255 represents 100% (fully on).
  • Be sure to use pins that support PWM output (usually marked with “~”) on your Arduino board.

PWM Pins – Pins Capable of PWM Output in Arduino

What are PWM Pins?
PWM Pins are specific digital pins on an Arduino board that support Pulse Width Modulation (PWM) output. PWM allows the Arduino to simulate an analog output by switching the pin on and off at a high frequency. The duty cycle of the signal determines how long the pin stays “on” in each cycle. Not all pins on an Arduino board support PWM, and PWM-capable pins are typically marked with a “~” symbol.

Use purpose
The PWM pins are essential for:

  • Controlling the brightness of LEDs: Using PWM pins, you can smoothly dim or brighten an LED.
  • Adjusting motor speed: PWM pins are used to regulate the speed of DC motors or other similar devices.
  • Generating analog-like output: They allow you to simulate varying voltage levels with digital pins.

Arduino Syntax use
analogWrite(pin, value);

Arduino Syntax Explanation

  • pin: The PWM pin number where the PWM signal is sent. Ensure that the pin supports PWM output (usually marked with “~” on the Arduino board).
  • value: A number between 0 and 255. This controls the duty cycle, where 0 is 0% (off) and 255 is 100% (fully on).

Arduino code Example

int pwmPin = 9; // Pin capable of PWM output
void setup() {
  pinMode(pwmPin, OUTPUT); // Set pin 9 as output
}
void loop() {
  analogWrite(pwmPin, 128); // Set PWM to 50% duty cycle
  delay(1000);              // Wait for 1 second
  analogWrite(pwmPin, 255); // Set PWM to 100% duty cycle (fully on)
  delay(1000);              // Wait for 1 second
  analogWrite(pwmPin, 0);   // Set PWM to 0% duty cycle (off)
  delay(1000);              // Wait for 1 second
}

Code Explanation

  • pinMode(pwmPin, OUTPUT);: Configures pin 9 (a PWM pin) as an output.
  • analogWrite(pwmPin, 128);: Sends a PWM signal with a 50% duty cycle to pin 9, meaning the pin is on half the time (moderate brightness or motor speed).
  • analogWrite(pwmPin, 255);: Sends a PWM signal with a 100% duty cycle, keeping the pin fully on.
  • analogWrite(pwmPin, 0);: Turns the PWM signal off (0% duty cycle).
  • delay(1000);: Pauses the program for 1 second between each change.

Notes

  • The PWM pins on an Arduino board are usually marked with a “~” symbol. Common PWM pins on the Arduino Uno are 3, 5, 6, 9, 10, and 11.
  • Each PWM pin operates at a specific frequency (approximately 490 Hz or 980 Hz, depending on the pin).
  • Only certain pins on each Arduino board are capable of PWM output, so make sure to use the appropriate pins for your project.
  • analogWrite() is used with PWM-capable pins to control the duty cycle, simulating analog output.

Common Problems and Solutions

  1. Not Using PWM Pins
    Problem: PWM does not work on a pin.
    Solution: Ensure you’re using PWM-enabled pins (marked with “~” on the board).
  2. Inconsistent LED Brightness or Motor Speed
    Problem: Inconsistent control over brightness or speed.
    Solution: Ensure the value passed to analogWrite() is between 0 and 255 for smooth transitions.
  3. Flickering LEDs
    Problem: LEDs flicker instead of dimming smoothly.
    Solution: Adjust the PWM frequency or ensure the components are compatible with the PWM frequency.

Chapter Summary

  • analogWrite() allows Arduino to output a PWM signal to control devices like LEDs and motors.
  • PWM frequency and duty cycle play a crucial role in smooth control over brightness and motor speed.
  • Use PWM pins (marked with “~”) to output PWM signals. Not all pins support PWM.

FAQ

  1. What does the analogWrite() function do in Arduino?
    It outputs a PWM signal, allowing control over brightness, motor speed, or simulating analog output.
  2. What is PWM Duty Cycle?
    The duty cycle refers to the percentage of time a PWM signal remains in the “high” state during each cycle, controlling the power delivered to a device.
  3. Why does my LED flicker when using PWM?
    Flickering can occur due to low PWM frequency or incompatibility of the LED with the PWM signal.

Simple MCQ Questions and Answers

  1. What is the purpose of the analogWrite() function in Arduino?
    a) Reads analog values
    b) Outputs PWM signals
    c) Toggles digital pins
    Answer: b) Outputs PWM signals
  2. What is the PWM duty cycle in Arduino?
    a) The percentage of time the pin is off
    b) The percentage of time the signal is high
    c) The frequency of the signal
    Answer: b) The percentage of time the signal is high