15

Arduino Functions - AnalogWrite · analogWrite(); The analogWrite function writes an analog value (PWM wave) to a PWM-enabled pin. An analog value ranges from 0 to 255. In Arduino

Embed Size (px)

Citation preview

analogWrite();

The analogWrite function writes an analog value (PWM wave) to a PWM-enabled pin.

Syntax

analogWrite(pin, value);

For example:

analogWrite(2, 255); or analogWrite(13, 0);

Note: Capitalization of the W in analogWrite is a MUST!

analogWrite();

The analogWrite function writes an analog value (PWM wave) to a PWM-enabled pin.

An analog value ranges from 0 to 255.

In Arduino Uno/Nano microcontroller,

Only pins 3, 5, 6, 9, 10, 11 can be used as analog output pins as they have the ~ sign printed beside their pin number. The ~ sign means that the pin is PWM-enabled.

What is a PWM wave?

PWM, also known as Pulse Width Modulation, is a technique for getting analogresults with digital means.

Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off.

What is a PWM wave?

The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.

If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

if();

The if function is used in conjunction with a comparison operator, tests whether a certain condition has been reached, for example an input being above/below a certain number.

Comparison operators consist of == (equal to), != (not equal to),

< (less than), > (more than)

Syntax

if(condition is true)

{

statements;

}

For example:

if(x != 200){

digitalWrite(3, HIGH);

digitalWrite(1, HIGH);

}

Note: Use of correct parentheses and curly brackets for functions like this are important! Wrong usage of any one of them will lead to compilation error and give you a hard time finding out the cause of the problem! You have been warned!

if(){}else if(){}/else{};

The if/else function allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together.

Syntax

if(condition){}else if(condition){}/else{};

Note: Code MUST begin with an if statement, followed by else if, or else statement. You cannot start with else if statement! An else statement is optional, it is okay if you leave out else statement in the code.

For example:

if(x>100){

digitalWrite(3, HIGH);

}

else if(x<50){

digitalWrite(1, HIGH);

}

else{

digitalWrite(2, HIGH);

}

or

if(x==100){

digitalWrite(3, HIGH);

}

else{

digitalWrite(2, HIGH);

digitalWrite(1, HIGH);

}

How to increase an LED’s brightness from an off state, to half brightness, to full brightness, every half a second?

Hint: First, setup your LED using one digital pin. Recall the minimum and maximum range of value for using the analog function. Can all pins be used to control LED brightness?

Answer:void setup(){

pinMode(3, OUTPUT);

}

void loop(){

analogWrite(3, 0); //LED is off

delay(500);

analogWrite(3, 127); //LED is half bright

delay(500);

analogWrite(3, 255); //LED is fully bright

delay(500);

}

Note: analog value ranges from 0 to 255. Only pins 3, 5, 6, 9, 10, 11 can be used as analog pins as they are PWM-enabled.

How to increase an LED’s brightness from an off state, to half brightness, to full brightness, and vice versa, at a faster speed?

Hint: Recall the useful feature of variables. When properly used, it helps save time changing the programming code.

Answer:int time = 250; //by changing this number, all the rest of code will change accordingly

void setup(){

pinMode(3, OUTPUT);

}

void loop(){

analogWrite(3, 0); //LED is off

delay(time);

analogWrite(3, 127); //LED is half bright

delay(time);

analogWrite(3, 255); //LED is fully bright

delay(time);

analogWrite(3, 127); //LED is half bright

delay(time);

}

Note: Why isn’t the last analogWrite(3, 0); needed? The reason is because the loop will repeat itself and the beginning is actually analogWrite(3, 0); . Experiment with different speeds by changing the “time” variable!

How to increase the brightness of LED slowly from an off state to full brightness? When it is fully bright, how to slowly decrease it back to an off state?

Hint: Create a variable to contain the analog value for the LED. Also, create a variable to control increment or decrement of LED analog value. Remember how to use if/else statement?

Answer:int brightValue; //increment/decrement is controlled by a variable named “brightValue”

int ledBrightness = 0; //LED brightness starts from 0

void setup(){

pinMode(3, OUTPUT);

}

void loop(){

if (bright <= 0){ //when LED is off

brightValue = 1;

}

else if (bright >= 255){ //when LED is fully bright

brightValue = -1;

}

ledBrightness = ledBrightness + brightValue;

analogWrite(3, ledBrightness);

delay(10);

}