Introduction: Magic Locket
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
As you can guess there is a microcontroller inside the locket. The microcontroller and an RGB LED are responsible for all this light show. For this simple project I chose PICAXE-08.
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
This is how it looks like when assembled (it looks slightly different from what you saw on the breadboard diagram, but electrically it is the same).
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)
Next you need to download PICAXE integrated development environment. This page lists a bunch of them: http://www.rev-ed.co.uk/picaxe/software.htm . Personally I like multiplatform AXEpad, but if you like nice diagrams more than boring text you might prefer PICAXE Programming Editor Software. Both are free for home use.
Step 6: Write (or Copy) the Program
You can write your own programs to implement different visual effects for the RGB LED, but if you're just starting with microcontroller programming it's more convenient to start with an example. For instance you can copypaste the code below into the IDE window.
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
Everything is ready to send the code to the microcontroller, so hit the Program toolbar button. New popup window will appear and tell you what’s going on. A few seconds later this window will disappear and new dialog window will tell you if the programming process went well or not.
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
So the prototype works pretty well, it’s time to pack everything into the locket. And here we face another challenge: the microcontroller, the LED and pair of very small batteries won’t fit the locket! And we also need to include some kind of switch with all this.
Step 10: Miniaturize the LED
As you can see it’s not trivial to fit all the parts into the tiny locket and requires some work with a file, sidecutters and sanding paper. Remove all platstic you can from the LED without compromising its integrity. I ruined 3 LEDs before getting satisfactory result, but check out how small the LED became comparing to the original one. It also became diffused, thanks to sanding paper.
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
Finally add two contacts for the battery and wrap everything with heat shrink. The white paint you see on the picture reflects the LED light making it more even.
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.
Step 14: The End
Here my project ends and yours starts. You can play with different program parameters or implement your own color changing scheme. In fact in one of the future instructables I'll explain how to make jewelry interactive using this approach. So follow, subscribe and read more interesting projects. Good luck!
First Prize in the
DIY Wedding Challenge