PWM: dim a Led

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how using the Pulse Width Modulation (PWM) for dimming an LED.

Introduction to PWM

PWM, Pulse Width Modulation, is a digital periodic signal. It is like a square wave, but the duty cycle is variable. The duty cycle is the ratio between the high level period duration and the low level period duration, often expressed in %. A duty cycle of 100% is a signal that is always high, a duty cycle of 0% is always low, and 50% is a perfect square wave where the high and low durations are the same. There are 2 main parameters of a PWM signal: the duty cycle discussed above, and the frequency of the signal, which represents the repetitions per second of our signal.

For example, a PWM with a frequency of 200Hz will have the period:

T = 1/f = 1/200 = 5ms

So every 5ms the period will be repeated. Using a PWM with 25% of duty there will be:

  • Total period: 5ms
  • High duration: (5ms*25/100) = 1.25 ms
  • Low duration: 5ms – 1.25ms = 3.75 ms


The PWM can be used for different scope:

  • Change the luminosity of LED
  • Generate frequency for tone
  • Control the velocity of stepper motor
  • Simulate a simple Digital to Analog converter (DAC)
  • etc

How to use it

In this tutorial we explain a simple application of PWM to change the brightness of an LED. In fact, by changing the PWM duty cycle we change the root mean square (RMS) voltage of the signal so the brightness of the LED. Flyport allows to generate 9 independent PWM signals.


Flyport PRO

Check the common connector J1 for Flyport PRO.

Code example

Name Function Description
 void PWMInit (BYTE pwm, float freq, float  dutyc) Initializes the specified PWM with the desired frequency and duty cycle.

Parameters: pwm pwm module number (from 1 up to 9). freq Frequency in hertz. dutyc Ducty cycle for the PWM in percent (0-100).

 void PWMOn (BYTE  io, BYTE  pwm) Turns on the specified PWM on the specified pin.

Parameters: io Pin to assign the PWM. pwm PWM module number previously defined in PWMInit.

 void PWMDuty (float  duty, BYTE  pwm) Changes the duty cycle of the PWM without turning it off. Useful for motors or dimmers.

Parameters: duty New duty cycle desired (0-100). pwm PWM module number previously defined in PWMInit.


Source code for FlyportTask.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
    int i;
    // Disable all Analog channels
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    const int maxBright = 100; //here we set max % of brightness
    const int minBright = 2; //and here the min %
    float bright = (float)maxBright;
 
    PWMInit(1,1000,maxBright);//Initialize PWM1 to work at 1000 Hz, maxBright % of duty
    PWMOn(p13, 1);//Assign pin 13 as PWM1 and turns it on
    while(1)
    {
        for (bright = maxBright; bright > minBright; bright--)
        {
            PWMDuty(bright, 1);//Change the duty at bright value
            vTaskDelay(1); //used to slowing the effect
        }
        for (bright = minBright; bright < maxBright; bright ++)
        {        
            PWMDuty(bright, 1);//Change the duty at bright value
            vTaskDelay(1); //used to slowing the effect
        }
    }    
}

Flyport

Check the common connector J1.


Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 1x LED
  • 1x Resistor (330 Ohm)
  • 1x Proto Nest Board
  • 1x Breadboard


Schematic

DoutSchematic.png


DigitalOut Image.jpg


Code example

Name Function Description
 void PWMInit (BYTE pwm, float freq, float  dutyc) Initializes the specified PWM with the desired frequency and duty cycle.

Parameters: pwm pwm module number (from 1 up to 9). freq Frequency in hertz. dutyc Ducty cycle for the PWM in percent (0-100).

 void PWMOn (BYTE  io, BYTE  pwm) Turns on the specified PWM on the specified pin.

Parameters: io Pin to assign the PWM. pwm PWM module number previously defined in PWMInit.

 void PWMDuty (float  duty, BYTE  pwm) Changes the duty cycle of the PWM without turning it off. Useful for motors or dimmers.

Parameters: duty New duty cycle desired (0-100). pwm PWM module number previously defined in PWMInit.


Source code for FlyportTask.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
	const int maxBright = 80; //here we set max % of brightness
	const int minBright = 2; //and here the min %
	float bright = (float)maxBright;
 
	PWMInit(1,1000,maxBright);//Initialize PWM1 to work at 1000 Hz, maxBright % of duty
	PWMOn(p9, 1);//Assign pin 9 as PWM1 and turns it on
	while(1)
	{
		for (bright = maxBright; bright > minBright; bright--)
		{
			PWMDuty(bright, 1);//Change the duty at bright value
			vTaskDelay(1); //used to slowing the effect
		}
		for (bright = minBright; bright < maxBright; bright ++)
		{		
			PWMDuty(bright, 1);//Change the duty at bright value
			vTaskDelay(1); //used to slowing the effect
		}
	}	
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox