Open In App

Analog Write and Working of PWM in Arduino

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about the working and functions of PWM in Arduino Uno R3. And also we will learn about the analog write function in Arduino using PWM pins.

Arduino:

It is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in the Arduino. It provides Arduino IDE to write code & connect the hardware devices like Arduino boards & sensors.

PWM pins in Arduino:

Arduino Uno R3 has 6 PWM pins that are 3, 5, 6, 9, 10, and 11. These pins are marked with the negation sign “ ~ “. These pins can generate a pulse as per the given inputs. Arduino supports an 8-bit wide pulse that can have 256 possible levels ( 0 to 255 ).

Pulse Width Modulation ( PWM ):

Pulse Width Modulation is a technique to get variable voltage in terms of Digital Input. PWM device generates ON and OFF pulses according to the control signal, which determines the desired voltage level. PWM is used to control the amount of power delivered to the load. It is commonly used for controlling the brightness of LED, the speed of motors, etc.

Digital to Analog Conversion in Arduino:

Arduino does not have a dedicated Digital to Analog converter. But It can emulate analog signals using the PWM technique. In PWM, the digital input is converted into a Digital pulse.

It may cause little confusion between analog voltage and digital pulse. Yes, Arduino cannot produce pure analog voltage. The analog output voltage is the average voltage of the “ON” time width of the digital pulse.

Pulse Width Modulation

In the above figure, Consider the time period of one cycle is 2ms. The “ON” cycle of the pulse is called as the duty cycle.

Duty cycle = ( ON Time / Time period ) * 100

From the above figure, 

Duty cycle = ( 1.2 / 2 ) * 100 = 60 %

This frequent change in on and off mode with a varied length of pulse produces variable analog voltage.

Duty Cycle to analog Conversion:

Analog Output voltage = ( Duty cycle / 100 ) * Amplitude of the pulse

From the figure,

Analog output voltage = ( 60 / 100 ) * 5 = 3 Volts

Of course, the digital pulse did not come to 3 volts. But the “ON” time of the pulse determines the average voltage per time period.

Digital to Analog conversion:

Similarly, The Analog voltage output can be calculated by using Digital input. The Arduino can write 0 to 5V in terms of digital input range 0 to 255.

Analog output voltage = ( Digital Input / Resolution ) * Amplitude of the pulse

Let us take Digital input as 100,

Analog output voltage = ( 100 / 255 ) * 5 = 1.96 Volts

Components Required:

  1. Arduino Uno R3
  2. LED
  3. Oscilloscope (optional)
  4. Jumper Wires

Circuit Diagram:

Analog Write in Arduino

Setup:

  1. Connect the positive terminal of the LED to the PWM pin.
  2. Connect the negative terminal of the LED to the GND pin.
  3. Connect any other output device to PWM as same as above steps.
  4. Upload the code to Arduino.

Syntax

analogWrite(Pin, Digital input);

// The analogWrite function generate digital pulse ( Emulated Analog signal) to the chosen PWM pin.
// PWM pins are 3, 5, 6, 9, 10 and 11
// Example: analogRead(6, 255); // Writing 5 Volts in PWM pin 6.

Arduino Code:

C++




void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  // for loop generate ramp from 0 to 255
  for(int digitalInput=0; digitalInput<255 ; digitalInput++)
  {
     
    Serial.print("Digital input: ");  // Text to be printed in Serial monitor
    Serial.println(digitalInput);     // Print Digital input in Serial monitor
     
    analogWrite(11, digitalInput);    // Write Analog output in pin 11
    analogWrite(9, digitalInput);     // Write Analog output in pin 9
    analogWrite(6, digitalInput);     // Write Analog output in pin 6
     
  }
}


Output:

Online Simulation link: Click Here

PWM in Arduino

Applications of PWM:

  1. Motor speed control.
  2. Brightness Adjustment in LED, Mobile phone screen, etc.


Last Updated : 19 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads