Raspberry Pi HATS compatible UPS – part IIb – status indication

The explanation of the trickle charger portion was a lot longer than I’d anticipated, and I hope it was clear. With that in mind, I’m going to dive straight into the development of the second main portion of the UPS board – the source and charge indication controller. As you can see in the schematic snapshot below, this consists of three main sections: power/reference (R1, R4, R14, IC1, C1, C2, C3), power source indicator (R10, R17, R11, R18, IC2A, R8, R5, LED2), and low battery indicator (R12, R19, R6, R20, R13, IC2B, R9, R7, LED3). An LM2903 dual comparator, IC2, forms the core of this part of the circuit.

indicator

Power supply and reference: if you’ve read about the trickle charger, this part should be fairly straightforward. This is again built around a TL431 shunt regulator, which is going to serve two purposes: to provide a stable and accurate voltage reference, and to provide a 3.3 V power supply which will allow interfacing with the Raspberry Pi. I chose a shunt supply so as to reuse the widely available and easily configured TL431 IC. It would have been possible to use a simple linear voltage regulator here but these often come in different packages and are usually more expensive. Additionally, a shunt regulator is inherently short circuit proof, which is important in testing as mistakes happen..! Without further ado, the values were calculated using the following procedure:

  1. Shunt regulators work by tapping off a portion of applied current to ground so as to keep the desired voltage, so we need a current source for the supply. In this instance, R1 is acting as a very simple (and very bad, but sufficient!) current source. To calculate its value we need to know the minimum input voltage, V_{in}, the output voltage V_{out}, and the shunt current I_{shunt}. Of these, the two voltages are known and the designer must chose the chose the shunt current. As can be seen from the schematic, the 3.3 V supply powers IC2, LED2 and LED3, and the outputs to the RPi (or whatever), BATT and LOW. As shown, the LEDs are powered at about 1.5 mA each (more on that below), and the comparator will take around 4 mA, and the voltage dividers and output will take some current too, so we need at least ~9 mA shunt current. However, for some tolerance, I’ve settled on I_{shunt} = 15 mA – this is a value that may be easily changed! Now, we are in a position to calculate R1. The voltage across R1 is V_{in} - V_{out}, with the minimum $V_{in}$ dependent on the battery chemistry; for our 6 V SLA battery that is around 5.9 V. So, R1 = \frac{V_{in}-V_{out}}{I_{shunt}} \rightarrow R1 = \frac{5.9 - 3.3}{0.015} = 173\ \Omega. Rounding this to a realistic resistor value gives R1 = 180\ \Omega.
  2. Now we need to program the TL431 (IC1) to give the desired 3.3 V output. This is set by resistor divider R4/R14. For the TL431, V_{out} = (\frac{R4}{R14} - 1)V_{ref}. From the datasheet, we know V_{ref} = 2.495 V, and as discussed in part IIa, R4 + R14 \approx 10\ k\Omega. We already used a 6.8 k\Omega resistor, so it makes sense to use that again for R14. Putting it all together after rearranging the output equation to solve for R4 give: R4 = (\frac{V_{out}}{V_{ref}} - 1)R14 \rightarrow R4 = (\frac{3.3}{2.495} - 1) \times 6800 = 2194\ \Omega, so rounding to a sensible value gives R4 = 2.2\ k\Omega.
  3. As a safety precaution, we should check the worst case power dissipation in R1 and IC1. The worst case for R1 is if the output is shorted to ground, in which case it will see the full V_{in} across it. Knowing the voltage and resistance, we can use P_\mathrm{R1} = \frac{V_{in}^2}{R1} \rightarrow P_\mathrm{R1} = \frac{6.8 \times 6.8}{180} = 257 mW, so we should use a 0.5 W part here. For IC1, the (unlikely – why?) worst case is all the current running through it, so we can calculate it as P_\mathrm{IC1} = I_{shunt}V_{out} \rightarrow P_\mathrm{IC1} = 0.015 \times 3.3 = 49.5 mW, which is within the power tolerance specified.

