About a year ago, I bought a pair of bluetooth earbuds. I got them for a very cheap price (somewhere around $10-15), and used them successfully throughout the summer. Unfortunately, one day the left earpiece popped out broke, leaving two wires poking out of the black plastic case. They worked perfectly fine except for the one broken piece, and I was out of an otherwise perfectly fine pair of bluetooth earbuds.
After having these sit around in a drawer for a couple months, I decided to take them apart to see how they worked and if I could salvage anything.
Opening the case revealed that one of the earbuds had a charging port and battery, and the other had the control electronics.
My idea at that point was to scrap the earbuds, which are just tiny speakers, and solder on a 3.5mm audio jack so I could connect my speakers to them. This would turn my broken earbuds into a working Bluetooth receiver, something which I had wanted to purchase anyway.
After prying the plastic housing away, I cut the wires connecting the two halves together and examined the circuitry hidden within. Interestingly enough, the wires coming from the micro-USB charging port go all the way across the headphone wire to the control board, and then other wires go all the way back to the battery. This actually makes sense, because the control circuitry needs to monitor the lipo battery voltage; it is important to not over-charge a lithium battery, and the board controls flow of charge to ensure that this does not happen.
After taking the board apart I found an enclosure to put everything in; I didn’t want bare electronics sliding around on my desk. I settled on an empty headphone case, the ones Apple earbuds come in. I still had mine, untouched since I got my new phone and otherwise just taking up space in my drawer. I haven’t heard of anyone reusing this case; it’s just packaging that almost everyone throws away after getting their headphones. The final component was the 3.5mm headphone jack. I found a pair of earbuds in the trash at work and grabbed it knowing I could repurpose what would have otherwise been trash into something I could use.
I planned out where the micro USB charging port would go, and drilled out a hole for the headphone jack wire. I used a dremel to cut the proper shape for the USB port, which you can see cut out in later pictures.
Next, I stripped the wires from both the headphone cord and the Bluetooth earbud’s circuitry. If you’ve ever stripped wires from earbuds or headphones,you know how annoying this is. I find these wires really annoying because they’re so hard to work with. They are very thin and coated with a nonconductive enameling, so you need to be very careful; a simple pair is of wire strippers won’t get you anywhere with these wires.
After removing the outer plastic sheathing with wire strippers or careful cutting with flush cutters, you’ll find many thin wires and possibly some electrical shielding. (Braided copper wire or a thin foil). Sometimes there will be a wire wrapped in additional braiding. After separating out each wire, the insulation needs to be removed. I’ve been told that using sandpaper is a good method, but I didn’t have any sandpaper at the time. The enameling can also be burned off with a lighter and scraped away. This method leaves a charred looking piece of wire, but if done carefully can work well for the time and effort spent.
After taking the time to match up and solder the correct wires together, I placed it into the case. I had to refer to a chart for how to wire up the 3.5mm jack, and tested it with the speaker on my desk to make sure that I was wiring up the correct channels together. You don’t want to mix left and right, or make a worse mistake that would cause everything to work.
After putting everything in the case, there was plenty of room to spare which I was fine with; it’s still a low profile case that has the nice look of apple’s design once the lid is on.
I used hot glue to keep everything stuck, and lined up the micro usb port with its hole I cut and the microphone to a small hole I drilled in the case (I don’t plan on using it, but may as well put it in).
And finally, I was done!
I now have a bluetooth adapter made completely of repurposed headphone parts that would have been thrown out otherwise. This project only took me a single night’s work after getting all my materials together, and I’m proud that I was able to make something useful with my old stuff. Now I can stream my music over bluetooth and listen to it through my speakers.
I hope you enjoyed my project, and perhaps have inspired you to make something useful out of the things that you normally throw away in your life.
A few months ago, I purchased an old analog oscilloscope. Nothing fancy, just the basics I needed to get familiar with using a scope. I wanted to figure out how to use this piece of test equipment, and plus the glowing green screen and abundance of very clicky knobs looks (and feels) pretty cool.
In this post I will explain the basics of what an oscilloscope is, the difference between PWM and analog voltages, and how to create an digital to analog converter to create analog voltages with a microcontroller. With two analog signals, I will then draw vectors on an oscilloscope screen in order to display graphics and text.
What is an Oscilloscope?
An oscilloscope works by plotting a voltage over time. That way, you can very easily visualize signals. On older analog scopes, they actually deflect an beam of electrons to make a moving dot on a phosphorescent screen to create a line. But newer digital oscilloscopes still work in pretty much the same way. Oscilloscopes are a very important tool in electronics because they allow engineers and technicians to see the electrical pulses and signals that occur inside electric circuits, even when they occur very rapidly.
One cool example of an oscilloscope in use is visualizing an amplified audio signal. This makes a very sci-fi and robotic waveform, and lets you see how sound is made up of waves of varying frequency.
One of the problems I had after I got my oscilloscope was that I had no signals to measure! With an Arduino or other microcontroller platform, you can easily make a 0-5V square wave by pulling a pin high and low in a regular patter. This is somewhat interesting, but is not using the scope to nearly its full potential.
My Arduino Uno can also produce a PWM signal, (short for pulse-width-modulated signal). This means it can very quickly (in the MHz to high kHz range) go from 0-5V with varying duty cycles (amount of time 0V compared to amount on 5V). This shows up on my oscilloscope as a square wave that changes the width of time high.
This signal is produced by the following code:
for (int iii=0; iii<256; iii++)
Producing an Analog Voltage
A PWM signal can mimic an analog voltage by providing the same average power, but it isn’t truly an analog value. 50% 0V and 50% 5V doesn’t equal 2.5V, especially if your sample rate is high.
So, now we face a problem: how do we get an analog voltage from pins that can only produce 0 or 5V? A device that does this is called a DAC (Digital to Analog Converter). One way is to use capacitors to “smooth” a PWM signal. The goal is to choose a capacitor sufficiently large such that it takes more time than the width of the pulse to charge or discharge. This will produce a voltage that is between 0 and 5V depending on the duty cycle, but is also fairly slow, because it needs to allow the capacitor to charge and change its voltage.
Another option is to use a circuit composed of resistors connected in a particular way, called an R2R circuit. This is a very simple circuit that allows multiple binary signals to be converted into a single analog output.
It is called an R2R circuit because it only requires two arbitrary values of resistors; one simply needs to be twice the other. Thus, one is labeled R and the other two times R (2R).
One cool thing about this circuit is that you can make any voltage you want, and your precision goes up the more pins you go up. As you count up in binary (000, 001, 010, 011, 100, etc). The voltage produced at point A will correspondingly change. It worked really well for my project because:
It’s fast: very little delay
Easy to make: you only need two values of resistor (or one if you use two of the same in series where 2R goes)
Cheap (because it is only made of resistors, which cost fractions of cents)
Works well with Arduino: using the PORT functions you can quickly and easily write binary values to multiple pins at once, perfect for this application
Here’s a video that explains how this circuit works very well, using Thevenin’s superposition theorem to explain how to simplify the circuit:
After this circuit is put together, and the digital signals connected to an Arduino’s PORT pins, we can create analog signals. I started with a ramp, as it is a simple changing signal that can I can easily visually inspect with my oscilloscope.
Success! The circuit works, and now I can create quickly changing analog values with my Arduino Uno and a simple circuit of resistors.
Putting it all Together
The next step is to use the new ability to quickly create analog voltages to create the image we want on the oscilloscope screen. With two R2R circuits (one on the UNO’s PORTB and the other on PORTD), we can create two independent voltages simultaneously. With the oscilloscope on X-Y mode, the voltage on probe 1 controls the x-position, and the voltage on probe 2 controls the y-position. Therefore, it becomes possible to make the display “draw” lines by incrementing each value.
The first thing I displayed on my oscilloscope was a circle, because it is a simple combination of a sine and cosine wave. Here’s what each signal looks like. You can see the x and y component of the image, as two identical waves spaced 90 degrees apart.
Here’s another test image, this time one of a starburst. This is basically made up of ramps with periodic amplitude.
Now we want to be able to draw vectors on the display, so we can create an image out of points connected by lines. Basically, a connect-the-dots type of image.
Because the DAC we have contains 256 discrete steps, we can’t technically make any “smooth” line. A way to work within these constraints is common in computer graphics applications, as pixels often need to represent vector-based graphics. The code I’m using employs Bresenham’s line drawing algorithm to create a line between two given points with the function line() which has inputs of x1, y1, x2, and y2, as the start and end points of the vector.
And that’s the main idea of the code. With the coordinates of each vertex of an image, the code runs through each point and draws vectors between each point.
I started with the image of a logo I made a decade or so ago, this picture:
And then plotted a points to represent it with straight lines, within a 0 to 255 grid (because we have 256 steps of resolution).
And finally wrote down all these points and loaded them into an array that is programmed into the Arduino Uno.
Now if we want the logo to “spin”, the x-coordinates are changed in a periodic sinusoidal fashion to compress into a center line. If you want to see more information about how to do what I have described here, I recommend you watch the following videos:
Great Scott video:
In the first video, W2AEW goes over most if not all of everything that I have mentioned here. His code is at this link:
It was from his youtube video that I was inspired to mimic the idea of using an R2R circuit to create spinning graphics, and I am thankful for all of the interesting things I have learned along the way.
This post details a project I recently finished, an electronic dice roller. My goal for this device was to display the result of a simulated “dice roll”, allowing for easy and quick generation of the sum of random numbers from 1 to n, where n is chosen by the user. This can be helpful in many role-playing games such as Dungeons and Dragons, where there are many times you need to roll something like two six sided dice (2d6), one twenty-sided dice (1d20), or three eight-sided dice for example (3d8).
Layout and Planning
After a few iterations of different screen designs and button layouts, I settled on a final design.
Preliminary design sketches/ideas:
(click to enlarge images)
My finalized design employed the use of a 128×64 pixel OLED display, mainly because of its flexibility: I could easily change my graphical interface as opposed to, say, seven-segment LED displays which severely limit the number of digits that can be displayed. They also are more complicated to use because you need many output pins and therefore would be forced to add extra components such as shift registers to make it work with a microcontroller of a limited number of pins.
The screen I got on Amazon also is dual color, in the sense that the first 16 rows are yellow, and the rest blue. This was perfect for my application because it fits my graphical layout (number of dice rolled at top in yellow, and blue for the larger text result). Plus, it just looks nicer than a single-color screen.
Apart from viewing the screen, the user interacts with the dice roller by pressing the “roll” button (the red upper button) which rolls the selected number of dice with the selected number of sides, or the “add” button (the yellow lower button) which adds a new roll to the number already displayed on the screen. The number of dice and modifier can be seen at the top yellow portion of the screen, and the roll result in a blue larger font at the bottom. If the result is greater than three digits, an error message is shown, as only three digits can fit on the screen.
The values for number of dice rolled, number of sides, and the modifier added or subtracted can be easily changed by using the knob: when pressed, it cycles which area is highlighted, and the value can be modified by turning the knob clockwise or counterclockwise. This makes changing settings quick and intuitive, and allows for very quick rolling of various amounts of dice.
Now I had to program my Arduino to do what I needed it to: interface with a screen, read the input from two buttons, and adjust values based on a rotary encoder.
Luckily, communicating with the screen was made quick and simple because of Adafruit’sLibraries. (SSD1306 for interfacing with the driver chip for the screen, and their GFX library for quickly and easily adding text or shapes to the display.)
The rotary encoder used external interrupts, which are a very useful feature that the ATMega328 has and the Arduino IDE has support for. It allows code to be ran only when a pin changes, and otherwise will not slow down the main loop() program by polling with the digitalRead() function. You can read more about this useful feature here: http://playground.arduino.cc/Code/Interrupts
After prototyping my design with my Arduino Uno and a breadboard, I arranged each part on a proto board. I also decided to use a bare ATMega328 chip because of its ease of use from prototyping on my Uno and integrating it into my final circuit. Because it is the chip that runs my Arduino, putting it in my final circuit is as easy as popping the chip out, soldering the connections in place, adding a 16Mhz crystal oscillator and 22pF capacitors, and adding a pull-up resistor to stop the reset from activating.
From the beginning, I wanted this project to run on an external battery, and a 9V battery seemed perfect for this task. I used a 5V linear voltage regulator to supply the microcontroller and display with the correct voltage. I added a 10uF capacitor on the output because it needs it for stability reasons, and a 0.1uF decoupling capacitor, though this is probably not necessary.
Finally I added the screen, which partially covers the voltage regulator and microchip to keep the device as compact as possible. After that was done (and after testing to make sure it all actually worked), I cut the extra protoboard to size, to make it fit better inside a case.
I also connected a power switch in series with the battery source to allow it to be turned on and off. And finally it works! A small, compact self-contained package that now does exactly what I want: to roll dice. All I need to do is build a case to house my newly-soldered electronics.
Instead of using a generic black box housing, I wanted to make my own unique case. I also liked the idea of making it out of metal and luckily I had some scrap laying around from taking apart an old fax machine.
To shape the metal to the case, I started by using calipers to measure my electronics, and made a 2d CAD image of the screen, knob, and button layout in SolidWorks to aid in cutting in the right spots. Next, I printed this on a piece of paper at 1:1 scale, cut the paper to shape, and taped my image onto the metal sheet. After using a marker to transfer the cutout shapes over, I had a template to cut holes for the case that were placed in the exact right spots.
I then used a dremel with a cutting wheel attachment to make the hole for the buttons and screen, and a large drill bit to make the circle for the knob. If I were to redo this step, I would have drilled out my button holes as well, as it is very difficult to make square holes of the size I needed, and I ended up using round LEDs to reach the buttons anyway. Oh well, now I know how to do it better next time.
After I cut the holes in the front plate of the case, I also cut a piece for the back and used my dremel with a different attachment to sand away the metal and change the finish from a matte look to a snazzier brushed metal look.
After I cut the metal to size for the front and back, I needed to find something to reach the buttons, and needed some sort of spacer to stop the whole thing from squishing together too much, at least enough so the buttons would not be pressed continuously.
As you already know, I used red and yellow LEDs not as a source of light, but to reach the momentary buttons soldered to the board. These actually worked astonishingly well, after some modifications. First I cut off their leads and added athin ring of hot glue around their base as a way to prevent the LEDs from sliding out of the case when it was fully assembled. One thing I actually could have done is make the buttons themselves light up when pressed, but at this point in the project I just wanted to have a finished project. Maybe I’ll make some cool light-up buttons on the next thing I make.
I made the spacers out of fairly small aluminum tubing, and hot glued them in place. This isn’t the most rugged case, but it should do for my purposes. I spaced the back far enough to easily fit a 9V battery, hot glued the switch to the front side of the case, and closed it all up with black gaffer’s tape.
And, now it’s done!
I’ll leave you with some more images of the device. Thanks for reading!
Making a Flash Trigger using Arduino code on an ATtiny85
In this post I’ll go over how I created a high-speed flash triggering device, as well as detailing how it works. Since creating this project I have learned a lot more about electronics and using the Arduino platform, and thus there parts of this device I would do differently now. But even with the mistakes I made then, this project was a great way to get me into prototyping my own devices that stand alone and perform a useful function.
What is it?
First, lets go over what it does: When a laser is crossed, an external camera flash is triggered.
The flash can also be triggered by a microphone, which will cause the flash to fire once a certain noise threshold has been reached.
So, why is this useful? A controlled flash makes it a lot easier to get consistent high speed photos. Think of this example: you want to capture a falling milk droplet right as it hits a surface; the idea is that the laser and sensor are set up so the droplet will cross the laser and cause the flash to fire, causing light to hit the camera’s sensor only at the instant that the laser is crossed.
In my device, I also added a display and adjustment knob so I can add a very specific delay, which is useful when you want the flash to fire later, so I can more easily capture specific instants after the droplet splashes up again. This allows for more flexibility in the laser placement.
Here’s a photo I took with the same scenario that I just described:
Note: The delay was actually one of my problems: due to the way my code ran, it caused too much delay. This is because the main loop of my program took too much time to run, as it would read the analog values of various pins on my microcontroller and output the delay to a screen. Were I to do this project again on the same platform, I would use external interrupts which allow a much faster execution time, as they only execute their relevant code the instant after the signal is received and don’t have to take time out of the main code loop to poll a sensor.
Now that you know the basics of what it does, lets go over how it works.
How it Works
I started by using my Arduino Uno to make an external camera flash fire. It’s a fairly simple procedure to make an external camera flash fire with an arduino. First, lets go over how the flash triggering works. Most external flashes have a port on the side, to plug in what is called the PC sync cable. You make the flash fire by completing the circuit between two terminals using this cable. This is also possible on most flashes through the hotshoe mount, which allows for proper flash firing when attached to a camera.
Because there’s a lot of current running through these wires for a short period of time, we need to isolate it from the arduino circuit. I used a photocoupler, which has a small light-emitting diode and a sensor in it. This means that when a voltage is applied through two of the pins, current can freely flow between two other pins, allowing us to control current without directly allowing it to flow through our arduino circuit (and thus isolating it). Interestingly enough, there is a small LED inside the DIP package, meaning a current-limiting resistor needs to be placed in series with the input pins. It is the lighting of this LED which allows the current to flow on the other two pins.
Here’s a demonstration of the photocoupler in action, allowing an LED to be lit up.
(Shot of LED lighting up when the two pins are applied current, or a graphic showing current flow with arrows)
So all we need to do to make a flash fire is use our arduino to create a +5V signal on one of its pins, place a resistor in series so it doesn’t burn out the photocoupler, and connect the two wires coming from the flash to the other side of the photocoupler.
Now that the arduino can cause our flash to fire, we need to find a way to trigger the arduino’s command. I chose to use a laser and a photoresistor to make the flash fire at the instant the light from the laser ceases to shine on the photoresistor.
An arduino can only directly measure voltage, not resistance; therefore we need to set up a voltage divider with another resistor connected in series to the photoresistor for the arduino to obtain useful data about light. This way, when the photoresistor changes its resistance, we can quantitatively measure it. And of course, we’ll need a laser. You can get cheap red lasers nearly anywhere these days. In fact, I ordered ten little 5V modules for less than five dollars. I soldered one of these onto some longer wires and a tripod mount by cutting ¼” threaded metal couplers in half and taping them on.
You could do this, or use any other bright light source, as long as it will constantly stay on the photoresistor until you want the flash to be triggered. Now that we can get data about light hitting the sensor, we’re ready to start with our program.
Programming and the ATtiny
I won’t go too far into the code I wrote, but I’ll try to touch on everything I needed to make the device work.
First, I prototyped my device using an Arduino Uno. When I was making this, (Winter of 2014), I had only been working with electronics and the Arduino platform for maybe a month or two, when I found time during school. It was during winter break that I was really able to dig into it more and make a larger project such as this. I also continue to be amazed by the support and communities around Arduino and how easy it is to set up a project and find answers to any questions that pop up while working on electronics.
One thing that I wanted to do with this project was have a nice, easy-to-read display. I had ordered a bunch of 7-segment displays online for pretty cheap and I like the look of them so I thought I’d go with that. Because you need to be able to control a lot of outputs to light the LEDs inside the display, I used shift registers (74HC595) to control the display.
I didn’t know about shift registers before making this, and I think they’re a really interesting way to expand the number of pins you can control by using only three outputs on a microcontroller. It works by having a communication protocol that uses three pins: one as a clock, to tell the register when to store each bit; one to actually send the data, as a digital 0 or 1 by sending a 0V or 5V signal; and the last one as a “latch”, to tell the shift register to output the data, causing it to create a digital signal on its eight output pins, corresponding to the data that was sequentially sent to its data pin.
With the help of four shift registers, I was able to control the values displayed on four seven-segment displays.
After figuring out how to use my displays, I just had to program and put it together. I used a 10kOhm potentiometer to select how many milliseconds of delay time I wanted. I am able to do this because the Arduino has an ADC (analog to digital converter), so it is easy to map its voltage to a 0-1000 millisecond delay.
My code allows for selecting a sound mode, or a laser mode. Each reads an analog value, but this time from a pin which is connected to either a microphone unit or the photoresistor mentioned earlier in its voltage divider setup. When one of these crosses a certain voltage threshold (which was fine-tuned by me earlier), it sends a signal to the photocoupler, allowing current to flow through the flash.
So generally, that’s how it works. Power, the sensor, and the flash unit are connected with screw terminals; there are four 7-segment displays which display the amount of delay; and the main input is a potentiometer that you turn to adjust values and enter the menu to change settings.
Next, we need to load the code onto an ATtiny. Luckily, there is a fairly easy way to do this with an Arduino Uno. With only a 10 micro-Farad capacitor and an Arduino, the serial data from your computer can be loaded onto the very small ATtiny chip. This website, http://highlowtech.org/?p=1695, has complete details on how to do it. With the new Arduino IDE, you can even download their libraries very easily, and can easily run the Uno as an ISP.
This is a key step that allowed me to move past the need to connect my Uno each time and let me make a stand-alone unit.
It was a lot of work soldering everything all together, especially with every connection I needed for the seven-segment displays. I added a 1/4 inch audio jack, which connects to the flash via the PC sync cable. Next, everything was wired up to an 8-pin IC socket adapter, where I could plug in the ATtiny85. Four shift registers were added, the potentiometer, and the screw terminals.
Here’s some images showing my planning process and the wires being soldered into place:
After all that work, I finally had a working product! I decided to also make a nice plexiglass housing, to make it a little more robust and create a more “finished” look.
4x 74HC595 Shift Registers
4x Common Cathode 7-segment displays with “.”
5x 330 ohm resistors (I used 1/6W)
10k Potentiometer and knob
female ¼” female threaded connector
ATtiny85 and 8-Pin IC holder
Various colors of ~22 gauge insulated wire
1-2x 10uF capacitors
3x 0.1uF capacitors
1x Photocoupler (I used a 4-pin)
Optical Resistor and 10k Ohm resistor
Double-sided Prototyping Board
Optional (for housing)
4x Machine screws
4x Lock washers
And finally, I have a finished product! As I mentioned earlier, there are a few things I would do differently if I were to make this again, but I am very happy with what I made. I went from very little experience with electronics to making a finished product with a flashy screen, working input, and ability to improve my photography. I hope you have enjoyed learning about what I have made, and I’ll leave you with some photos that I have taken with this device.