fpga4fun.com - where FPGAs are fun.
Home
Welcome
Information


FPGA projects
Music box
Pong game
R/C servos
Text LCD module
Quadrature decoder
PWM and one-bit DAC
Debouncer
LED displays
Crossing clock domains
External contributions

FPGA interface projects
RS-232
JTAG
I2C
EPP
SPI
CNC steppers

FPGA advanced projects
Graphic LCD panel
Digital oscilloscope
10BASE-T interface
PCI interface
Spoc CPU core

Hands-on
A simple oscilloscope


FPGA introduction
What are FPGAs?
How FPGAs work
FPGA pins
Clocks and global lines
Download cables
Configuration
Learn more

FPGA software
Design software
Pin assignment
Design-entry/HDL
Simulation/HDL
Synthesis and P&R

FPGA electronic
SMD technology
Crystals and oscillators

HDL info
HDL tutorials
Verilog tips
VHDL tips

Quick-start guides
ISE
Quartus

Site
News
FPGA links
HDL tutorials
Forum


Varying an LED intensity

Turning an LED on and off

Here's how to make an LED blink (on and off).

module LEDblink(clk, LED);
input clk;     // clock typically from 10MHz to 50MHz
output LED;

// create a binary counter
reg [32:0] cnt;
always @(posedge clk) cnt<=cnt+1;

assign LED = cnt[22];       // blink the LED at a few Hz (change the bit index to change the blinking rate)
endmodule
Making an LED half-lit
One solution would be doubling the value of the resistor used in series with the LED.

Another solution is to drive the LED FPGA output half of the time. If that's done fast enough, your eye will see the LED half-lit. Anything faster than 100Hz is too fast for the human eye to detect - it averages the light intensity it receives.

module LEDhalflit(clk, LED);
input clk;     // clk should be at least 200Hz. Anything above is fine (most FPGA boards have adequate clocks, running at a few 10's of MHz)
output LED;

reg toggle;
always @(posedge clk) toggle<=~toggle;     // toggles at half the clk frequency (at least 100Hz)

assign LED = toggle;
endmodule
Fine-tuning the LED intensity

For more LED intensity control, a PWM is an ideal solution.
Here's an example that uses a 4-bits control to select between 16 intensity levels out of an LED.

module LED_PWM(clk, PWM_input, LED);
input clk;
input [3:0] PWM_input;     // 16 intensity levels
output LED;

reg [4:0] PWM;
always @(posedge clk) PWM <= PWM[3:0]+PWM_input;

assign LED = PWM[4];
endmodule
Glowing the LED

By continuously changing the LED intensity, the LED appears to "glow".

module LEDglow(clk, LED);
input clk;
output LED;

reg [23:0] cnt;
always @(posedge clk) cnt<=cnt+1;

wire [3:0] PWM_input = cnt[23] ? cnt[22:19] : ~cnt[22:19];    // ramp the PWM input up and down
reg [4:0] PWM;
always @(posedge clk) PWM <= PWM[3:0]+PWM_input;

assign LED = PWM[4];
endmodule

This is the same example that was presented in part 0.



>>> NEXT: 7-segments LED displays >>>



This page was last updated on August 09 2006.