LED Variable Blink Circuit
This writeup describes a simple circuit that blinks an LED in one of 8 different modes. I will use copies of it to light up the Plexiglas logo and the coin door lights. A slightly modified version will control the power on/off button.
I'm no expert at this stuff. This project was done primarily to keep my brain energized. It took lots of internet searching and a bit of encouragement from a few acquaintances that have experience working with this technology. There are a lot of folks on this forum that know this stuff a lot better than me. I'm just writing it up here to pass on what I learned and hopefully encourage at least one of you to try something similar.
Attached below are a schematic diagram of the circuit, a photo of the breadboard proof of concept, a few photos of the first draft PCB, a few photos of the device used to flash the chip that controls the circuit, and a zip file containing the source code for the project. I've got some .mpg video files of it in action and will post them when I get them edited.
Finally, in this hobby I have an opportunity to actually write some code. Not just a batch file here and there but real compiled C code with loops, a state machine, timers, enums, and other such fun. It beats the heck out of working with Excel spreadsheets and giving Power Point presentations.
This project involved two technologies; PIC microcontrollers and the code that runs in them. A PIC is a chip that has variable inputs and outputs, that you write code for, that is downloaded into the chip, and runs whenever power is supplied. It stores the code and state variables (counters, parameters, scripts, etc.) even when the power is off. Their primary purpose is to turn on and off outputs, based on the state of inputs or parameters. PIC controllers are used in most all electronic toys these days, many kitchen appliances, and other cheap (and not so cheap) applications. The code can be written in a variety of languages. I chose to develop in C.
PIC controllers come in a variety of sizes. The one I chose is one of the smaller ones available, a 12F675. It runs at 4MHz, has 1 timer, 1k of program space, 128 bytes of eeprom (parameter storage), in an 8 pin package. It also has 6 configurable input/output pins. Most PIC chips are available in DIP and surface mount packages. I used the DIP variety because they are through hole, and can be soldered by hand.
There are a lot of ways a light may be cycled on and off. Not knowing which would look best in different settings, I decided to have a number of blink modes. I also found that some modes look better when cycling fast, while others look better slow. The circuit and code supports two user inputs, mode and frequency. On the circuit they are selected with small pushbuttons. As a button is pressed, the value increases until it hits it's maximum, then it cycles back around to the lowest value. I could have avoided a lot of time, code, space, and dare I say money (oooh, 2 x $0.15 for pushbuttons) by just hard-coding one mode and one frequency.
For the most part, LEDs can only be on or off. To get them to “dim” or have variable intensity, the power is cycled on/off very quickly. This method is called Pulse Width Modulation (PWM). The chip has one PWM circuit built in, but I chose to implement this feature in code instead of hardware. In the code, the different blink modes are handled with a mode variable. Each mode has a slightly different algorithm that controls how much time the LED is on and how much it is off.
This device has 8 modes including:
sawtoothup // gradual up, drop to off
sawtoothdown // gradual down, jumps to all on
smooth // gradual up, gradual down (pulsate – cool!)
blink // on 50%, off 50%
blip_off // quick on, variable duration off
blip_on // quick off, variable duration on
all_on // all on, variable brightness
all_off // off
All modes except “all_off” use the frequency variable. For the first 5 modes, it controls the speed at which the cycle takes place. They range from several cycles per second (very annoying) to one cycle every 3-4 seconds. The blip_off and blip_on modes have a very short on or off state, followed by a variable duration in the opposite state. When slowed down, the blip_off mode looks like a beacon on a radio tower or light house. The all_on mode uses the frequency variable to control the overall intensity or brightness.
Both the mode and frequency variables are stored inside the chip every time a button is pressed. The variables are stored in 2 of the 128 bytes of eeprom space in the chip. When the circuit is powered up, an initialize function runs. One thing this function does is read these two variables from eeprom. The code/circuit remembers the configuration from when it ran last. Good stuff. I've always wondered how they pulled that off.
There is a lot of sample code available for programming PIC controllers. The code used in this project was pieced together from examples, and code I put together. If you are considering playing with this stuff, I'd recommend downloading lots of sample programs and studying how they work. Mine is included below.
The code on this project compiles to a whopping 400 bytes in size. This compared to the last “real” application I wrote that compiled to just over 23 megs. Given that the 12F675 has 1k of program space, I could stuff a lot more logic in there, control 3 more LED’s, expand the number and complexity of modes, or even implement a scripting language.
Once the code is written, it is downloaded into the chip. The process is called “programming the chip” or “flashing the chip”. There are lots of chip programmers available. I'm using a $38.00 beginner's kit from Microchip.com. It comes with a compiler and a PCB that you temporarily plug your chip into to flash. The programmer has some built in LEDs, a pushbutton and a pot to test out your code. The kit also comes with a couple of chips to start out with.
After the code is downloaded to the chip, the circuit is bread boarded. An input pin is considered “true” when it is shorted to ground. Output pins provide 5vdc at a max current of around 20mA, enough to drive a couple of LEDs or several transistors that in turn can power numerous LEDs.
Components
1 Microchip 12F675 chip $1.05
1 86 ohm resistor $0.04
1 Green LED $0.14
2 PCB pushbuttons $0.15
1 portion of prototype PCB $0.50
Total: $1.88 each
(minus shipping)
This project was really a blast! It has kept me up way past midnight for many nights this week. I haven't had this much fun in a very long time. No smoke and fire. I'm still on my first chip and LED. That's a first. My only mistake (besides neglecting my wife & kids at night, and being extremely tired at work in the mornings) was purchasing a bag of surface mount chips instead of through hole. The vendor eagerly credited my account.
After this, I'll be building on my learning's and following rockin_rick's recommendation of moving up to a much bigger chip for the multi-LED control applications needed on this project. Wish me luck.