бытовая техника

## 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, 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.

Precaution:

The website https://electricianworldnews.com has been stealing content without consent from this weblog(http://ee-diary.blogspot.com) and therefore information there may not be accurate, so for original content go to .

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.

Example 1:

An example code to generate PWM signal on Pin 11 of Arduino Uno is as follows.

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

void setup () {

}

void loop() {
analogWrite(11, 127); //PWM on pin 11 with 50% duty cycle
delay(10);				//10ms wait
<span>    [/size]<span>    [/size]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.

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
<span>    [/size]<span>    [/size]analogWrite(11, 0);		//PWM on pin 11 with 0% duty cycle
delay(5);				//5ms wait
<span>    [/size]<span>    [/size]analogWrite(11, 230);	//PWM on pin 11 with 90% duty cycle
delay(10);				//10ms wait
<span>    [/size]<span>    [/size]analogWrite(11, 0);		//PWM on pin 11 with 0% duty cycle
delay(5);				//5ms wait
}

``````

The following graph shows the PWM signal generated.

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

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://ee-diary.blogspot.com

void setup () {

}

void loop() {

for(dc = 0; dc <255; dc++){
analogWrite(11, dc);
delay(1);
}
analogWrite(11, 0);
delay(10);
}``````

Following graph shows the output PWM signal for this example.

Next see tutorial Arduino CTC mode Programming with Examples.

[fixed][/fixed]

#### Похожее

[poll] {poll} [/poll]
Marijuana deliveri