Arduino PWM Programming with analogWrite() Examples

 Arduino can be used to generate PWM(Pulse Width Modulation) signals which are used in application such as DC motor control, power control and converters, communication, audio signal and analog signal generation etc. There are two types of PWM signal that can be generated using Arduino- Fast PWM signal and Phase Correct PWM signal.

PWM signal can be generated with Arduino using the analogWrite(pin, dutycycle) function. Here we have to provide pin number which can be 3, 5, 6, 9, 10, or 11 for Arduino Uno and duty cycle which can have value in the range 0 to 255 to the analogWrite() function. But using analogWrite() function we will not have control over the frequency. For Arduino Uno the PWM frequency on pin 3,9,10 and 11 is 490Hz while on pin 5 and 6 it is 980Hz. The PWM frequency generated actually depends upon which Arduino board is being used. Following is table that shows which board and pins generates what PWM frequencies.

 

PWM Example 1: 

Consider driving a DC motor with PWM as shown in the following circuit diagram. 

Arduino PWM driving DC motor

The pwm code to generate PWM signal on Pin 11 of Arduino Uno and drive the DC motor is as follows.

//source: https://electronics-circuits.live
void setup(){
 }

void loop(){
	analogWrite(11, 127);     //PWM on pin 11 with 50% duty cycle
	delay(10);		//10ms wait
        analogWrite(11, 0);     //PWM on pin 11 with 0% duty cycle
	delay(20);		//10ms wait
}

In the above code, we generate PWM signal of 50% duty cycle on pin 11 for 10ms using the analogWrite(11, 127). Then we generate PWM signal  of 0% duty cycle on the same pin 11 for 20 ms.


Using Fourier Transform we can observe that the frequency of the PWM signal on pin 11 is 500Hz.


PWM Example 2:

The following arduino PWM code example generates PWM signal with 20% duty cycle for 10ms and PWM signal with 60% duty cycle on pin 11.

//source: https://ee-diary.blogspot.com

void setup(){
}

void loop() {
	analogWrite(11, 51);	//PWM on pin 11 with 20% duty cycle
	delay(10);				//10ms wait
    analogWrite(11, 0);		//PWM on pin 11 with 0% duty cycle
	delay(5);				//5ms wait
    analogWrite(11, 230);	//PWM on pin 11 with 90% duty cycle
	delay(10);				//10ms wait
    analogWrite(11, 0);		//PWM on pin 11 with 0% duty cycle
	delay(5);				//5ms wait
}

Again the PWM signal generated can be used to drive DC motor.

Arduino PWM driving DC motor example 2

The following graph shows the PWM signal generated.


 The frequency of the PWM is in this case also 500Hz.

 PWM Example 3: 

Another example is provided in which the PWM signal with increasing duty cycle from 0% to 100% is generated each lasting for 1ms. After the final 100% duty cycle PWM is generated which also last for 1ms, the PWM signal of 0% is generated that also last for 10ms. Then the same PWM generation pattern is repeated. This can be useful in providing increasing power to control brightness of LED or Lamp.

//source: https://www.electronics-circuit.live

void setup () {
}

void loop() {
	for(unsigned int dc = 0; dc <255; dc++){
	analogWrite(11, dc);
	delay(1);
	}
	analogWrite(11, 0);
	delay(10);
}

The following animation shows the above code used to drive DC motor with the increasing PWM signal duty cycle. Notice how the DC motor spins gradually faster and faster due to increasing duty cycle.gradual increase PWM duty cycle driving motor



Following graph shows the gradually increasing duty cycle PWM signal waveform.

Next see tutorial Arduino CTC mode Programming with Examples.

Post a Comment

Previous Post Next Post