Ask HN: What's the hardest problem you've worked on recently?
The hardest problem I worked on was only hard relative to my own ignorance level - it wasn&#x27;t necessarily &quot;hard&quot; in any objective sense. And frankly, to an experienced EE it would have probably been trivial. But I&#x27;m mostly a self-taught hobbyist when it comes to electronics and there are lots of gaps in my knowledge, so...<p>Anyway. The problem was this: I want to power a Raspberry Pi from a battery. OK, that&#x27;s easy enough in and of itself. Use a USB &quot;Power Bank&quot; device, and bob&#x27;s yer uncle. But... I need this thing to be up and running close to 24x7 and it also needs to be portable. So AC for charging the battery will be coming and going on an unpredictable basis. Superficially this looks easy: just package a USB mains powered charger in the box, plug it into the charging port of the Power Bank and bob&#x27;s yer uncle. But... it turns out that RPi&#x27;s are fairly power hungry and (at least some) USB Power Bank devices limit their charging rate to less than the rate at which an RPi consumes current, so they battery can drain down even when the charger is plugged in. This is sub-optimal.<p>So I decided I wanted a circuit of some sort to let me connect both the mains adapter and the Power Bank to the Pi more or less simultaneously, and have it automatically switch between then using the rule &quot;use power from the mains adapter when available, otherwise use the battery&quot;. Some constraints I set were:<p>1. The Pi can&#x27;t reboot or glitch when the switch happens.<p>2. No &quot;back current&quot; can flow from either power source through the other, ever.<p>3. The battery cannot be discharging into the load at all when the mains power is available.<p>So I whipped up a few circuits to try to do this with a pair of MOSFETs, one for each power rail. But here&#x27;s the big rub, that it took me embarrassingly long to understand: the power rails are ultimately bonded together at the output terminal, and electrical fields will spread wherever there is a conductor. And semiconductor devices that block current flow don&#x27;t necessarily block electrical fields.  The net-net being, the AC powered rail always has an apparent voltage on it, even when the mains adapter is unplugged, because it&#x27;s coming from the battery. And that makes it very hard to implement any logic based around &quot;turn this transistor on, and this one off, when power is present on this rail&quot; and vice versa. But the rails are effectively the same in terms of always having an apparent 5V potential.  And MOSFETs are, well, &quot;field&quot; effect transistors, so any apparent voltage affects the state of the FET even if a transistor or diode &quot;upstream&quot; is blocking current flow.<p>In the end, I &quot;solved&quot; this by ditching the semiconductor approach temporarily and using an old-fashioned electro-mechanical SPDT relay. But this has all the disadvantages of relays: they draw more power, generate more heat, take up more space, and have a limited lifespan.<p>So I still have some ideas about how to make it work in a semiconductor based approach, that I need to experiment with. One is to use BJT&#x27;s for the power handling instead of FETs. I <i>think</i> this might &quot;just work&quot; since BJT&#x27;s require current through the base-&gt;emitter path to &quot;turn on&quot;. Another possibility is to add a current-sensing shunt resistor on that AC powered rail, and use the presence or absence of any current flow to make the logic decision about the system state. The reasoning being, when the power is coming from the mains adapter, there will be current flow through the shunt resistor. But if it&#x27;s coming the &quot;other way&quot; so to speak, current flow can be blocked by a transistor or diode. So I <i>think</i> I can make something work based off of this idea.<p>Anyway... still some experimenting to do, but for now I do have something in place that works. But yeah, I pulled some hair out in frustration (and wasted a lot of components) while trying and failing to get this working.