Arduino

Pulse Width Modulation (PWM) in Arduino

Pulse Width Modulation also known as PWM is a technique that involves the alteration in the width of the pulse of the generated signal as a result the duty cycle of the signal is changed which will give the variable signal. This variable input can be used in many applications like controlling the brightness of LEDs, controlling the speed of motors and where analog output is required by using digital sources.

Pulse Width modulation with Arduino

The pulse width modulation can be done in Arduino using the analogWrite() function. The analogWrite() function itself generates the square wave signal that can be varied from the function.

The analogWrite() function uses two arguments, one is a pin which will specify the port number at which the modulated signal will generate and the other one is value that specifies the value of the duty cycle of the modulated signal.To use the analogwrite function in Arduino programing the following syntax should be followed

analogWrite(pin number , value of duty cycle );

The pin number is of integer data type whereas the value of the duty cycle has the form from zero to 255.The pulse width is the part of the pulse in which its value is high. Similarly, the duration of cycle of the pulse is the duration of its high and low values. Moreover, the percentage of the ratio of duration of pulse width to duration of cycle is called the duty cycle. There different duty cycles are given for more understanding of the topic. The plotted graphs have time on the horizontal axis whereas the voltage is on the vertical axis. These are the percentages for how much time the voltage was high. Duty cycle is the time for which the voltage was high.

Example

The code for the pulse width modulation is given below.

int ledPin = 6; //Arduino Uno PWM pins: 3, 5, 6, 9, 10
int brightness = 0;
int fadeValue = 5;

void setup() {

pinMode(ledPin, OUTPUT);

 }

void loop() {

analogWrite(ledPin, brightness);
 
brightness = brightness + fadeValue;

if (brightness = 255) {
fadeValue = -fadeValue;

 }

delay(10);
 
}

First the ledPin variable is declared at which the LED light is connected then to store the analogwrite value a variable of brightness is declared. The value will cycle in the range between 0 to 255. To control the fadedness of the LED a variable called fadeValue is used.

Coming to the set-up section the pin number assigned to the LED is declared and in the loop section the pulse width modulation signal is generated using the analogWrite() function. The brightness of the LED is controlled with the alteration of width of the pulse. Led Pin and brightness are taken as arguments of the analogwrite function. After that the brightness and fadeValue variable is added. To increase the brightness by five times at every time the loop runs that’s why the fadeValue is given the value 5.

The if condition is used to run the code only if the brightness is less than equal to zero or greater than equal to 255.

So at the start the value for brightness is zero and fadevalue is 5. So in the first statement the fade amount is added into brightness and now the brightness has a value of five. Then coming to the if statement the condition is false as the brightness is not less than equal to zero or the brightness is greater than equal to 255. So the loop will continue to run until the brightness value reaches 255. So if the if condition is true then a value of negative five 5 is added to the fade amount.

So now at each iteration the value will decrease by 5 till it reaches zero and the led will turn off.

Conclusion

There are a wide range of projects that can be done using Arduino. Using Arduino somewhat makes it easy to work on projects. In this article the pulse width modulation (PWM) is discussed and one of its applications is described to give more detail of how pulse width modulation (PWM) can be used for specific tasks in Arduino programming.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.