Supply source indication: the LM2903 is an open collector dual comparator. What this means is that it acts like a switch inside, which is closed when the -ve input is at a higher level than +ve input. When the open collector output is on (closed), it acts like a wire to ground, so that node is at 0 V when it’s on. This also turns on LED2, which now has a path to ground When it’s off (open), R8 pulls up the voltage to 3.3 V, and LED2 turns off as there’s no voltage difference across it. In this case, we just need to divide both the external supply voltage and the battery voltage by an equal amount as we just need to know which is bigger. The constraint is that the inputs to the comparator must be less than the power supply voltage, which is 3.3 V. The maximum specified input voltage is 15 V, so simply divide by 5 to get a signal within range. So:

  1. For a simple voltage divider, V_{out} = \frac{V_{in}R17}{R10 + R17}. Now, the values for R1o and R17 on the schematic probably look familiar as 3.6\ k\Omega and 910\ \Omega respectively. I can’t remember which order I designed this whole things in, but I suspect I started here..! For the sake of argument, let’s say that we’ve already used a 3.6\ k\Omega resistor somewhere else (R10). Now, we can rearrange the voltage divider equation to find our unknown, which is R17: R17 = \frac{V_{out}R10}{V_{in}-V_{out}} \rightarrow R17 = \frac{3 \times 3600}{15-3} = 900\ \Omega. Rounding to a sensible value gives R17 = 910\ \Omega. As we need to divide both the external and battery power, that also gives R10 = R11 = 3.6\ k\Omega and R17 = R18 = 910\ \Omega.
  2. To calculate the value LED2’s current limiting resistor, we need to know the LED voltage and then specify a current through it. For V_\mathrm{LED} a sensible value is 2 V, which covers the high end of red and the bottom of green. Delve into the datasheet for a more precise value, but it’s not a particularly critical value. Now, to choose I_\mathrm{LED} – based on the 3.3 V power supply, we’ve given ourself a current budget of 10-15 mA and LEDs don’t need a large current to function. With this in mind, I chose I_\mathrm{LED} = 1.5 mA. Now, using Ohm’s law, R5 = \frac{V_\mathrm{in}-V_\mathrm{LED}}{I_\mathrm{LED}} \rightarrow \frac{3.3 - 2}{0.0015} = 867\ \Omega. This value is not critical to the function of the circuit, so let’s use the 910 \Omega resistors already in use.
  3. Finally, the pull-up resistor R8. This must be sufficiently small in value to drive any pins the circuit connects to, but not large enough not to drain too much power. The datasheet for the Raspberry Pi’s BCM2835 shows a 50 k\Omega pull-down resistor in the GPIO schematic, but it isn’t specified and could be in the range of 40-100 k\Omega. As long as we pick a value approximately 10\times less than the lowest value, it will drive it to a low enough value (imagine it forming a voltage divider with the GPIO’s pull down resistor). There are a lot of 3.6 k\Omega resistors in use already, so we can just re-use one of those without worry.

Battery condition indicator: Firstly, it’s worth bearing in mind this will be an approximate indication. The chemistry of batteries is dependent on a lot of factors, including temperature, current draw, and charge state. However, this will give a fairly good indication of the battery having, say, 40% charge remaining. When the Raspberry Pi receives this signal, it would be a good idea to start shutting things down in an orderly manner but that’s up to you. This part is a little more complicated, as the comparator here has hysteresis. The battery will be be discharging fairly slowly, so the voltage will also be dropping slowly. At the point at which the battery voltage is equal to the reference voltage, the comparator will be in an undetermined state as both inputs are equal and so it will bounce between on and off. This would be fine if we just had an LED to show that the battery was running low, but as we’re signalling to an external circuit we want a clean transition here.

  1. Note the commonality between the power source indicator and this part and we can re-use parts without having to calculate anything else. We still need to divide down the battery voltage, which we’ve already done using R10 and R17, so that’s just connected directly. The pull-up and and the LED are both identical, so R8 = R9, and R5 = R7.
  2. A full coverage of hysteresis is a beyond this page, and there’s a lot of text already so we’ll cheat a bit here. When you have time, I would encourage you to understand the principles behind this circuit (references are at the bottom of the page). We need to pick a low threshold, a high threshold. As we’re using the battery voltage roughly divided by 5, we need to determine the battery voltage where there is say 40% capacity remaining. For a 3-cell lead acid battery, this is roughly 6.15 V, so our low threshold is \frac{6.15}{5} = 1.228 V. The comparator will transition cleanly to 0 V when this is reached, and from the hysteresis, will not return to 5 V until the input is 1.278 V. The values were found using this handy comparator hysteresis calculator.

