Analog Write and Working of PWM in Arduino
Last Updated :
30 Apr, 2024
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 ModulationIn 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:
- Arduino Uno R3
- LED
- Oscilloscope (optional)
- Jumper Wires
Circuit Diagram:
Analog Write in ArduinoSetup:
- Connect the positive terminal of the LED to the PWM pin.
- Connect the negative terminal of the LED to the GND pin.
- Connect any other output device to PWM as same as above steps.
- 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
Applications of PWM:
- Motor speed control.
- Brightness Adjustment in LED, Mobile phone screen, etc.
Similar Reads
analogRead() - Arduino Reference In the world of Arduino programming, The analogRead() function plays a crucial role. This function used to fetch the analog data from the analog pins in the Arduino bords. Let us think of an example of developing a weather station. This needs to collect the data from sensors. To analyze the produced
5 min read
Analog to Digital conversion in Arduino In this article, we will learn about the working of analog input in Arduino Uno R3. Also, learn how to read and write analog data using Arduino Uno R3. Arduino: It is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different senso
5 min read
LED Control with Potentiometer using Arduino In the field of electronics and programming, controlling an LED with a potentiometer is important too. By varying the resistance of the potentiometer, you can change the brightness of the LED. We will interface an LED (light-emitting diode) to the Arduino UNO board. An LED is a simple diode that emi
6 min read
Difference Between Analog and Digital Processing In the realm of signal processing and data management, it is vital to distinguish between analog and digital processing. Analog processing involves continuous signals that fluctuate with time and are therefore able to capture real-world phenomena in their most natural forms. Conversely, digital proc
5 min read
How to make digital voltmeter using Arduino? Digital VoltmeterVoltmeter is an instrument used to measure the potential difference or voltage (V) between two points of an electric circuit. Voltage is the potential difference between two points. It pushes charged electrons through conducting loop. It is also known as electric pressure, electromo
3 min read
Introduction to Analog Communication Communication is the process of establishing a connection or link between two or more points for the exchange of information. Analog communication had a significant need for centuries as it has been a fundamental need of human interaction, which allows anyone with the continuous transfer of signals
10 min read
Overview of the Arduino UNO Components Arduino is an incredibly important part of modern-day electronics. The ease with which these Arduino boards can be programmed makes them the best choice especially when it comes to integrating them with large-scale projects. In this article, we will get an overview of the basic components that make
9 min read
Servo motor Interfacing and Control using Arduino In this article, we will learn how to interface and control servo motors using Arduino Uno R3. Arduino 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 t
3 min read
LED Blinking Using Arduino We will interface an LED (light-emitting diode) to the Arduino UNO board. An LED is a simple diode that emits light in a forward bias. We will write an LED-blinking program on the Arduino IDE and download it to the microcontroller board. The program simply turns ON and OFF LED with some delay betwee
7 min read
Getting Started With Arduino Arduino is an important device used in electronics engineering for creating mini-projects or for integrating large projects. Arduino itself consists of various components that can be programmed according to the project requirements using some assembly languages like C/C++. Arduino is the first choic
15+ min read