This microcontroller-enhanced locket makes a great present and in this instructable you will find how to build your own. While implementing this project you will also learn all basic things required to program PICAXE-08 microcontroller and find out how to get 3 PWM channels from a microcontroller that has just one.
Step 1: Video Overview
Check out this video for a quick demo and build process overview.
Step 2: PICAXE-08 Microcontroller
Why PICAXE-08? Well, this is the simplest beginner-friendly microcontroller out there. It doesn't require anything sophisticated to program it, it's cheap, it uses programming language called Basic, which associates with the simplest programming language possible in most people's minds.
Step 3: Get Parts for PICAXE-08 Programmer
So let's first program our microcontroller. We will need the following parts:
- the PICAXE-08 microcontroller itself. Surprisingly you can get this sophisticated piece of microelectronics for about only $3!
- a basic breadboard
- 10K resistor and 22K resistor. I recommend you buying a variety pack from Radioshack. It contains dozens of resistors of different nominal.
- either a DB-9 connector or 3 wires to replace it. If you choose to go with wires just twist them around needse on one end so you can connect it to the serial port connector pin. Some heatshrink will make this contact more reliable. DB-9 connector is by far more convenient option though.
- bunch of wires good for breadboard (with solid core, not stranded)
- if your computer doesn't have a COM port (and I'd be surprised if it is so old that it has one) you will also need a USB-to-RS232 convertor. We already used it in episode #3 to connect voice-controlled robot to the computer and will use it frequently in the future.
- A common-cathode RGB LED. RGB LED is a combination of red green and blue LEDs in single package, so it has 4 wires instead of 2: one common cathode wire and 3 anodes for each color/LED.
Everything from the list is available from RadioShack or eBay and should not cost you more than $20 total. Once you get all the parts assemble them according to the diagram.
Step 4: Assemble the Programmer
Please note that
* The RGB LED must be of common cathode type, not common anode, not 2-contact one. This is very important
* Input voltage is approx 3V (2xAA). PICAXE-08 specs recommend 4.5V, especially for programming it, but I found it works even from 2.8V (two rechargeable batteries) just fine and you can even program it with this voltage applied.
* RGB LED is not a part of the programmer, it's for our project only. You don't have to remove it while programming the microcontroller.
* DB9 connector is F-type (the one with holes, not pins)
Step 5: Download Integrated Development Environment (IDE)
Step 6: Write (or Copy) the Program
What's cool about this code is that it drives RGB LED and independently dims every color. Typically this is achieved by using PWM (Pulse Width Modulation) and most microcontrollers have special hardware to produce PWM signal. PICAXE-08 also has this capability, but it is only available on one channel. So this program generates PWM signal for three colors software way.
----------------- CUTLINE ----------------- 8< -------------------- CUTLINE --------------------
Simple PICAXE 08m basic program that randomly and independently controls
brightness of all 3 colors in a RGB LED
creating calming patterms (like in a mood lamp).
See my blog http://rtfms.com for more details
You're free to do whatever you want with this code, just mention my blog http://andrey.mikhalchuk.com
in the comments, so people can find updated code. Thanks!
symbol SPEED = 3 ; the smaller the value the higher the color change speed
symbol SPEED_VARIATIONS = 7 ; defines how many different color change speeds should it use
; map pins
symbol RED0_PIN = 0
symbol GREEN0_PIN = 1
symbol BLUE0_PIN = 2
; map pin values to mem
; pin value is the current brightness of the LED
symbol red0 = b0
symbol green0 = b1
symbol blue0 = b2
; map pin_deltas to mem
; delta is the speed of the brightness change
symbol red0_delta = b3
symbol green0_delta = b4
symbol blue0_delta = b5
; temp values for the subroutine
symbol delta_w = w3
symbol tmp = b7
; subroutine parameters
symbol pin = b8
symbol val = b9
symbol delta = b10
; initialize everything
red0 = 0
green0 = 70
blue0 = 200
red0_delta = 1
green0_delta = 5
blue0_delta = 9
; start servo mode
; note that "servopos RED0_PIN, 255" renders LED off!!
; "servopos RED0_PIN, 0" makes it really dim, but lit. Is that a bug in PICAXE?
servo RED0_PIN, 255
servo GREEN0_PIN, 255
servo BLUE0_PIN, 255
; this code is like loop() in arduino
; emulating function call in function-less environment
pin = RED0_PIN : val = red0 : delta = red0_delta : gosub set_color_val : red0 = val : red0_delta = delta
pin = GREEN0_PIN : val = green0 : delta = green0_delta : gosub set_color_val : green0 = val : green0_delta = delta
pin = BLUE0_PIN : val = blue0 : delta = blue0_delta : gosub set_color_val : blue0 = val : blue0_delta = delta
; this sub adjusts the brightness of the LED and delta
val = val + delta
if delta < 128 and val < delta then ; fwd
delta = delta % SPEED_VARIATIONS + 1
delta = 255 - delta
val = 255
elseif delta >= 128 and val <= delta then ; reverse
delta = delta % SPEED_VARIATIONS + 1
val = 0
tmp = val - 1 ; servopos bug workaround
; yeah, servopos takes only constant as the first argument :(
if pin = RED0_PIN then
servopos RED0_PIN, tmp
elseif pin = GREEN0_PIN then
servopos GREEN0_PIN, tmp
elseif pin = BLUE0_PIN then
servopos BLUE0_PIN, tmp
; see my other blog http://rtfms.com and video blog RTFMs on youtube for demo
; this is covered in episode #7: Microcontroller Meets Jewelry
Step 7: Configure AXEpad
Now connect the programmer we assembled to the computer using the USB-to-RS232 converter and go to View->Options menu in AXEpad. Open the Port tab and select the last port in the list.
Now switch to the Mode tab and select microcontroller type PICAXE-08M. Hit Firmware button. In a few seconds a dialog will appear telling you if you AXEpad can communicate with the microcontroller or not.
If an error dialog will popup - try a different port.
If you tried all ports with no luck please refer to the complete troubleshooting guide provided in my blog http://www.rtfms.com/rtfms-7-microcontroller-meets-jewelery.htm
Step 8: Compiling the Program and Writing It to the Microcontroller
If everything went well then you will see the LED start changing color like in mood lamp. If not, again, refer to the troubleshooting guide .
As you can see in the final dialog only 146 bytes out of 256 available on the microcontroller are used by the program. That means you still have memory to enhance the mood lamp effect the program implements. Or you can write your own completely unique effect.
Step 9: Fitting All Components Inside the Locket
Step 10: Miniaturize the LED
Step 11: Miniaturize the Microcontroller
The microcontroller also can be trimmed from both sides. You can remove approximately half mm of plastic from each side. Before doing that make sure you mark the the first pin by making a notch on the key side of the chip. The original marking will be gone with the excessive plastic. This procedure essentially turns DIP package into a SOIC-like package (hint: you can also buy PICAXE-08 in SOIC package, it's just not very suitable for the breadboard).
Step 12: Assemble Everything
Step 13: Adding the "On/Off Switch"
But what about some kind of switch for this device? Cut the wire connecting the battery to the microcontroller and expose its ends. The locket is made of metal, so to turn the microcontroller on you just need to put this package inside the locket. The locket will shortcut the wires and let the current flow to the microcontroller.