Well – there it is! Again, what appears to be quite a simple circuit has a lot of design choices to be taken care of with a reasonably high level of precision. Quite a lengthy article again, but hope it’s been informative! Comments, questions, and suggestions welcome as ever. The links to some articles on hysteresis are listed below.

Curing Comparator Instability with Hysteresis – Analog Devices; Adding Extra Hysteresis to Comparators – Maxim Integrated Products; Hysteresis – Wikipedia

Raspberry Pi HATS compatible UPS – part IIa – trickle charger

Following on from part I and a little discussion on the Raspberry Pi forums here is part IIa, in which I’ll go through and explain and calculate the values and also the reasoning behind each part for the trickle charger. This was going to be for the whole board, but ended up being very long so bitesize chunks for both me and anyone reading!

I made this design with a bit of a nod toward it being produced, and this constraint I hope will become clear during the text.

55 mA trickle charger

Trickle charger

Trickle charger

As previously discussed, sealed lead acid batteries were intended to be the first choice of backup power due to their ready availability, high capacity, and low cost. To keep the battery topped up, a small current is applied – a trickle charger. The charging current is much lower than the battery capacity, and also much lower than the highest charging current the battery could withstand. The current needs to be limited in case a deeply discharged battery is connected; an SLA can take several amps when discharged so could do some damage to the power supply.

Initially, I considered using the SLA charging circuit from the LM317 datasheet. This had a few issues for this design: 1) the dropout voltage for the LM317 is quite high, at least 1.25 V, and it would be prudent to have some voltage headroom headroom as well, 2) the current limiting is on the ground side so there would be different ground potentials connected, which is equivalent to a short circuit. I could have implemented a high-side current limiter, but that doesn’t make any difference to the dropout voltage, and in fact makes the problem worse with a shunt resistor. From a manufacturing point of view, as well, this introduces a lot of new parts, and the LM317 required would have to be in an expensive and large TO220 (or SMD equivalent) package to dissipate the power.

To address these issues, the trickle charger is implemented using a constant current source (CCS) (LED1, R2, R3, Q1) driving a shunt regulator (R15, R16, R21, R27, IC3, Q2, C4). Since the biggest factor we need to address is the charging voltage, I’ll discuss the regulator first and then the CCS. For a lead acid battery at ambient temperature (25 °C), the voltage required to keep one cell topped up is 2.25-2.30 V. So for the intended 3 cell 6 V SLA battery this was nominally designed for, this means a charging voltage of 6.75-6.90 V is required so around 2% accuracy is required. This is actually quite a tight tolerance, and I’ll come back to this is a later post for those with different requirements.

