Arduino PWM (Pulse Width Modulation) demonstrates the use of the analogWrite( ) function of Arduino library in fading an LED off and on. This can be done by turning a digital pin on and off very quickly with different ratio between on and off, to create a fading effect.

1. CONCEPTS

What is Pulse Width Modulation or PWM?

Although digital devices cannot generate a pure analog signal. Therefore, we use this technique called Pulse Width Modulation or PWM. Digital control is used to create a square wave, a signal switched between ON and OFF. In short, the pattern can simulate voltages in between full ON(5V) and Full OFF(0V). In this project, we are going to fade an led connected to one of the PWM pins of the Arduino Board.

Arduino PWM figure showing that how by varying the width of square wave we can vary the power or duty cycle and generate analog signal
Figure 1: Arduino PWM

In the graphic above, the vertical dotted lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino’s PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

Once you get this example running. For instance, grab your Arduino and shake it back and forth. What you are doing here is essentially mapping time across space. To our eyes, the movement blurs each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink in length. Now you are seeing the pulse width.

The table below shows PWM pins with Frequency on various Arduino Boards

BOARDPWM PINSPWM FREQUENCY
Uno, Nano, Mini3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)
Mega2 – 13, 44 – 46490 Hz (pins 4 and 13: 980 Hz)
Table 1: PWM pins with Frequency

2. COMPONENTS

3. CONNECTIONS

Connect the anode (the longer, positive leg) of your LED to digital output pin 9 on your board through a 470-ohm resistor. Connect the cathode (the shorter, negative leg) directly to the ground.

arduino pwm for led fading using microcontroller. Image shows how an led should be connected using breadboard with controller. Circuit Diagram
Figure 2: LED connections with Arduino

4. PROGRAM

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: ARDUINO LED FADING ON PWM PIN 9
  Fades a Light Emitting diode connected on PIN 9 of Arduino
  Using PWM (Pulse width Modulation)

  Connections:
  9     LED(+)
  GND   LED(-)
*/

int ledPin = 9;     //LED connected to PWM pin 9
void setup() {
}

void loop() {
  //to increase the brightness from minimum to maximum
  for (int inc = 0; inc <= 255; inc += 5) {
    analogWrite(ledPin, inc);
    delay(30);
  }

  //to decrease the brightness from maximum to minimum
  for (int dec = 255; dec >= 0; dec -= 5) {
    analogWrite(ledPin, dec);
    delay(30);
  }
}

5. FUNCTIONS USED


void setup( )

This function is called when a sketch starts. You can use to initialize variables, pin modes, start using libraries, etc. This function will only run once, after each power-up or reset of the Arduino board.


void loop ( )

This function is called just after calling setup(). As the name suggests this function actively controls the Arduino board by looping consecutively.


analogWrite ( )

Description: Writes an analog value (PWM) to the pin. You can use it to change the brightness of an LED or vary the speed of the motor. After calling analogWrite function, the PWM PIN generates a rectangular wave of specified duty cycle.

PWM PinsFrequency
3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino PIN number. | Allowed data types: int
  • value: a range of values between 0 (Lowest voltage) to 255(Highest Voltage). | Allowed data types: int

Returns: Nothing

Notes and Warnings:

  • In Arduino Uno, the output resolution lies between 0 to 255.
  • Above all there is no need to set pinMode() before using analogWrite(). The analogWrite() function has nothing to with analog pins or analogRead().
  • Pin 5 and 6 can have a higher-than-expected duty cycle because of delay() and millis() as they share the same timer which is used to generate PWM outputs.

delay ( )

Description: Pauses the program for the amount of time (in milliseconds) as specified inside the bracket. (1sec = 1000 ms).

Syntax: delay (ms);

  • ms: the number of milliseconds to pause. Allowed data types: unsigned long

Notes and Warnings:

  • While it is easy to use delay function for short pauses in a program, the use of delay() has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt.
  • Certain things do go on while the delay function is controlling the Atmega chip, however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained.

FAQ’s

Why LED is not Fading after increasing the parameter delay( ) function?

It’s because the signal we are generating is a rectangular wave and if timings and frequency are not proper you can get different results. So try to play around with timings or use the ones which are given in the program above.

How to identify Positive(anode) and Negative (cathode) of an LED?

There are two Ways to identify this

  • Check the legs of an LED. Positive Leg is always longer than the Negative
  • Check the Flags inside the LED. Bigger Flag is Negative and smaller one is positive.
how to identify positive-anode or negative-cathode in an LED
Still, having doubts?

Ask your questions in comment section below or Contact us.

Leave a Reply