Swordfish Program - Large LED Array & Brightness Control via PWM

Pulse Width Modulation (PWM) is extremely handy for multiple purposes, one in particular is controlling high current devices such as DC motors and large  LED arrays. PIC's have their limitations, namely 25mA per output, so if you want to drive something a lot larger, using PWM and a high power switching device such as a logic MOSFET will do the job perfectly.

Although there are many other uses (such as Inferred modulation and Ultrasonic), this example focuses purely on a PWM program designed to control the brightness of a large LED array.

 

What Is PWM?

A PWM signal consists of both frequency and duty cycle. Both are important to understand;

  • The Frequency component can be though of as how many pulses per second. If your PWM signal had 50 pulses per second, then it would be 50Hz. Although this is slow, you don't need to make things to much faster to trick the human eye that there is no flashing. Consider your TV playing video at 25 frames per second, or ceiling lights pulsing at 60Hz! I like to aim for 200-1Khz with my designs, I chose 5KHz out of the blue the example below.
  • The Duty Cycle component is the important part of PWM, it is what controls the average current/voltage to the target device. It is not nearly as complicated as it sounds, consider these two differentsignals; 
    • Steady 5 Volts 1KHz 50%
    • Duty Cycle PWM Signal

If you put your multimeter between GND and the 5V signal, you will read 5 Volts... However, if you put it between GND and the PWM signal operating at 50%, you will read 2.5 Volts! It simply reads an average of what's actually there, in this case, only 50% of the time is there 5V available!

Now to put this excellent tool to use and drive 12 Very Bright LED's that require 250mA to operate at full brightness - a task that a PIC could not do without PWM!

 

Schematic

PWM20Example1

The 10 Ohm resistor is very important, as it controls the maximum amount of current that can be supplied to the LED's, without it, the LED's would self destruct themselves when the duty cycle percentage becomes high enough, and you can say goodbye to 12 LED's in a matter of moments...

Logic MOSFET's make mince meat of any high power task, the turn on hard with 5 Volts to the Gate, and usually have an on resistance of about 0.01 ohm or less. In this scenario, I am switching the earth on and off, so I am using a N-Channel Logic MOSFET, if you require to switch the supply on and off, then you need to use a P-Channel Logic MOSFET. See the MOSFET tutorial for more info.

 

The Code

Now for the source code;

Device = 18F4550
Clock = 8
Config FOSC = INTOSCIO_EC
 
Include "INTOSC8.bas"
 
 // import PWM module...
Include "PWM2.bas"
 
// local duty variable...
Dim Duty As Byte
 
// main program...
PWM.SetFreq(5000)
PWM.Start1
While true
 Duty = 0
 Repeat
 PWM.SetDuty1Percent(Duty)
 Inc(Duty)
 DelayMS(25)
 Until Duty > 100
 Repeat
 PWM.SetDuty1Percent(Duty)
 Dec(Duty)
 DelayMS(25)
 Until Duty = 0
Wend

You will notice that I am using the PWM2.bas library, this is not a standard library within Swordfish, you need to download it and save it in your \Swordfish\UserLibrary directory. The module can be found here..

This program will set the frequency of the PWM signal to 5Khz, and then ramp up the Duty Cycle from 0 to 100% in 2.5 seconds, and then back down again. The LED array will go brighten and dim accordingly, all with the power of PWM and Duty Cycle.

 

save_f21 Download the library: PWM Library

Share this article

Tags: LED, PWM, Swordfish