Since becoming a dad about a year ago, I find myself strangely drawn to things like Lego, Scalextric and remote control toys "for my son". I also feel that ratherjust than buying toys I want to make personal one-off stuff for him and introduce him into the ways of being a maker himself.
I saw this large Lego figure torch (about 9 inches tall) and felt that it had to be hacked to have a geekier use. So with the addition of a programmable PIR sensor and a few simple components, a motion sensing nightlight was born. Now rather than an on/off switch it will sense if my son wakes up in the middle of the night and gently fade up some LED lighting. Once there's been no movement for 30 seconds then it will fade down again. His room can be completely dark at night and I feel that if he wakes up it's nice that he can see where he is.
I decided this would be a perfect entry to the Hack It contest because it's a double-hack. Not only has the toy been hacked to take a PIR sensor, but the PIR sensor has also been hacked and reprogrammed to do more than it was intended to - it fades the external LEDs up and down gradually using pulse width modulation. If you feel this meets the spirit of the contest then a vote would be nice. However, check out the other entries and vote for one of them too if you think they're good too.
Here's a video of the finished product. You can see it sensing movement and fading up the light. Then I switch it off using his arm as a hidden switch.
Step 1: What You'll Need
Firstly, there's the things you'll obviously need.
- A Lego figure torch like this one. There are a few available, but I liked the construction worker with his hard hat. If you think he's a little bit Village People there always a fireman (hmmm still a bit YMCA) or Darth Vader (that's better), a Stormtrooper or a Ninja. There's even a "plain" figure if none of those work for you. Avoid the dynamo or alarm clock version if you're doing exactly this hack.
- A MSP430 based PIR sensor from Olimex.
Then there are a few little electronic bits and pieces. These are the sort of things that anyone who dabbles with electronics should have lying around anyway.
- A basic NPN transistor
- 1 x 1K ohm resistor
- 2 x 100 ohm resistors
- A small amount of stripboard
- Some wire
- A switch
Finally, you'll need some tools
- A soldering iron (and solder)
- A Dremel or a craft knife if you're careful
- A MSP430 launchpad direct from TI's eStore
- Some superglue
Got all that? Good - then we can begin.
Step 2: Lego Figure Open Heart Surgery
First we have to open up our little Lego man and remove some of his internal organs. Remove the batteries if there are any in there. Pull off his head. Unscrew 4 screws on his back and prise the two halves of his chest apart.
Don't worry. He won't feel anything and we won't do anything that will stop us putting him back together so he looks just like new. I try to avoid crossing that line where the donor item is now "broken" and there's no going back. I this case I managed it. We make no permanent modifications that you can see from the outside.
A square plastic switch and spring will fall out. Put this to one side. Unsolder the wires to the small PCB. Put the PCB to one side too.
His arms will probably fall off too. Keep those. He'll look ridiculous with no arms.
Step 3: Liposuction
We can fit the PIR inside his chest, but it has to poke out the square hole where the ON/OFF switch was. We'll also have to trim away a few insignificant pieces of plastic that are in the way. Don't worry - he can be reassembled without these.
The areas we need to trim are shown in the photo. You'll see that one on his front is in the way of the PIR's onboard button. The other is just in the way of the board. Carefully Dremel (or file or trim) away until the PIR sits nicely on the front piece.
Step 4: Programming the Sensor
There are lots of PIR sensors available, so why did I go with this particular Olimex one? In a word - hackability. The MSP430F2013 microcontroller it contains is fully accessible and can be reprogrammed. The default code simply blinks a red LED when it senses movement, but I wanted something different. I decided to slowly fade in some external LEDs in the Lego figure and fade them back down after a minute or so. Whilst I'm sure this timer functionality would have been possible with some external circuitry, why not hack what's already there?
You'll need two things to reprogram the microcontroller. One is a MSP430 LaunchPad. This device looks a bit like an Arduino. The kit actually contains two MSP430 16-bit microcontroller ICs and the device to program it. We're going to ignore the two microcontrollers for now and use the Launchpad to program the PIR. Do you know what the best bit is? Once you're done with the Lego figure you'll still have 2 more microcontrollers if you want them and it only costs $4.30 including international shipping! That's not a typo. TI must be losing money to get you started using their stuff.
The second thing you'll need is the free Code Composer Studio software to edit, deploy and tweak your code. You can even set breakpoints in code on the device to help you debug. Top stuff.
There's plenty of sites out there with information on working with the MSP430 - my favourite being 43oh.com. I won't turn this Instructable into a MSP430 class, but if anyone wants more information then please ask and I'll be happy to help. It's a little more awkward to set up perhipherals than some other popular boards, but otherwise it's just standard C.
The only unusual thing you'll have to do it remove the existing DIP chip from the LaunchPad board and connect to the PIR. You'll need to connect 4 wires - two for power and two for deploying and debugging. See the photo for details. From then on Code Composer Studio can happily work with the PIR's onboard MSP430F2013. I case you can't see clearly in the photos the connections are as follows looking into the 14-pin header on the PIR with the notch at the bottom:
Top row: n/c, Vcc (orange), n/c, TEST (purple), n/c, n/c, n/c
Bottom row: n/c, n/c, n/c, n/c, GND (grey), RST (green), n/c
Attached you will find "main.c" - this is the source code we'll be deploying. The code does the following:
- Lights the red onboard LED and waits 10s for the PIR and ADC circuitry to settle.
- Sets up the timers, inputs and outputs required.
- Periodically checks for movement
- If it senses movement it flashes the red LED and starts fading in the larger white LEDs in the guy's feet
- If there's been no movement for 30s starts fading down the LEDs
If any of this is not quite to your liking, feel free to take a look at the code and modify it to suit your needs.
Step 5: Our Little Piece of PCB
Using the onboard processor saved us from creating a lot of extra timing and fade-in / fade-out circuitry. If the onboard LED wasn't being driven directly from the MSP430 then we could probably have got away without any, but unfortunately the LEDs in the figure are a little to powerful to be driven this way. They also need to work from 4.5V whereas the MSP430 has an upper limit of 3.6V. We don't need much - just a standard NPN transistor circuit to act as a switch.
As the Lego figure has 3 1.5V AA batteries we can take 3V for the PIR from two of them and use all three for the LEDs. I did consider using 3 x 1.2V rechargeable AA batteries and using 3.6V for everything but decide against it. Feel free to try this if you want.
You can see the circuit on the stripboard in the photo. There's a 6-pin header connecting to the PIR. From the top in the photo we have:
- Ground (with black wire and emitter of our transistor)
- 2 - not used
- 3 - output from the PIR. It's connected by a 1K ohm resistor to the base of our transistor
- 4 - not used
- 5 - not used
- 6 - 3V power to our PIR
You can also see that the red wires to the LEDs is connected to our RED (4.5V) from the batteries. Each of the white wires goes via a 100 ohm resistor to the collector of the transistor so it can be controller by the PIR.
See the schematic for the circuit diagram. To the left are the three connections to the battery. On the right are the connection to the LEDs. Both red wires connect to 4.5V and each white wire connects to its own resistor.
I've wired this up on a tiny piece of stripboard but as there are only 4 components you could easily do it "dead bug" style without a PCB at all. You could even go overboard and etch your own surface mount PCB if you felt like it!
Step 6: A Handy Replacement Switch
Well, it's all coming together, but one piece of the man we've had to ditch is the power switch. I considered a few options for a replacement but I really wanted something that didn't ruin the look of it and preferably didn't leave a hole if I backtracked on the project. Then I had an idea - use one of his arms.
It took a bit of fiddling around, but I managed to find just the right sort of switch (a PCB mounting push button) and worked out where I could mount it to be operated by the arm and not get in the way of anything else. I had to cut away some plastic from the back piece near the guy's left arm and super glue the switch in place. (See first photo.)
I also needed to trim some corresponding material from the front piece. (See econd photo.) Finally I filed way a piece of the arm so the the switch wasn't pressed when his arm was down, but is pressed in any other position. If you use a different switch then you may have to do things slightly differently. What really matters is that you can disconnect the 0V wire when you want. I used my trusty Dremel but hack, file or melt away as you see fit.
I was very pleased with the result.
Step 7: Stitching Him Back Up
Now we have to solder the circuitry and put it all in place.
We're connecting to the battery in 3 places. Firstly there our ground connection - the black wire. This goes via our main switch (the arm) to our little PCB. It goes to the left-most pin on the PIR sensor. Then we have 3V to supply the microcontroller. This is the purple wire. It goes to the right-most pin. Finally we have 4.5V to supply the LEDs only. It connects to the red wires running to the LEDs in his feet.
You'll need to cover the 3 lower battery connections with some electrical tape. Also the bottom of our PCB for good measure. Once it's assembled they've a bit close.
To reassemble our guy first seat the PIR sensor so it fits neatly in the square where the original button went. Then put his arms in place. Next connect his legs to his torso. Then carefully join the front and pack piece of his chest. Finally pop on his head to hold it all in place and do up the four screws.
Your figure should now be a working motoin sensitive light. Lift his arm and check if it works. You should see a red glow for 10s and then the fun begins...
Step 8: All Done!
The final result turned out perhaps a little better than I expected. I was expecting to have to use hot glue or Sugru to hold the PIR in place, but with a bit of filing it's a fairly snug fit in the hole left by the original switch. Using the arm to turn it on worked first go. I half expected to need some bodging there too.
My son loves it. It isn't so sensitive that it's operated by him rolling around in his cot, just when he stands up. I know from a previous project using the same PIR that a CR2032 coin cell lasts about 4 months. The AA batteries should last WAY longer. The MSP430 is designed to be very low power and spends 99% of its time in deep sleep mode, waking occasionally to check for movement. The LEDs will draw far more power than the microcontroller.
The best thing is that the functionality isn't fixed. If I want to extend the "ON" time then I can. (Or you can - my source code is included so feel free to adapt it.) I've left the standard flashing red LED for movement but could turn this off if I wanted.
Most of all I hope you've enjoyed reading this Instructable and are inspired to give it a go. Feel free to ask any question or add any comments.