As mentioned, the regulator is based around IC3, which is a TL431 shunt regulator. This is an incredibly cheap and accurate part, and very widely available. It’s used in a lot of things, and you’ve probably got dozens in your house. In brief, it will shunt away excess current to keep a constant voltage at it’s output, hence the name. The TL431 has a nominal 2.495 V internal reference, and an opamp driving a transistor. Here’s how the values were calculated:

  1. The voltage out for the shunt regulator, as shown in the datasheet, is given by V_O = (1 + \frac{R15}{R21})V_{ref}. We know what V_{ref} is, and I’ve defined a value for R21 as 3k6 \Omega. Why 3k6 \Omega? Further down you’ll see this value used a lot, and when manufacturing, you always want to have the fewest number of individual values as that will drive down your inventory costs, and allow you to access bulk buy discounts. Also, having R15 + R21 \approx 10\ \textrm{k}\Omega is a sensible choice, giving around 0.5 mA through the resistor divider. With that information in hand, we can determine R15. Rearrangement of the first equation gives: R15 = (\frac{V_O}{V_{ref}} - 1)R21 \rightarrow R15 = (\frac{6.81}{2.495} - 1) \times 3600 = 6226 \Omega.
  2. Now, we want to use readily available, and thus cheap, resistor values. The Electronic Industries Association specifies a list of preferred values. You can choose from the lowest series which meets your requirements. We need to be quite precise here, so I’ve chosen from the E24 series. So, the 6226 \Omega becomes 6k2 k\Omega with little impact on the function of the circuit.
  3. Calculate the worst case power dissipation in the TL431. The worst case is if the battery voltage is greater than the 6.8 V we set as V_O. In that case all the current is passing through the TL431. Using P = IV, we can work out the dissipation. As shown below, I’ve specified I to be 55 mA, and V is V_O. So, P = IV \rightarrow P = 0.055 \times 6.8 = 374 mW. Now, checking the TL431 datasheet, this is more power than the chip can handle so we need to include a pass transistor, which is Q2. This is in a bigger package designed for heat dissipation, and will handle all the excess shunt current easily.
  4. As we now have the pass transistor, we need to feed the TL431 a little current to generate the desired voltage. As per the datasheet, this current is in the range 1 mA < I_\mathrm{TL431} < 100 mA, so let’s give it 2 mA. The voltage at the junction of R16, IC3, and the base of Q2 is V_O - V_{be}. To conduct current, Q2 requires current applied to it’s base. This I_B is then multiplied by the transistor’s h_{FE}. You can find the values of V_{be} and typical h_{FE} value in the datasheet for the BCP52-16, which are 1 V and 100 respectively. This means the current through the TL431 is equal to the current through R16 plus the current into the transistor’s base. Putting this together gives: I_\mathrm{TL431} = \frac{V_{be}}{R16} + \frac{I_C}{h_{FE}}. Rearranging to find R16 yields: R16 = \frac{V_{be}}{I_\mathrm{TL431} -\frac{I_C}{h_{FE}}} \rightarrow R16 = \frac{1}{0.002}-\frac{0.055}{100} =\ 500 \Omega.
  5. You’ll notice there is another resistor in parallel with R16, that is resistor R27. Why two resistors and not one? Again, this comes down to manufacturing ease: two 910 \Omega resistors in parallel is equivalent to a single 455 \Omega resistor, which is standing in for the value we calculated in step (4). It’s a bit smaller, but that just means there’s a little extra current going through the TL431, which is not a problem.
  6. Finally, C4 is there to provide some bypassing. It’s value isn’t (usually) too critical, and may be anywhere from 100-470 \muF.

