In this project, we'll build a USB volume control using an Arduino compatible Trinket from Adafruit, and a rotary encoder. Finally, we'll 3D print a housing, fill the base with lead shot to add weight and stability, and laser cut an acrylic bottom cover.
The Arduino code and underlying design were originally found on the Adafruit website. More information can be found Here. The Arduino code will require the Adafruit Trinket libraries, which can be downloaded Here (You'll specifically want the "TrinketHidCombo" library). The actual Arduino code can be downloaded Here.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Gather Your Parts
(Hover over the items in the photo for details and links to order):
- Adafruit Trinket, 5V, 16MHz (be sure to get the 5V version, NOT the 3.3V one).
- Rotary Encoder (the one shown here is a D-shaft, but there are also encoders with splined shafts, depending on the knob you use)
- About 2.5" of 5 conductor ribbon cable. Strip and tin the ends as shown in the photo.
- Five 1/2" long pieces of 1/8" heat shrink tubing.
Step 2: Wiring the Trinket to the Rotary Encoder
The rotary encoder has five pins - three on one side, and two on the other. The two pins on the one side are for the button. They make contact when the encoder's shaft is pressed. This will become the mute function. These pins are not polarity sensitive, and there is no difference in how the button wires connect to them. The three pins on the other side are the signal pins. If you hold the encoder facing the side with the three pins, with the shaft pointing up, the left pin is Signal "A", the center is Common and the right pin is Signal "B". This is also shown on the photo of the data sheet.
Wire the Trinket to the encoder as follows:
- Trinket Pin #0 to encoder's "A" signal pin.
- Trinket Pin #1 to one of the encoder's button pins.
- Trinket pin #2 to encoder's "B" signal pin.
- Trinket pin 5V to the other encoder button pin.
- Trinket pin GND to encoder's common pin.
Be sure to slip a piece of heat shrink tubing over each wire before soldering, and slide it as far over the pin as possible before shrinking it. The base will later be filled with lead shot, and these pins need to be as insulated as possible because they will be in contact with the shot when finished. A good tip is to further insulate the encoder pins between the shrink tubing and the encoder with a dab of hot glue.
Step 3: Prepare for Programming the Trinket
Open the Arduino IDE. Download and install the Adafruit Trinket libraries, and the volume control code (links in the beginning of the project). Set the Board Type as "Adafruit Trinket 16MHz", and the Programmer as "USBtinyISP".
The Trinket must be in Bootloader mode in order to upload code to it. When first plugged into the computer's USB port, the green LED will come on steady and the red LED will blink for 10 seconds and then go out. During this 10 second window, the Trinket is in Bootloader mode. You can enter the Bootloader mode at any time by pressing the button at the end of the Trinket opposite the USB port.
I've found that the Arduino IDE takes a bit longer than 10 seconds to compile and verify the code before being sent to the board, so when you send the code to the board, watch the green progress bar in the lower right corner of the IDE window. When it's at the halfway point, press the reset button on the Trinket. The video above shows the green progress bar. When it's about half way, I press the reset button on the Trinket. As long as the green progress bar gets all the way to the right before the 10 second window closes, the Trinket will accept the code. You can see the code transferring by the red LED going solid before turning off. If the transfer doesn't start before the Bootloader time ends, you will see an orange error message on the Arduino IDE. If this happens, just repeat the sequence and try again.
Step 4: Testing the Encoder
Once the programming completes successfully, and the Trinket reboots, you will be able to control your volume with the rotary encoder. Rotating the encoder clockwise should increase your computer's volume, and counter-clockwise should decrease it. Pressing the shaft should mute your computer. If you find that the direction of rotation has the opposite effect (volume increases when it should be decreasing), then you have the encoder "A" and "B" leads reversed. You can either swap the wires at the encoder, or just swap the pin definitions (0 and 2) on lines 3 & 4 of the Arduino code, and then send it to the Trinket again. In the video clip above, you'll see the rotary encoder controlling the volume and mute on the PC.
Step 5: 3D Printing the Base
The 3D Printing file can be downloaded from Thingiverse by clicking Here. I printed it using PLA, 0.15mm layer height and an 0.4mm nozzle. The rectangular block just outside the base in the print file is only used to minimize support material on the outside, since that support would be too thin and tall to remain stable while printing. Use concentric supports everywhere. The only difficult to remove support material is under the bridge that supports the Trinket. I use a combination of small screwdriver, angled tweezers and needle nosed pliers to remove it. It's important to get it out (or at least as much as you can), because that space will later be filled with lead shot.
Step 6: Add the Electronics
Install the Trinket in the base. All of the mounting holes in the 3D printed base are sized for 2-56 screws to self tap. Use two 2-56 x 1/4" screws to fasten the back end of the board. If you want a pack of 100 screws, they can be purchased from McMaster Carr by clicking Here.
Alternatively, if you'd like to buy just what's needed to build your project, a set of screws (for both the Trinket and the bottom cover), as well as the laser cut acrylic cover, rubber feet and optionally the lead shot can be purchased together from my eBay page -- Edit: I no longer have the eBay listings up, because eBay made me remove any of my listings containing lead shot as they consider it ammunition (even if used as weight or ballast). If you're interested in purchasing any of the hardware parts (i.e. screws, Acrylic bottom, rubber feet, lead shot, etc. -- anything but the electronics and knob), message me here (Click my image next to my name at the top of the project, then click the Message button).
Insert the rotary encoder through the hole in the top of the base, add the flat washer and nut and tighten securely.
Step 7: Add Weight to the Base
The base is filled with #7.5 (0.095") lead shot to add weight and stability (about 6 ounces, or 175 gm). This prevents it from slipping around on your desk when you rotate the knob.
Be sure to avoid getting any shot in the cavity where the Trinket is housed. Use angled tweezers to "push" the shot under the bridge, and fill it up to the tops of the bottom plate mounting posts and the walls surrounding the Trinket cavity. Level it out. You want the base to be full enough that it doesn't sound like a maraca when you shake it, but not so much that the bottom cover won't sit flush when installed.
Step 8: Make and Install the Bottom Cover
The DXF file for the bottom cover is included in the Thingiverse page for the base, or click Here for a direct link to the file for the bottom cover. I laser cut it out of 3mm (1/8") acrylic. You can use the same 2-56 x 1/4" screws as you used for mounting the Trinket to attach the bottom cover. Optionally, you can countersink the holes and use flat head screws so the bottom is flush. If you choose to use flat head screws, you can also order a 100 pack from McMaster Carr by clicking Here.
Finish off the bottom by adding four clear rubber feet to prevent slipping.
Step 9: Finish Up
Add a 38mm diameter knob of your choice. The knob I used can be purchased Here. Note that this knob has a set screw, so it is intended to be used with a D-shaft encoder. If you chose to use an encoder with a splined shaft, be sure to select a knob that is for a splined shaft. The knob's shaft hole will have matching splines, and not be smooth. You can choose any knob you like, as long as the outer diameter is 38mm, and it's compatible with the 6mm shaft of your encoder.
Finally, plug your USB cable in, give the computer about 15 seconds to detect the device (the Trinket has to get through its 10 second Bootloader sequence before the PC will detect it), and you're all set.