Pwm signals – Fading a LED


Learn what is PWM and how to fade in and out an LED.

PWM signals

PWM is the acronym of “Pulse Width Modulation”. What this really means is that you can control the time the signal is “ON” and “OFF”.

Imagine you want to control the intensity of the brightness of a Led or the speed of the motor, declaring a “DigitalOut” pin has only two states: ON (Voltage) and OFF (Ground), so that means that we only have full brightness or full speed and no brightness or no velocity on a motor. What does a PWM signal is that it communicates between ON and OFF at very high speeds, and the time the pin is in High over the total of time is called “Duty cycle”. In the next image you can see a graphic description of a PWM signal with different duty cycles.


Now, lets play.

Step 1. New file.

Create a new file, you can use the simple template “Blinky LED Hello World”.

Step 2. Declaring a PWM pin

Its easy just, instead of “DigitalOut” declaration for digital outputs, type “PwmOut” for a pwm output. Declare one pin as Pwm.

PwmOut myled(LED1);

Step 3. Making fade effect

If we want to fade in and out we need to use a “For” loop, if you are unfamiliar with “For” cycles you can visit this link. So here’s the example code:

float i;
for (i = 0; i <= 1; i += 0.1){ //Use smaller increments for smoothness
     myled = i;


You can find the complete program at:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s