In this tutorial, we use two RGB LEDs controlled by PWM and the Alchitry AU FPGA board, to build a few quick projects:
1: A dimmer circuit where we alternately brighten and dim two white LEDs.
2. A dimmer circuit where we gradually transfer our LEDs back and forth between Red and Green.
3. A color changing circuit where we transition our two LEDs around the color wheel, displaying all the colors of the rainbow.
4. A display of named colors, where we switch our LEDs between Red, Orange, Yellow, Green, Aqua, Blue, Violet, White, and Black.
5..A simple traffic light controller, using our two LEDs as traffic lights. This controller could manage your basic intersection!
These projects add some new elements to the ones I presented in my earlier FPGA projects. Here I have made extensive use of PWM (pulse width modulation) as well as ROMs (read only memory) to implement tables.
HardwareThis project starts with my Beginner's Guide to FPGAs, where in Part 1, we set up both hardware and software for the Alchitry Au FPGA board. And then in Part 4, I showed you how to use the Br Prototype board to connect external hardware to the Au board. You will need to refer to those two earlier projects in order to get started on this new project.
Our LEDs are common Anode, so I moved one wire (the grey one) to +v which is +3.3 v to power our LEDs. (The anode is the longest lead on the LED.) The constraint file below shows how the cathode connections are made. The different color LEDs each have different forward drops, with blue being close to 3 volts, so with a voltage source of only 3.3 volts, the limiting resistors for our LEDs need to be very different values. I used 100 ohms for the blue, 330 ohms for red, and 1000 ohms for green to get all three LEDs at approximately the same full-on brightness. See Schematic for more detail.
All the files for each project here are contained in the attached ZIP file.
We use PWM in all these projects to both dim and mix colors of our LEDs. So all our projects have a module called dimmer, which contains a PWM controller. Then in turn, the PWM module uses a table stored in a ROM module with 8 bit PWM values which approximate a smooth transition of an LED from completely off to fully on. We need this table because what the eye perceives as smooth dimming is actually very non-linear.
I kept the dimmer used in all these project fairly simple. It has just 32 steps between off and full on. We could make it smoother by increasing the steps - anywhere up to 256 steps with 8 bit PWM.
Most of our projects use multiple dimmers, potentially up to six, as we could mix 3 colors in each of our LEDs. So most of the projects have another module called dimmers where we create multiple instances of dimmer.
Note: One place where FPGA hardware differs from programming microcontrollers is that each dimmer has its own copy of the ROM table. We would never make multiple copies of a fixed table in software, but the alternative here would be to use memory and a sequencer to share one table among the various dimmers using it. But if we have lots of unused gates, multiple copies is the easiest approach.
Each project also has some kind of sequencer module that times out and sequences the various steps we put our LEDs through. And another thing all of our projects have in common is the au_top module, which links up our outputs to the physical LEDs on the Br prototype board. The outputs to the LEDs need to go low to turn on the LEDs as they are connected to the cathodes. So you will see the outputs are all inverted in the au_top module.
The Simple Dimmer ProjectThe first project is pretty straight forward. Our dimmers (plural) module only contains a single dimmer. It is first used to brighten and then dim one LED and then used to brighten and then dim the other LED. We are connecting that dimmer to all three colors on each LED, so the LED lights up white.
Red Green Dimmer ProjectThis project is similar to the first one. The dimmers module, though, has two dimmers this time. One is getting brighter while the other is getting dimmer. One is attached to red of LED1 and the green of LED2. The other is attached to the green of LED1 and the red of LED2. The two LEDs switch back and forth between red and green.
The Rainbow Color ProjectFor this project, both LED do the same thing. We employ two dimmers to mix two colors. Starting at red, we gradually reduce red, increasing green, and we gradually transition from red through orange and yellow to green. We then mix green with blue, starting at pure green and gradually transition though aqua to pure blue. Then starting at blue, we start mixing in red, transitioning from blue through purple and violet back to red.
The Standard Colors ProjectThis project also has both LEDs doing the same thing, and is similar to the rainbow project. Except this time, we are not making gradual changes. We have a color chart table where we have defined 8 standard colors: red, orange, yellow, green, aqua, blue, violet, white, black. We simply step through each of these standard colors, holding each for one second, and then repeat.
The Traffic Light ControllerHere each of our two RGB LEDs represent a traffic light which can be red, green, or yellow. We use a single dimmer to produce the yellow by simply setting it a 50 percent and applying it to both red and green LEDs. We use a table of all possible configurations of the two traffic lights, and the time duration of each configuration. And we have a simple controller that uses that table to manage the lights and their timing.
Final ThoughtsIf you want more information about RGB LEDs, PWM, color management, etc., I did an earlier article on this subject which goes into more detail. You can read it here.
If your interested in more FPGA projects, I will be doing others in the near future.
Comments