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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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: . 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 for more details

You're free to do whatever you want with this code, just mention my blog
in the comments, so people can find updated code. Thanks!

#picaxe 08m
setfreq m4

; config
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
goto main

; this sub adjusts the brightness of the LED and delta
val = val + delta
if delta < 128 and val < delta then ; fwd
random delta_w
delta = delta % SPEED_VARIATIONS + 1
delta = 255 - delta
val = 255
elseif delta >= 128 and val <= delta then ; reverse
random delta_w
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
pause SPEED

; see my other blog 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

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!

DIY Wedding Challenge

First Prize in the
DIY Wedding Challenge

Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    36 Discussions


    6 years ago on Introduction

    awesome instructable, just one concern. I have a 08m2 microcontroller, because i couldnt find a 08m. i programmed it the same way it was written here, and it went though, but it only stays on 1 color with the program installed (its a mix between r&g) and when i take off the program, it turns blue. if you could please help me figure out a solution for this it would be much appreciated.

    1 reply

    Reply 6 years ago on Introduction

    Hi DirtyMax,

    Unfortunately I don't know exactly what's happening in this case, but here are my guesses:
    - wiring. Is there a possibility that the LED is hooked up to the controller incorrectly or of a wrong type? There are ones with common anode and common cathode. Also LEDs have different characteristics, many require different voltage on different pins.
    - the clock speed. This is a sensitive parameter, please try to change it
    - can you check that all colors are working in the LED? For instance use a battery to turn the colors independently. Please refer to the LED spec for correct voltage for each color.
    - Try using a multimeter or, much better, oscilloscope to verify that the microcontroller outputs correct voltage or signal on each pin connected to the LED.

    These are my best guesses, unfortunately I don't remember all the details about this project, I made it a really long time ago.



    7 years ago on Introduction

    Hi mikhalchuk,

    Great project! :)

    Just got a question about using the servo and servopos commands in the code.

    So the servo command produces 20ms pulses / 50Hz.

    So servopos 2, 75 would produce a 50Hz signal with 3.75% duty cycle

    and servopos 2, 225 would produce a 50Hz signal with 11.25% duty cycle

    So none of LED pins ever get to 100% DC / fully on?



    7 years ago on Step 3

    Cant we just use the disconnect commant and then remove the 10k resistor as a pull up.


    8 years ago on Step 3

    How to connect DB-9 connector... On bread-board...??
    dis is my 1st time so i need ur help plz...
    tell me...

    Thank you

    1 reply

    Reply 7 years ago on Introduction

    probably the easiest way is to solder some solid (not stranded) wire to the 3 pins you need from DB9 and insert them into the breadboard. DB9s unfortunately not breadboard-friendly as is.
    BTW, Sparkfun also sell breadboard-friendly breakout boards for DB9.


    8 years ago on Introduction

    Don't think this will work, the locket is too small for what has to go into it. I 'm pretty sure the guts are to big. How can you prove it will work? This is also not workable to those who do not have the wherewithal to complete this project as shown. Too technical for your everyday instructable folks. Nice locket though!

    1 reply

    Reply 7 years ago on Introduction

    Sure, check out the video. Some sanding paper and a file help to fit large chips into small lockets :)
    In fact if you manage to solder SMD version of PICAXE you can fit it inside easily.

    I agree with "too technical" for most people, but I found a lot of very smart and skilled people here on instructables doing much more complex things. So hopefully someone will use this to make a cool present to the loved ones.


    7 years ago on Introduction

    This is a very cool instructable! Thank you for sharing.
    My question is: Do you know how long the battery will last in this configuration and with the programming used to make the instrucable?
    Thx! Davercles

    1 reply

    Reply 7 years ago on Introduction

    Thanks Davercles,

    I didn't do full-blown testing, but it sure can run for a few hours based on tests I made.



    7 years ago on Introduction

    Hi Mikhalchuk , I cannot find in Romania the Picaxe microchip. and it's kind of costly to get it from UK , as I only want to build one . I have at my disposal one 12f675 microchip can I use this one instead ? how will the programmer need to be changed? Thank you

    1 reply

    Reply 7 years ago on Introduction


    Sorry, I don't know that as I'm not a PICAXE guy (I like atmels more). My guess would be that exactly the same programmer will work for 12f675, you just need to check the pinout for that chip and map it to PICAXE. I could be wrong though.



    7 years ago on Introduction

    Very nice little project!
    The 08M isn't quite fast enough to do full control of brightness using mark / space ratios without flicker but using the servo command as you've done is a neat way around it, even though it doesn't give you control up to full brightness. 
    You're actually running with a very low mark-space ratio - 0 to 2.55 ms in a 20ms frame, but you've no current limiting so it all averages out and the main thing is - It works, it fits and it's bright enough.  ( Who needs design rules anyway #;¬)

    With Valentine's day coming up I may do something similar using a flat square 'Pirhana' type LED.

    3 replies

    Reply 7 years ago on Introduction

    Something you don't mention anywhere is connecting pin2 of the PicAxe to 0V once it's removed from the breadboard. This is important as letting it float makes the micro prone to random resets.
    Watching the video more closely I see you do mention shimmering. If that's a problem then upping the frequency (setfreq M8) would reduce it. (But you probably know that already #;¬)


    Reply 7 years ago on Introduction

    Well, the frequency is actually set somewhere in the top of the code and it's easy to change, but I decided to stick to 8MHz hoping this will let it run longer from the tiny batteries I use.
    Thanks for the floating pin note!


    Reply 7 years ago on Introduction


    Thank you for the comment and useful info.

    Magic locket-2 is coming, it will be even better. I hope to have it published here soon.



    8 years ago on Introduction

    Awesome idea. I live in Perth WA and wouldn't have a clue where to get all of these parts. Do you make and sell these? If so can I get 2 of you