In this instructable I will describe the electronics part of of a "laser gun", Hollywood style. As for the mechanical setup and cool looks, I will leave that entirely up to your fantasy. The image attached is just for your inspiration and borrowed from user MF99K on deviantart.
Here is my wish list of what I wanted the electronics part to do:
- Play a "PeePeePeeuuw" sound when triggered
- Shoot (AKA flash) a red laser module in sync with the sounds
- Powered from a single 3.7 volt LiPo cell without complex voltage converters, no standby
- Include a safe recharging circuit
- Use a cheap laser module
- Be reprogrammable (sound, flashing)
- Be reasonably loud
In the next few steps I will guide you through all these requirements and how they are solved.
Part list (all obtainable through Ebay)
- ATtiny85 and a platform to program it
- LiPo battery
- TP4056 based charger PCB
- PAM8403 based amplifier PCB
- Small speaker
- Laser module
- Transistor 2N2222 or equivalent
- Two 1N4148 diodes
- Resistors (18R, 1K5, 4K7, 18K)
- Capacitor 4.7 nF
None of the values are critical, with the exception of the 18 ohm resistor.
Step 1: Power Source
As power source I used an old camera LiPo cell. Basically all old cells from camera's or phones will do, as long as they are 3.7 volt LiPo cells. Raw cells usually have two contacts, packaged cells either have a temperature sensor included or sometimes even a complete battery management system on a a chip. What you need is a raw cell as we will add a stand alone LiPo charger in the next step. If your package, like mine, has more contacts you have to figure out which contacts are connected to the cell inside. Easier is a raw cell such as in the picture. Look for quadcopter cells.
If you're shopping on Ebay, buy a cell that already has leads attached so you don't need to solder on the battery itself. If you need to solder the battery, be very careful and very quick: LiPo cells are not supposed to be soldered and can fail spectacularly (read: fire) when overheated.
The voltage of a LiPo, while nominally specified as 3.7 volts, can actually vary from somewhere between 3.6 and 4.1 volts. As the rest of the laser gun is designed to run entirely off this voltage range, there is no need to include anything else but a charger
Before we continue, a word of caution: these cells pack a huge amount of energy and they have a very low internal resistance. Any shorting will immediately create smoke, heat and a fire will quickly develop. The fire will be very hard to extinguish. It is very wise to include a fuse in your design.
Step 2: Charger
To charge the cell we will use a complete cheap PCB you can easily obtain from Ebay. Look for a TP4056 and you'll find dozens of sellers. Most will sell for less than a euro. All logic is included and the PCB's are sold with a mini USB or micro USB connector already mounted. It is simply a matter of connecting the plus and minus connections to the LiPo battery.
The boards will normally charge at a maximum of 1 Amp. This is OK for almost all LiPo cells. I have seen tiny 600 mAh cells that can be charged using 9 Amps in 4 minutes. 1 Amp is fine and a cell like the one shown in the previous step will be charged completely in just over half an hour. A cell from a camera or phone will take longer, but will also run longer.
Step 3: Laser Module
For the laser I have used a cheap 650 nm (red) module. I bought 5 for 2 Euros. They are available for 5 volt and 3.3 volt, but as we will modify them to use the variable voltage of the battery, it's not important which one you choose.
Laser diodes are extremely sensitive for over-current. These cheap modules shown above use a simple series resistor to limit current to 30 mA: 82 ohm for 5 volt versions or 33 ohm for the 3.3 volt version. With variable voltage, a series resistor does not keep the diode current constant, so I've shorted out the tiny series resistor installed on the back side of the module and made a constant current source tuned to 30 mA. The transistor was needed anyway as we want to switch the laser module on and off from a micro-controller that is only rated for 20 mA.
If the output of the controller is switched high, a current flows through the 4K7 resistor and the two diodes to ground. The two diodes act as a fixed voltage reference: they will maintain a voltage of 1.2 volt on the base of the transistor. The transistor will create an equilibrium around a base-emitter voltage of 0.7 volt, so consequently, it will maintain a voltage over the 18 ohms resistor of 0.5 volt. Ohm's law dictates a current flowing through the resistor of just under 30 mA. As the current through the base can safely be ignored, the 30 mA will flow through the collector and the laser diode, independent of the battery voltage.
Step 4: Microcontroller and Code
To control the circuit, I chose my favourite microcontroller, the ATtiny85. It has just enough processing power and corresponding peripherals to generate sound and flash the laser. Although a standard ATtiny85 is not rated for 3.6 volt, it will run without problem on a LiPo cell. Also, it will clock and boot itself nicely without any external components.
The main part of the code is based on work done by David Johnson-Davies - www.technoblogy.com, see his post dated 29th September 2014. I could have programmed the sound using the on-board timers, but I used his "Audio Sample Player" instead, so with some effort you can program entirely different sounds.
The sound player is written very cleverly and uses about everything possible you can get out of this tiny processor. It runs entirely interrupt driven, and the interrupts are triggered by an internal timer.
NOTE: THE SOURCE CODE WAS PREVIOUSLY PUBLISHED ON CODEBENDER, BUT THEY CLOSED. I HAVE INCLUDED THE SOURCE CODE AS A ZIP FILE SO IT CAN BE USE IN THE ARDUINO IDE
The code plays a "Peeeuuwww" sound that I created using Audacity software, downsampled using Ubuntu's Sound Converter and then arranged into a chunk of C-code using xxd. Also, the code restarts two times after it has played roughly 125 ms, so it sounds like "PeePeePeeuuw".
The audio PWM output is played on pin 2 and on pin 3 in reversed polarity. The output stages of the ATtiny85 can only drive 20 mA, so only a high impedance speaker (250 ohm or more) can be connected between those pins. I wanted a reasonably loud sound, so I added a simple low pass filter and attenuator, consisting of a 18K and 1K5 resistor and a 4.7 nF capacitor and fed that signal to an amplifier module.
While the sound is played, pin 7 controls the laser driver of the previous step in sync with the sound.
Now you do need some sort of device to put your programs (or sketches as they are called in Arduino parlance) into the chip's flash memory. Such devices are called ISP's. This instructable is not about how to build yourself an ISP nor how to use the Arduino IDE with it If you're new to programming ATtiny85 chips, please read my "Butt Light" instructable, as it contains links to building your own ISP and how to use the Arduino IDE to compile and upload your firmware.
Step 5: Amplifier
I bought a class D 5 volt stereo amplifier module on Ebay for less than a euro. It can deliver up to 3 Watt per channel, and we're using only one channel here. The amplifier is build around a PAM8403 chip. It is surprisingly powerful and runs nicely on the LiPo cell.
The output of the amplifier is connected directly to a tiny 8 ohm 0.5 Watt speaker. This can easily be fried, but as long as the gun is not continuously fired, the short overloading is fine. As the amplifier is stereo, you can decide to use two speakers and simply connect the two input pins. Glueing the speaker(s) in some sort of resonating enclosure will improve the loudness and sound quality significantly. I used a 3 cm long piece of PVC pipe. This is of course pretty dependant on what you've created as "gun".
I wanted the entire circuit to be completely disconnected from the battery when not in use. The amplifier is the only part that needs a short start-up delay, so the code waits a hardly noticeable 125 ms before playing the sound and flashing the laser.
A friend of mine who rebuild this ended up with an amplifier board like mine, but it had a bigger rated start-up capacitor. This resulted in the project only playing "- - - uuwww". As we're not worried about the power-on click, the solution was to simply de-solder pin 8 of the amplifier chip from the board. This pin is connected to the start-up capacitor, which has it's other pin connected to GND. If you want to be sure this is the problem before fiddling with the amplifier board, power everything up, then reset the ATtiny85 by connecting it's pin 1 to ground for momentarily. If you now hear the full "PeePeePeeuuwww", you know the startup time of the amplifier chip is set too high for this project and it's not some other problem.
Step 6: Done!
That's it! The complete schematic is listed in step 1 for easy reference. Wire it all up in a nice enclosure. For some advanced fun, you can code some misfiring, play silly sounds, or whatever you can come up with. Have fun!