Using PWM outputs with an Arduino and a LED

Hi everyone, been a while since my last post, but I have been a busy new daddy. 🙂 I wanted to demonstrate what PWM output was and how to use it nicely in a sketch. I’m really big on ramping lights on and off (my entire house is set up that way) and would like to share how do accomplish that. I also wanted to use a video to show PWM outputs on a scope to help me explain the process.


Slide from video above

Pulse Width Modulation (PWM, but also sometimes referred to as Pulse-Duration Modulation -PDM) is the manipulating (modulating) of the width of a fixed pulse. The pulses are sent at the same voltage and frequency, so just the width of the pulse is changed. In the screen shot above the fixed voltage is 3.2V and the Frequency is 490Hz.

You can grab the serial controlled Arduino code here, or the shorter fading sketch here.

9 thoughts on “Using PWM outputs with an Arduino and a LED

  1. What is the portable oscilloscope you are using ? Are you happy with it ?
    I’m looking for a portable oscilloscope (im a hobbyist) That’s good enought without ruining myself ! If you have any recommendations that would be great 😉

    Nice blog, keep it up !

    • Thanks sigo, I was thinking about doing a tutorial on the scope that I use (DSO Nano 2), AND how to put good firmware in it. Maybe I will in the next few days. 🙂

  2. Pingback: Using PWM outputs with an Arduino and a LED « adafruit industries blog

  3. Excellent demo! The combo of oscilloscope and multimeter in your video was very good from educational point of view. Thanks

  4. Pingback: Electronics-Lab.com Blog » Blog Archive » Using PWM outputs with an Arduino and a LED

  5. Pingback: A Quick Word About Arduino Pins (ATmega168/328) | The Custom Geek

  6. Pingback: Newbie: Need to convert a pulse to flat

  7. Hi,

    Thanks for the great tutorial. I’m totally new to MP but not to OOP, so it;s great to see physical output generated by code.

    I am configuring something similar but to eventually control a large load with some LED arrays. I wanted to give my dimming more of a “breathing” feeking so I wrapped the output values in a sin() function so that it lags a bit on the bright part. Right now it hangs a bit much, so I will try to flatten the wave a bit by playing with the values.

    Right now the loop looks like this:

    void loop() {
    float amp = 255 * sin(i * .75);
    if(amp < 0) {
    amp *= -1;
    }
    analogWrite(LED, amp);
    delay(190);
    i += .1;
    }

Comments are closed.