Christmas time is always such a miserable, stressful lovely time at ch00ftech headquarters as I frantically scramble to finish all of the gifts that I’ve started for friends and family. Unfortunately, in my haste, I haven’t been able to write up everything I did in detail, but I hope this post serves as a good summary of some of the stuff I did and what I learned.
CNC4UANDME
A few weeks into October, I tried to sign up for a new iPhone through T-mobile. With credit card in-hand, I waited at the T-mobile store while my sales rep entered in my information, called over his manager, called T-mobile headquarters, entered in my information again, and generally struggled with the one task he was hired to do.
After 45 min, I was told that they would be unable to sell me that phone on that day. Unsure of why a phone store would still remain open when they are unable to complete the one transaction that they are in place to perform, I walked home. Unsatisfied by the lack of gross irresponsible financial release that I had so thoroughly prepared for, I ordered a CNC machine.
It wasn’t really an impulse buy though considering I have been thinking about buying a CNC mill for the past year or so. I used to have access to one back in undergrad which is how I made the metal stamp that I used to make the burned wood image you sometimes see in this page’s header:
I really missed having the ability to make extremely precise objects out of a whole range of materials. I know a lot of folks are going the 3D printer route, but I wanted to have the ability to make very precise and durable parts and enclosure for my projects out of a plethora of materials from wood and plastic to various metal alloys.
The mill I settled on is a Taig from Soigeneris. A lot of folks have told me that I should have built my own CNC, but I wanted this machine to be a tool that I use to complete projects, not a project in itself. I ordered the mill with the optional rotary axis table, though I haven’t found much of a use for it yet. I just thought that I might as well go all out.
It comes with the software CamBam and Mach3. CamBam is a CAM or Computer Aided Manufacturing package. Unlike CAD, CAM work involves actually deciding what kind of motions the tool has to take to carve out the shape you’re trying to make. CamBam can do some 3D work, but it’s sometimes referred to as a “2.5D” solution as it mostly focuses on cutting and carving shapes out of flat stock. Mach3 is meant to control the CNC mill directly. This software can read in GCode to control the CNC, or it can control it using direct input from the keyboard. It apparently also has a number of built-in wizards for performing common tasks, but I haven’t had a chance to play with them yet.
The mill, the software, and a few collets and bits ran me almost exactly 13.
My second attempt with the 60 degree bit worked much better:
And though it took a long time to finish, it was still faster and less messy than etching the PCB, and the end results were beautiful!
I really was impressed with the level of precision I was able to achieve. Just look at this close up (human hair for scale):
And compare it to a similar trace etched with acid:
It looks a heck of a lot cleaner. Now, the 10 mil traces definitely weren’t 10 mil across, but more trial and error with the isolation and epsilon settings will improve that.
Case
I’ll get back to how the PCB worked out, but now for the case.
For the new version of the earrings, I wanted a very simple case (box and lid) made out of some kind of durable semi-translucent material that cuts well on a CNC machine. I settled for a simple design that I whipped up in the cheap CAD suite ViaCAD and cut out of some “delrin” or acetal resin that I purchased from Online metals as 1″ round stock.
I might cover the CAD process in a later post, but the hardest part of making such a ridiculously tiny box (only 14mm across) was holding the damn thing in place.
For the super thin lid, I was able to use the same method that I used for the pinewood car:
But I had no such luck on the much taller box. Once it got down far enough into the form, the bit would always catch the box, rip it from its adhesive, and throw it across the room. This happened five or six times before I decided that it just wasn’t going to work out. I heard some suggestions to move to a stronger adhesive, but I decided to just clamp it down and do it right.
The adhesive method would have let me mill the entire object at once, but when clamping the raw material down, I needed to stop once the bit neared the vice grip so I could flip the piece around and start milling out the other side. This was trivial from a mechanical perspective, but programming the mill to know the coordinates of the newly re-seated part was a lot more challenging.
Some parallels let me set the Z dimension, and an edge-finder up against the inner lip of the vice got me the Y dimension easily, but X presented a problem. Because the part itself was overshadowed by the mass of stock I was cutting it from, there was no way to get an edge finder up against the part’s outer wall. This image might make it a little clearer:
As far as the CNC was concerned, that part could be anywhere along the length of that vice. I ended up sort of fudging it using a parallel and an edge finder like this:
This worked okay, but the tolerance stackup presented by the lack of rigidity of my hand and the softness of the delrin amplified by the length of the parallel caused one of my earrings to have a characteristic lip where the second half was milled just a few micrometers off center from the first. This didn’t have any mechanical impact, but considering that these are more form over function, I was mildly upset. In the future, I might try making a custom tool for this.
All setbacks aside, the final product turned out pretty well. Here’s the case with the original circuit in it:
The thinner walls of the delrin cases made the earrings much brighter and smaller than their Sculpey counterparts:
And lighter too:
I made some other (poorly documented) improvements over the original design. The hole in the bottom was precision drilled with the mill which means that the little screw heads fit very snugly and almost snapped in place (a dab of super glue kept them there). I attached the ear hooks to a steel loop of wire instead of the copper wire I used before. The wire was the same stuff I used to make this:
Steel is more durable than copper, and it leaves me the option to use magnets to attach the positive charging terminals instead of alligator clips. I was also happy to find that it can be easily soldered to copper to make the necessary electrical connections.
I’m a dope
You might be wondering why the above pictures show my original circuit board getting loaded into the new delrin case after I spent all of this time re-making said board with the CNC machine.
Well, there were some poor assumptions I made in the design of my original earring’s suicide circuit. I ended up using this:
Despite the fact that R13 should actually be tying the gate and source of Q1 together (oops), this circuit worked exactly as advertised. When the trace leading off the left side of the schematic is disconnected, R12 shuts off Q2 which in turn stops providing charge to the gate of Q1 which then turns off and disconnects circuit ground from the battery.
When I first designed this circuit, I had something like this:
Where I was driving the gate of the NFET directly from the output of the micro controller. This failed because as the NFET shut off, the ground pin of the microcontroller rose, so it became impossible to exert a low enough voltage on the NFET to shut it off further. The part would sometimes shut off, but most of the time, it settled into a half-on state where it continued to drain the battery. Through some trial and error, I ended up adding both a PFET and some pull up/down resistors.
Though it was too late to undo what I had done, I was convinced by the end of that post that I didn’t need the PFET and could get away with a single NFET as long as I made sure to include the pull-down resistor on the gate. After all, the issue I originally dealt with was exerting a low voltage from the micro controller. If the pull-down pulled from an outside source, it should be trivial to turn the part off simply by switching the micro controller pin to a high-impedance input.
As it turns out, I was wrong.
Upon re-building this circuit with the CNC, I discovered that my new circuit exhibited the same half-on battery-killing problems that I experienced with my very first draft of the board despite the addition of the pull-down resistor and change to the firmware. I wasn’t taking into account protection diodes.
Page 54 of the ATTiny24’s datasheet includes this figure:
Pxn is the pin presented to the outside world. You’ll note two diodes connected to this pin. These two diodes are in place to protect the main portion of the IC from over or undervoltage conditions. In other words, if the voltage on the pin drops below chip ground, the lower diode will source current from ground to this lower outside voltage to prevent the rest of the part from experiencing a negative voltage event.
In reality, these diodes are only really used for extremely quick pulses of high or low voltage (things like static shocks). Because of their important role in protecting the chip, these diodes cannot be switched on and off and are always present even if the pin in question is configured as a high impedance input.
So while I was assuming that no current was flowing from the pin that was configured as a high impedance input, this diode was actually sourcing a fair amount of current into the gate of the NFET (and associated pull-down resistor) as the chip’s ground rail rose above the negative terminal of the battery.
Perhaps this image drawn with a laptop trackpad on an airplane will make it more clear:
(I can’t wait to get home)
In this doodle, the “load” resistor represent the current draw of the processor itself from its VCC to its GND pins.
I poured over this for a while, and I really don’t think anything can be done besides falling back to the NFET/PFET combo that I originally had. That’s the only configuration where current leaking through these diodes does not prevent the FETs from turning off.
So the issue for me leading up to Christmas was that I needed to get these earrings done in a weekend, and I had just discovered the need for a PFET which I did not currently have in stock. This meant that I had to make everything happen with the original PCBs which were some of the ugliest circuits I’ve had to deal with…
Power management
Despite the ugliness of the circuit boards, I still pressed on to see if I could employ any of the power management techniques I discovered after I wrote my last post.
Watchdog
Because my earrings have no power switches, they are designed to go to sleep when in the presence of a magnetic field and then periodically check for that field to see if they should wake up. They also need to monitor their batteries to detect when they drop below the maximum discharge voltage. This was previously done by putting the chip into idle mode and waking it up every 8 seconds to read the hall-effect sensor and battery voltage.
Readers suggested using the watchdog timer to wake the device up from power-down mode. Power-down mode draws much less current than the idle mode, but the part cannot be woken up from this mode using a typical software timer. Fortunately, the ATTiny24 includes a “watchdog” timer.
A watchdog timer is meant to serve as a fail safe to protect a system controlled by a micro controller that’s caught a bug. Code programmed to utilize a watch dog will need to ping the watch dog at regular intervals to tell it that everything is okay. If the code catches a bug and gets stuck somewhere, the watchdog can be programmed to run a special line of code to attempt recovery or restart the part entirely.
All the watchdog needs is a time out delay setting and an instruction of what to do when it times out. Unlike the software timers left enabled in idle mode, the watch dog timer runs on its own special low-frequency oscillator that requires a very small amount of current and can continue to run in power-down mode. Figure 21-12 on the ATTiny24 datasheet specified a power-down current of around 3-5
A.
I modified my code to look for the presence of a magnetic field, and then turn on the watch dog timer and drop into power-down mode. I selected the watchdog prescaler options to have it trigger every 8 seconds, and I made no attempt to ping the watch dog to prevent it from timing out. Every 8 seconds, it threw an interrupt that woke the part up and let it iterate through checking the hall effect sensor.
This was surprisingly easy to setup. I did have one problem with it though. If the battery voltage dropped below the safe threshold while the part was asleep, I noticed that the suicide circuit wasn’t activating and the part was remaining alive.
It turns out that my code continued to iterate after activating the suicide circuit, so even though power was disconnected, it made it far enough to get back to the power-command which also disabled the suicide circuit.
I think the reason I didn’t see this before is that my new lower-power state allowed the part to generally survive longer off bypass capacitance. This doesn’t totally follow considering that the current draw before the power-down command hasn’t changed, but I’m guessing that I was skating some edge case.
The problem was easy to solve by adding a 100ms timeout after the suicide command.
ADC configuration
In order to read my battery voltage accurately, my previous attempt fed the battery voltage through a voltage divider and into an ADC pin. The divider was necessary because the ADC’s 1.1V reference voltage only allowed it to measure voltages under 1.1V.
The big problem with this solution is the need for the voltage divider. These two resistors will continuously drain current out of the battery even when they’re not in use. Larger resistors will improve that, but once they get too large, the measurement will become very susceptible to noise.
One reader had a brilliant suggestion to use the ATTiny’s ADC in a way I hadn’t considered before. Instead of using the 1.1V reference to measure the battery voltage, the battery voltage can be used to measure the 1.1V reference!
A reference voltage is simply what the ADC considers to be the highest possible input voltage. An 8-bit ADC will split this voltage up into 256 possible input measurements, so if my battery voltage is used as reference and drops to 3.0V, I know that a reading of 256 from the ADC will correspond to 3.0V.
While the battery voltage is at 3.0V, a reading of the 1.1V reference will come out to:
Perhaps a little counter-intuitively, if the battery voltage is higher, my reading will actually be lower using this method.
This probably isn’t a great method if high precision is required as it allows for small variations in the 1.1V reference to have a greater impact on the measurement, but for my purposes, it’s fine. This allowed me to completely remove the voltage divider which not only saved power, but it also saved space on my tiny board.
Pull up/down resistors
On the topic of removing resistors, I was surprised by my choice of pull up/down resistors in the previous design. I was using a 10k
pull up/down on the two FETs and the open-collector output of my hall-effect sensor. This accounts for nearly 1mA of current draw even when the battery is all the way down to 3.0V.
I think I chose these small values while I was still paranoid about the board not shutting off, and I figured that smaller resistors would pull harder to shut the circuit down.
I quickly swapped these three components out for 470k
resistors, and they worked fine.
Power results
With these changes in place, I noticed a substantial decrease in power draw from my circuit. While my previous sleep current was over 450
A, my new current draw hovered around 25
A.
This went up slightly when a battery activated the hall-effect sensor, but still not much:
This is the state that the earrings will stay in when left in their box, and with such a small current draw, they will drain just 4% of the tiny 19mAh battery a day. I’m curious to see if I can get it even smaller though as I may not really need the pull-up resistors at all since I’m using the two-FET solution. I’ll have to play around with it later.
A moderately ruined Christmas
The circuits for these earrings have seen some serious shit. By the time I was buttoning up the circuits for final delivery, I had wires breaking after being soldered and desoldered a dozen times. Traces were starting to lift up off the PCB. Solder-bridges everywhere. Hell, one of my batteries even exploded in the process…
The short story is that they were ugly as sin, and I didn’t have enough time before the big holiday to fix them.
I explained to my sister that her gift may only be partially functional on Christmas day, and sure enough, the negative charging terminal on one of the earrings broke, so I was unable to charge it before giving it to her.
I took the earrings back and spent the last few weeks completely remaking their circuit boards until they looked like this:
I feel much better about giving them away in this state, and I’ll be mailing them out soon.
Conclusion
This turned into an insanely wordy post, and I really blazed through a lot of content here without much explanation. I plan for the CNC machine to be a regular guest on the ch00ftech stage though, so expect a bunch of future posts with much more detail on how it works and what it can do.
In the mean time, I’m going to take a much-needed nap.
Project files can be downloaded here.
I don’t recall how I found your blog, but I very much enjoy reading your posts.
You remind me of http://imajeenyus.com/ – another engineer/hacker you should definitely check out.
Love the metal QR code.
How many RPM is the spindle motor of the CNC machine?
Pingback: Finally, party lights » Geko Geek