Well, that was a rather in depth look into something which on the surface may appear quite straightforward! I suggest taking a piece of paper and checking everything’s as calculated and makes sense. We’re half way there for the trickle charger, next up is the constant current source: The values for the CCS are derived as follows:

  1. LED1 has a roughly known value (~2.2 V for a green LED), specified in it’s datasheet
  2. Q1 is a general purpose PNP bipolar transistor. It is in a SOT223 for heat dissipation (we’ll calculate this later). The values of interest in it’s datasheet are: hfe and Vbe.
  3. Emitter resistor R3 sets the current. The transistor will try to maintain the same voltage drop across R3 as (VLED + Vbe). This gives us the equation we need to solve: R_3 = \frac{V_{LED} + V_{be}}{I_C}. Since we know all these values, we can then calculate R3 as: R_3 = \frac{2.2 - 1}{0.055} = 21.1 \Omega. As for the regulator, we need to pick a “real” value; in this case, 22 \Omega is ideal.
  4. Next, we need to check the power dissipation of R3. Using P = I^2R, we get 66.6 mW. It’s prudent to specify at least double the wattage, so I’ll specify a 250 mW part for this position.
  5. Finally, the power dissipation in Q1 should be determined. I’d intended for a 15 V power supply to be used as the maximum for the power source, which will give us the worst case dissipation in Q1. The voltage across Q1, V_{CE} will be: V_{CE} = V_\mathrm{supply} - V_\mathrm{R3} - V_\mathrm{regulated} \rightarrow V_{CE} = 15 - 0.055 \times 22 - 6.8 = 6.99 V. Now, P = I_CV_{CE} \rightarrow P = 0.055 x 6.99 = 384 mW.

So, we’ve successfully designed a trickle charger for our lead acid backup battery! As I hope was clear through that was that the values you see on schematics take quite a lot of calculation, and there are little tricks like holding values constant and working from those constraints. As ever, any questions, feel free to comment here or on the forum page.

 

Raspberry Pi HATS compatible UPS – part I

Introduction: A previous comment, a couple of weeks off, and the release of the Raspberry Pi B+ HATS specification. led to the development of this uninterruptible power supply (UPS). While laid out with the Raspberry Pi specifically in mind, it’s a very flexible design that can be used with any number of low voltage systems. I’ll elaborate in a future post(s) the steps and changes needed. In the first instance, the battery supply is intended to be a sealed lead acid (SLA), but can be anything you need to fit your own requirements within certain parameters. I am also going to work through the rationale behind parts choices, and also in choosing component types and values. This is an all analog design (excluding the EEPROM, more to follow), and for a lot of people just starting out, the values are seemingly plucked from the air. This project is completely open to suggestions and comments, and you’ll be able to find the thread on the Raspberry Pi forum.

Photo-CarBattery.jpg
Photo-CarBattery“. Licensed under Public domain via Wikimedia Commons.

Open a copy of the v0.01 schematic here to follow.

Operating Principles: a UPS is required for two broad categories of need: 1) intermittent mains power, and 2) critical systems. An example of the first kind would be perhaps an isolated or underdeveloped area where mains supply is rationed. The second need covers things from a medical device which must function, to a security system that needs to be on constantly. Or, of course, if you just want your RPi to keep the time if the lights go out..! The UPS must, therefore, swap seamlessly and instantly between the mains supply, and your chosen backup power supply. In this case, I’ve chosen a battery (I’ll explain why later), but you could use a bank of capacitors, or a mechanical flywheel, or anything else that can store electrical or potential energy, perhaps a reservoir if you have the luxury!

Electrically. the simplest way to achieve this is with a diode OR bridge. This is a very simple circuit that consists of two (or any number of) diodes connected at their cathodes. The individual anodes are then connected to, in this case, the individual power supplies. As diodes only conduct when the anode-to-cathode voltage is greater than 0.6 V (for a standard silicon diode), the supply with the highest voltage will power the downstream circuit. This also means, importantly, that there is no gap if one supply drops out and so this is the basis of our UPS. In the schematic (you can open it above), the diode OR bridge is made up of D1 & D2. They connect to +9V and V+ respectively, which is how I’ve chosen to label the external supply and the battery supply. While the external power source is present, it would be good to keep the backup battery topped up with charge. This is achieved with the components in the top left of the schematic (R2, R3, R15, R21, R16, LED1, C4, Q1, Q2, IC3), which provides a steady ~50 mA to keep the battery charged. There is a lot of scope for variation here depending on battery types, supply voltages etc, which I’ll cover more fully later.

