Project #3: Demonstrating PWM
Now let’s try this with our circuit from Project 2. Enter the following sketch
into the IDE and upload it to the Arduino:
Project 3 - Demonstrating PWM
int d = 5;
void setup()
{
pinMode(3, OUTPUT); // LED control pin is 3, a PWM capable pin
}
void loop()
{
for ( int a = 0 ; a < 256 ; a++ )
{
analogWrite(3, a);
delay(d);
}
for ( int a = 255 ; a >= 0 ; a-- )
{
analogWrite(3, a);
delay(d);
}
delay(200);
}
..................................................................................................................................................................
The LED on digital pin 3 will exhibit a “breathing effect” as the
duty cycle increases and decreases. In other words, the LED will turn on,
increasing in brightness until fully lit, and then reverse. Experiment with
the sketch and circuit. For example, make five LEDs breathe at once, or
have them do so sequentially.
Now let’s try this with our circuit from Project 2. Enter the following sketch
into the IDE and upload it to the Arduino:
Project 3 - Demonstrating PWM
int d = 5;
void setup()
{
pinMode(3, OUTPUT); // LED control pin is 3, a PWM capable pin
}
void loop()
{
for ( int a = 0 ; a < 256 ; a++ )
{
analogWrite(3, a);
delay(d);
}
for ( int a = 255 ; a >= 0 ; a-- )
{
analogWrite(3, a);
delay(d);
}
delay(200);
}
..................................................................................................................................................................
The LED on digital pin 3 will exhibit a “breathing effect” as the
duty cycle increases and decreases. In other words, the LED will turn on,
increasing in brightness until fully lit, and then reverse. Experiment with
the sketch and circuit. For example, make five LEDs breathe at once, or
have them do so sequentially.
Comments
Post a Comment