Electrical Design Rev 1
Both the camera and the printer operate over TTL serial UART which was great considering that the STM32F105 has multiple UART ports available. All my circuit really has to do is connect both of these components to the micro-controller, power them, monitor the battery, and offer a few bits of user interface like a button and LED.
My original schematic looked like this:
I was a little nervous about managing power for a 7.4V battery. Getting a micro controller to go to low power sleep mode is easy, but when the micro controller is powered through some kind of voltage step-down, managing quiescents from the power stages becomes a little problematic. I instead opted to give the device a big honkin' toggle power switch which connects to J2.
When in the "on" position, this switch connects the battery to the micro controller and other components. When "off" the battery is connected to the charge circuitry. When the charger isn't powered, it will draw a small amount of current backwards from the battery according to the data sheet, but it's such a small amount that it probably won't have real affect on a 3000mAh cell. If the device is stored for a long period of time and the battery does discharge substantially, the integrated protection circuitry should prevent any permanent harm.
I originally wanted this guy to be USB powered, but the complication of boosting 5V up to 9V minimum to then feed into the charger made a 12V DC wall-wart much more attractive. When connected, this 12V input powers the charger as well as the main circuit through a diode power OR. This allows the micro controller to monitor charge status and provide feedback to the user. I also included a pair of voltage dividers on the battery rail and 12V rail that allow the processor to safely detect which are connected.
My first pass at this design used a linear charge management circuit, but after realizing that I'd probably be using a wood enclosure, I started getting visions of smoke forming as the case struggles to dissipate the wost-case 6 watts of heat produced. Instead, I used the LT3650 which is a much more efficient switchmode device. A later thermal test indicated that the temperature inside the case rarely got above a balmy 101F while charging at 340mA.
That being said...I probably should have utilized the thermocouple hookup for the LTC3650, but...oh well.
Unsure of how big the camera and printer standby currents would be, I included PFETs that let me shut them off when they're not needed. It's good I did that too because the camera draws a whopping 50mA even when it isn't taking a picture. I also included some gigantic bypass caps for the printer which is sure to have some wildly fluctuating current draws. To power these, I used a simple inrush current limiter similar to what I built for the Gutenberg Clock demo to prevent the large capacitors from drawing too much from my power rail on startup.
Finally, I needed a way to measure the battery voltage. This is a little tricky with an 7.4V battery. Normally, I would use a voltage divider to drop the battery voltage down to something safe, but this would present a constant load which would reduce the battery's lifespan.
The second option is to connect an NFET to the low side of this divider to disconnect the load when not in use. This brings up another issue because with no current flowing through the divider, the output will rise up above the tolerance of the micro controller pin and damage it:
One solution that solves both of these problems is to supply power to the voltage divider from the high side using a PFET. In order to turn the PFET off though, you need to pull its gate up to match its source which in this case is 7.4V. To do that, you need a pull up resistor. You can't connect this pull up resistor to your processor line though, so an NFET is required on the low side to protect the processor:
While this solution is perfectly fine, it's actually more complicated than it needs to be. NFETs typically have their sources connected to ground. This is to ensure that whatever is driving their gates can raise the gate voltage up high enough above the source to turn the NFET on.
If you're clever though, you can put a small resistor between the NFET and ground and use that resistor to measure current passing through the FET. As long as the voltage drop across the resistor plus the V of the NFET is less than what you can supply to the gate, it should work just like the second example above:
When the NFET is off, the 1k resistor pulls the output to ground, and the 15k resistor pulls the drain up to 7.4V. No current flows from the battery. When the gate voltage is raised up to 3.3V (GPIO level of the processor), the NFET turns on and the Drain-Source voltage drops close to zero. About 460A will flow through the resistor divider which will raise the drop across the 1k resistor to 0.46V. This voltage will be proportional to the battery voltage which will allow the processor to safely measure the battery's state of charge.
Issues with Rev 1
Given the tricky 64 pin package of my STM32, I opted to get these boards made at SeeedStudio. As you can see from the image below, there were some...issues:
The first and most important issue is that the date on the silkscreen reads 9-17-14. I temporarily forgot that October, my birth month, is in fact the 10th month of the year, and so it looks like I've had these boards for a month longer than I really have.
Seriously though, there were a bunch of really annoying errors starting with the connectors. The thermal printer I chose came with a set of cables that used a particular type of connector. After hunting around and looking at images on Digikey, I finally identified them as JST type connectors. I later discovered that JST connectors are something of an industry standard. They're incredibly cheap, and they come in a large variety of pin numbers. They sure are a lot nicer than the crap I used in the Gutenberg Clock:
I opted to use them for the rest of my design connecting all of the other components like the button and switch to the PCB.
The data sheet for these connectors has no recommended footprint, so I had to make my own using the 0.64mm pin width as a reference.
When I designed the library part for these connectors, I made the holes 0.7mm in diameter figuring that would give me enough space. I failed to account for the fact that these pins are square, so while the sides are all 0.64mm, the hypotenuse is about 0.9mm. There's a great "a mathematician, a physicist, and an engineer" joke to be made here. This problem was immediately obvious and incredibly infuriating. Although I could sort of surface mount the tips of the pins into their holes, it was extremely easy to tear them out by accident especially considering the high retention force of JST connectors. For some components I just gave up and soldered directly to the PCB.
Given that I was diving into unfamiliar territory with designing a PCB for a processor I had never used, I was understandably concerned when it didn't immediately start up.
The first problem was that the pin 1 designation on the package isn't very obvious. The mark is comprised of a small circular divot in the corner near pin 1, but in the opposite corner is another divot that is likely a result of where the plastic was injected into the mold during manufacturing. These circles look very similar.
The datasheet didn't offer much help here. I finally figured it out after probing the pins of the part to verify that what I thought were ground pins all had internal connectivity. I got it wrong the first time, so I had to desolder and rotate the part around.
That wasn't the only problem though. Even after replacing the part with a fresh new one in the correct orientation, I didn't get any communication through the debugger. I started working my way around the part to see if I had anything connected wrong, but everything looked right. I even went as far as to replace my Discovery board debugger with a proper dedicated USB dongle.
Finally, I realized that I had VDDA configured incorrectly. As you might expect, VDDA is an analog voltage source. When I designed my schematic, I was thinking of AREF from the AVR. AREF is an external connection to the analog voltage reference. Through software commands, this can connect to a number of things including VCC and the 1.1V internal reference. It only has external access to allow you to decouple it with an external capacitor.
In a recent project, I had connected this pin to VCC and ran into problems when I tried to set the pin to the 1.1V reference. When I was laying out this camera schematic, I decided to keep VDDA disconnected from the power rail and just decouple it with a cap.
Well as it turns out, VDDA on the STM32F105 is not only used to power the ADC. It also powers the PLL and the reset circuit:
Perhaps I should have paid attention to this note:
With that reworked, my debugger instantly started working. I still had trouble for another day or so getting my debug LED to do anything intelligent, but after closer investigation, I found that that processor pin connected to the LED had been damaged at some point and wasn't able to source or sink any current while other pins had no such trouble. In fact, I actually never got it to work even on the second revision. Not sure what the deal is with PA15. The data sheet didn't offer much.
I definitely did find it weird that my debugger was indicating that code was running while nothing outside the debugger was doing anything intelligent, and I couldn't shake the feeling that gremlins were messing with my design and causing strange and unexplainable behavior. Here I was, drifting further and further away from assembly code and things were making less and less sense. This feeling followed me to another problem.
The Discovery board didn't have any hardware to control power going to the camera module, so I had to add code to accommodate my new board. What I found is that the debugger glitched out every time I switched on the camera. Things would run fine until it just suddenly lost the connection to the device. I didn't trust my debugger set up very much at this point, so it took me longer than usual to point the probe at my own circuit.
The power rail looked like this:
Tantalum capacitors can be made much smaller than electrolytic or ceramic capacitors, so I didn't even think to check what kind of bypass capacitance was built into the camera module.
That little yellow guy is 100F of capacitance! When switching on camera power, I was connecting a huge totally dead capacitor to my 3.3V power rail which drew an incredible amount of current and caused my rail to sag. I estimate somewhere around 3.5A based on the waveform. The bottom tip of that sag is very close to 1.8V which is right where the processor will cease to function. This makes it pretty clear that the processor was browning out just enough to mess up the debugger connection.
Adding an inrush current limiter fixed this problem:
Though the rail still sags, it's much better than it was, and the part started behaving normally.
I think this is the first time bypass caps have actually been the problem, and not the solution.
The last major problem I had was definitely a new one. The 8.4V battery charger I used comes in a variety of packages and voltages. I originally opted to use the MSOP package as it was easier to work with than the no-lead options, and I had plenty of board space anyway.
After I assembled my board, I noticed that the battery charger wasn't pumping any current into the battery. When I disconnected the battery, the charger provided an open-circuit voltage of exactly 4.2V. I had accidentally ordered the 4.2V version!
No problem, just a quick Digikey order, and...
Uh oh... No matter where I looked, nobody had this part in stock. It's a real bummer when you know something will work and you just can't get your hands on it. It's a problem that I certainly haven't come across before in my projects.
In writing this article, I noticed that Digikey does now have the part in stock under a slightly different part number. I'm going to assume this is a recent development because I couldn't find a source for the life of me when I was assembling the board, and I ended up moving to the smaller DFN package for my second build.
Besides that, there were just a few alterations to part footprints to make them fit better, and I was off to PCB V1.1!
Electrical design Rev 2
By the time I made this second revision, I had already been writing software on the first revision for a number of weeks. I had a chance to test out all of the systems, so there was almost nothing wrong with this board.
The one mistake I did make was with my capacitor selection on the 12V rail.
Many of the capacitors in my parts library were made a long time ago when I was working on very simple circuits, so I never went through the effort of specifying the voltage rating of the components. I just picked 10V caps that were "good enough" for everything I was doing.
As a result, many of the caps in Rev 2 are not rated for 12V. I'll have to go back through my library and make a different part for higher voltage rails. For the time being, I just stuffed different caps that I had on hand.