The output from this circuit needs to be 5 V. As shown above, one supply has to be at a higher voltage than the other, and there are the added problems of the voltage drop across the diode OR (0.6 V) and the fact that batteries aren’t a constant voltage – they change with their state of charge or discharge. This means that a voltage regulator is required to provide power within certain bounds. The block which does this is located in the bottom right of the schematic. I’ve chosen to use a buck switching regulator to do this on the grounds of efficiency. While linear power supplies are generally cheaper and much simpler electronically and physically, they literally burn the excess energy as heat. I’ll elaborate on the switching regulator and its values and layout in a further post.

This leaves the circuit in the top right centred around comparator IC2. The power supply should be able to communicate with the target, and in particular two pieces of information are useful: 1) the target is running on backup power, and 2) that backup power is running low. This is implemented by simply comparing the battery and external power supply voltages, and then comparing the battery voltage against a stable reference respectively. The reference is formed by IC1, which also provides the regulated 3.3 V supply required for communication with RPi. The design also provides visual feedback in the form of LED2 and LED3. The external/backup supply indication is centred around IC2A, and the low-battery voltage sense is centred around IC2B. You may notice that there is positive feedback (R13) implemented in IC2B; this introduces hysteresis to the system, and I’ll rationalise the values in a later post.

This will be a fully open-source project, and the files will be hosted at: https://github.com/awjlogan/RPiUPS

 

KiCad revisited

This refers to KiCad 2014-03-19 build 4756 on Xubuntu 14.04

It’s been quite a while since I last had a go at using KiCad as a complete PCB design package. Recently, for a few reasons, I’ve wanted to go beyond the usual Eagle free constraints and so this was a good opportunity to see if KiCad had matured from previous incarnations. So, as you’d hope on a Debian (or Windows, or OS-X) system a simple apt-get kicad would suffice. Well, it did, for a while. I went through the Getting Started tutorial for the schematic and saved the netlist etc. Problems started at cvPCB where none of the package modules seemed to be installed and they weren’t anywhere on the system. There appeared to be a link to a Bazaar repository to download the original .mod files, but that’s been taken down. After a lot of searching around, it turns out you have to do a complete build from scratch to get KiCad up and running into a usable form after getting it from the Bazaar repository. For Ubuntu users, you can try this (I’ve only tested this on Xubuntu 14.04, but it should work for others).

sudo apt-get install bzr
sudo apt-add-repository ppa:js-reynaud/ppa-kicad
sudo apt-get update
sudo apt-get install kicad
cd kicad
./kicad-install.sh --install-or-update

In order, this will 1) get the source management software bzr, 2) add the location of the KiCad files, 3/4) update your local version info and get the kicad sources (this is a 550 MB download), and 5/6) build and install KiCad. Compiling will take quite a while, so get a cup of tea while it does so. This is dependent on your computer spec, but mine took around 25 mins (2.6 GHz Core2Duo, 4GB RAM). Be aware you will have to enter your su password during the build. This will leave a big folder at the end called kicad_sources, which you may safely delete.

You should now have a working copy of KiCad installed on your computer ready to go! With this version, there are some inconsistencies with the Getting Started guide, but you can work around them reasonably easily. Honestly though, this is not what KiCad should be doing to increase its user share. In fact, while searching for solutions to installation problems, changing to other software was a common theme. Last time I tried it, it was so close to being a perfect FOSS replacement for Eagle, and I was really hopeful a couple of years later. However, it’s a major headache for people who just want it to work out the box rather than typing in obscure and unknown terminal commands and watching pages of scrolling text for half an hour. The packaged version doesn’t have to be the latest and greatest, but it does have to just work. There’s also no point in updating documentation until there’s at least a fixed version it refers to.

Anyway, I’m looking forward to using it now there’s a working version installed. So far, it’s looking hopeful and full credit to the KiCad developers for making such a good piece of software. I hope they can sort out their packaging so all this work doesn’t go to waste with a withering user base. Would welcome anyone’s own experience and thoughts with KiCad below.