PWM control with Arduino

Just a real quick snippet today. If you followed along my first Arduino post (Getting started with Arduino) then you should already have a simple blinking LED on Pin 10 (or pin of your choice). I wanted to take that just a step further and use PWM (Pulse Width Modulation) for a fade in / fade out effect.

PWM uses a pulsing digital signal (high/low) to simulate the effect of an analog signal. If you really want to learn the technical details of PWM, head on over to the Arduino page –¬†http://arduino.cc/en/Tutorial/PWM

arduino-blink-wiringThe best part, no hardware changes! Simply upload this new sketch with the breadboard layout in the original post (below, in case you missed it)

// Our LED is connected to Pin 10
// Set a variable called LED for Pin #10
int led = 10;

void setup()  { 
  // No Setup
} 

void loop()  { 
  // Fade in from min (0) to max (255)
  for(int fade = 0 ; fade <= 255; fade++) { 
    // Set the pin to output the value of fade
    analogWrite(led, fade);         
    // Delay for 5 milliseconds
    delay(5);                            
  } 

  // Fade out from max (255) to min (0)
  for(int fade = 255 ; fade >= 0; fade--) { 
    // Set the pin to output the value of fade
    analogWrite(led, fade);         
    // Delay for 5 milliseconds   
    delay(5);                            
  } 
}

One thought on “PWM control with Arduino”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.