Get a free year on Tuts+ this month when you purchase a Siteground hosting plan from $3.95/mo
Working with LEDs can range from the simple LED throwie to advanced full-color wall-matrix displays. This project looks at the world of addressable RGB color LEDs and how to use them in fun electronics projects. In this tutorial I'll show you how to construct an RGB flashlight where the color of the light, brightness, and blink speed are all controllable with dials on the flashlight.
I'll show you how to use an Atmega Tiny85 platform for low cost Arduino compatible projects, work with addressable RGB LEDs, read and use the values from potentiometer dials and toggle switches, and learn a few basic tips for working with electronic components in a project off the breadboard.
Gather the Components and Tools
- 16 LED Neopixel Ring
- 3.3V Trinket (Arduino compatible mini-microcontroller board)
- Three Thumb-wheel Potentiometers
- On/Off Rocker Switch
- Mode Toggle Switch
- JST Jumper Wire
- Lipo Battery
- Lipo Battery Charger
- 26-Gauge stranded hookup wire (red, black, yellow, green)
- A Prototype Strip Board with copper cladding connecting rows
- Heat shrink tubing
- Hobby Knife
- Drill with 5/64 inch bit
- Hot glue gun and glue
- Wire stripper that can strip 26-gauge wire
- Computer with Arduino IDE for programming Trinket
- USB A-Male to Mini-B Cable (for programming Trinket)
- Needle-nosed pliers
- Fine-point permanent marker
- Soldering iron
- Helping hand tool
- Utility shears
- Electrical tape
By following the instructions for this project you will create the circuit, as shown in the diagram below, inside the body of a flashlight and without a breadboard.
The theory behind it is simple. The Arduino-compatible Trinket board will read the values of the potentionmeters. It will use these values to manipulate the color, brightness, and blink rate of the LED ring. The toggle switch will bring another pin on the Trinket high and low to switch modes of the flashlight from color changing mode to brightness and blink mode.
Examine the diagram, below, and notice how the components are wired to the Trinket. The instructions will lead you through installing the circuit into the body of a flashlight. Before you go any further though, take the lipo charger and start charging you lipo battery from a USB port. You'll need this charged in a few steps.
Prepare the Flashlight Body and Reflector
For this project you will be stripping out the flashlight insides and using the body to house the project. In this stage you will be preparing the flashlight to become your new creation.
Prepare the Flashlight Body
- Unscrew the ring holding the clear lens and the reflector in place. Set aside the ring, lens, and reflector for the next half of this stage.
- Using the needle-nosed pliers remove the switch from the side of the flashlight and the battery spring from inside. If you used the model linked above in the components list, use the pliers to push the tab on the inside of the switch outward until the switch comes loose. A little force will be required. Snip, or pry, the metal components from inside the flashlight until you have just the empty plastic body.
- Set each of the components onto the flashlight body and mark where the component pins will go through the plastic with the marker. The layout I used looked like the images below.
- Drill the the holes in the body that will allow the pins to pass through
- Cut the rocker switch hole out using the hobby knife. The switch I used had a fin on one side and I had to notch out the circle to accommodate it. Don't fit the parts together yet. You will have soldering to do before assembling it.
Prepare the Reflector
- Center the LED ring onto the reflector rim
- Mark the location of the IN, VCC, and GND pins on the rim of the reflector using the marker
- Drill a hole in the cup of the reflector to allow a wire to pass through to the LED ring. Ignore the fourth hole in the reflector. I got a little carried away. You will only need the three mentioned above.
Solder the Wires Onto the Components
Each of the components will need hookup wires attached. Various techniques can be used to connect the stranded wire to the pins of the components and to each other. Here are a few tips:
- Before soldering stranded wire to a pin, tin the wire by applying a light coating of solder to the wire first
- Use a helping hand tool to hold the wire and components together so you can hold the solder and iron in your other two hands
- When using a helping hand tool to hold the wire clip the wire an inch or two away from the solder point or the heat from the solder will make the PVC coating soft and you can get clamp marks or even holes in the wire
- Use heat shrink tubing wherever you can to cover splices and connections. This will prevent accidental shorts and act as strain relief
- Do NOT apply heat shrink tubing to the wires before you thread them through the body of the flashlight or they will not fit. Unshrinking the tubing is harder than you would imagine, so plan ahead
- Use a lighter or heat gun to shrink the tubing (when it is time)
A good helping hand tool is a must for projects where you will be splicing and soldering components together without a prototyping board. I highly recommend the SparkFun Third Hand because it is stable and easy to use. You can also choose to build it yourself based on the Instructable link on the product page if you are so inclined.
- Cut three 4" lengths of black, red, and yellow wire each and strip 1/4" off of the end
- Solder the red, yellow, and black wires onto the thumbwheel potentiometer where red and black are voltage and ground respectively. Resistors are not polarized so it does not matter which pin is voltage and which is ground, but be consistent
- Solder the yellow wire onto the output pin of the potentiometer, the center pin of the part listed above. This wire will carry the adjusted voltage to an analog pin on the Trinket so it can read the value as you change it
- Cut, strip and solder a 4" red wire to one flange of the rocker switch. The rocker switch will interrupt the battery supplied voltage. In many components it is helpful to attach to ground first when applying power. Switching the voltage supply line accomplishes this
- Cut the JST jumper wire in half and stow the male end. You will not use it for this project
- Pull the red and black wires apart on the JST jumper with the female JST socket so you have about two inches separated
- Solder the red lead from the female JST jumper to the other flange of the rocker switch
- Cut and strip a 4" length of red, black, and green wire
- Solder the red and black wires to the VCC and GND holes in the LED ring from the back so the wires extend away from back side of the ring
- Solder the green wire to the IN hole from the back
- Thread the wires through the holes in the reflector
- Make sure the LED ring is centered on the reflector and put a blob of hot glue on the back of the reflector where the wires come through so it holds the wires and subsequently the LED ring in place
- Cut and strip a 4" length of red wire
- Solder the red wire to one pin of the toggle switch
- Cut and strip a 2" length of black wire
- Solder the black wire to the other pin of the switch
Attach the Components to the Flashlight
Before soldering the components to the Trinket, the wires will need to be fed through the wall of the flashlight. Follow the steps below to attach all of the components to the body of the flashlight.
- Gently thread the wires for the potentiometer through the holes in the flashlight pulling the wires out of the mouth
- Put a small drop of hot glue on the underside of the potentiometer and firmly press it to the body of the flashlight
- Repeat for all three potentiometers
- Use the same process to place the toggle switch onto the body
- Feed the wires for the rocker switch through the hole you cut and press fit the switch into place
Complete Assembly Soldering
The toggle switch is going to connect the 3.3V power to a pin on the Trinket that is set to INPUT mode. When the electricity is allowed to flow, the pin will register as HIGH and the program will switch the mode in which the flashlight is operating. The resistor is connecting the pin on the Trinket to ground to pull down the pin so it doesn't pick up transient voltage changes and decide to randomly get high.
- Solder one resistor to the black wire from the toggle switch. If you are feeling adventurous you can try using the NASA style but I found a straight splice works just fine as well
- Slide heat shrink tube over the resistor and up the wire. This will be used to cover the resistor splice, the resistor and the splice on the other end of the resistor so a two inch piece may do. Use a tubing size that will slide over two wires together. You'll see why in a in the next steps
- Cut and strip a 3" section of green wire and black wire
- Twist one end of the green and black wire together and tin with solder
- Solder the other lead of the resistor to the tinned green and black wires you just prepared. This will make a Y connection
- Slide the heat shrink tube over the resistor and Y splice
- Gently wave a flame beneath the tubing until it shrinks into place. Be careful not to burn the tubing or the PVC coating of the wire. It smells bad and the fumes are no good. Don't ask me how I know
While having unregulated power sounds really exciting, it is not good for electronics. The LED ring and the Trinket can accept the raw battery voltage from the switch, but the rest should be regulated down to 3.3V by the Trinket's on board voltage regulator. The steps below shows you how to splice together the unregulated power leads.
- Slip a 1" section of heat shrink tubing that will cover two wire widths over the red wire lead from the rocker switch
- Cut and strip a 3" section of red wire
- Twist the rocker switch red wire end together with the red wire you just cut and tin them with solder. The loose wire will go to the Trinket eventually
- Splice the two wires junction you just made with the red lead from the LED ring
- Slip the tubing over the splice and shrink carefully
- Solder the loose red lead from this Y splice in the BAT+ hole on the Trinket. When you do this, slip the wire tip into the Trinket from the bottom. Leave a little space between the trinket board the and PVC coating of the wire. If you press the PVC coating up firmly against the back of the Trinket you stand a chance of melting the PVC into you joint. Again, don't ask
There should be four remaining power leads dangling from the mouth of the flashlight. These will need regulated power.
- Cut and strip a 4" section of red wire
- Solder one end to the 3V hole on the Trinket
- Cut one row of the proto board making sure that the row is connected with copper cladding. A hefty pair of shears will make quick work of the proto board.
- Cut a 5 hole piece off of the proto strip
- Solder the red wire from the Trinket into the first hole of the proto board. Feed the wire through the back so you are soldering the metal end to the copper of the board
- Solder the remaining 4 red wires to the proto board strip. Make sure to untangle them and route them carefully before soldering
- Wrap the exposed wire and strip in electrical tape to prevent this power bus from shorting when it is pushed into the body of the flashlight
There will be 6 black ground leads from the flashlight. These need to be grounded together with the Trinket.
- Cut a seven-hole piece of the proto strip
- Cut and strip a 4" black wire
- Solder the wire to the GND hole on the Trinket similarly to the way you solder the red wire
- Solder the other 6 black leads to the ground bus
- Cover with electrical tape
Signal Leads to the Trinket
At this point you should be left with two green (one from the LED ring) and three yellow wires. The green wires are digital connections for the LED ring data control and the HIGH/LOW toggle switch. The yellow wires are the analog voltage level from the potentiometer that the Trinket will analogRead(). The steps below describe where to connect them.
- Solder the green wire from the LED ring to the #1 hole on the Trinket
- Solder the green wire from the toggle switch splice to the #0 hole on the Trinket
- Solder the yellow wire from the potentiometer that you designate to control the red color value to the #2 hole on the Trinket. In my build this was the left most potentiometer. I setup red, green, blue controls from left to right
- Solder the yellow wire from the green control potentiometer to the #3 hole on the Trinket
- Solder the yellow wire from the blue control potentiometer to the #4 hole on the Trinket
All wires should now be soldered and accounted for. Before stuffing the lot into the flashlight you will program the Trinket and test the build.
Program the Trinket
The Trinket is similar to the Arduino family of boards because it uses an Atmel chip, specifically the ATtiny85. This is a smaller chip than the larger Arduino board cousins use but still quite powerful.
To get the cost down to something that could be soldered into a project and left, the manufacturer left off some of the more expensive parts that make up the cost in something like an Arduino Uno. Because of this, a few more steps are required to program the Trinket. Fortunately there is a brilliant tutorial on just how to setup your existing Arduino IDE or install a pre-fixed version. Follow the instructions in the tutorial and push the sample blink sketch to the Trinket. Ensure you are comfortable with pushing a sketch to your Trinket successfully before proceeding.
Once you have successfully pushed a sketch to the Trinket open the rgbflashlight sketch that is in the zip package with this tutorial. You can also find it and the latest Fritzing diagram at the rgbflashlight github repository.
Notice that the pins defined in the sketch for the red, green, and blue pins are not the same as the silk screen holes to which the leads were soldered. This is certainly confusing, but if you refer back to the tutorial on programming the Trinket and read the fine print, you will see that the analogRead function on the Trinket platform numbers the analog pins differently. Don't ask me how much time I wasted before finding that little gem.
Power Up and Test
You are now ready to connect the battery power and test the assembly.
- Disconnect the programming USB cable
- Make sure the rocker switch is in the off position
- Connect you lipo battery to the JST connector on your project
- Flip the rocker switch to the on position
What happens next will depend on the orientation of the potentiometers and the toggle switch. After a couple seconds the lights should come on. The two types of behavior you should expect are:
Toggle switch open (no current passing): In this mode, the dials will control the red, green, and blue saturation levels by turning them. Try them out.
Toggle switch closed (current passing, pin is HIGH): In this mode, the dial that controls the red color in the other mode now controls the brightness of the LED ring. It gets VERY bright. The green dial controls how fast the LED ring blinks. Have some fun with this.
Now that you confirmed that it is all working it is time to put it together.
Assemble the Flashlight
Make sure the rocker switch is off before starting.
- Carefully fold the wires into a bundle and slip them into the body of the flashlight. Make sure there is no unintentional contact with exposed wire. If you used the heat shrink tubing and electrical tape as suggested you should not have to worry
- Slide the battery into the body of the flashlight next in a way that it will be easy to get back out for charging
- Put the LED ring and reflector into the mouth of the flashlight and cover them with the lens and ring
- Hold the lens down firmly against the LED ring and turn the flashlight ring gently. By pushing down on the lens you prevent the LED ring from spinning and tangling the wires inside the flashlight. The flashlight ring will not screw completely closed because of the space the LED ring takes up between the reflector and the lens. Tighten gently.
This tutorial has shown you how to construct a high tech flashlight that runs on a lithium ion battery and can shine in every color of the rainbow. On top of that kids can't keep their hands off of it, well at least mine can't.
More importantly you've expanded your skills in working with Arduino based electronics. You learned how to use an Atmega Tiny85 based Trinket for low cost Arduino compatible projects. You worked with addressable RGB LEDs and read values from potentiometer dials and toggle switches to control the colors, brightness, and blink rate of the lights.
You also gained a few tips and some experience building a project into a non standard case. The blink special effect is probably the simplest effect that can be done with the LED ring, so feel free to explore with the code for the Trinket and see what other special effects you can create.