Arduino Functions AnalogWrite PDF
Arduino Functions AnalogWrite PDF
Syntax
analogWrite(pin, value);
For example:
analogWrite(2, 255); or analogWrite(13, 0);
PWM, also known as Pulse Width Modulation, is a technique for getting analog
results 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?
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){ if(x==100){
digitalWrite(3, HIGH); digitalWrite(3, HIGH);
} }
else if(x<50){ or else{
digitalWrite(1, HIGH); digitalWrite(2, HIGH);
} digitalWrite(1, HIGH);
else{ }
digitalWrite(2, 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?
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?
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);
}