Detecting power loss (here: Pixl.js)

Posted on
  • I am using a Pixl.js for a bike (velomobile) computer project.
    It should monitor the onboard LiPo battery with 11.1 V and is also driven by it.

    In case off immediate power loss (battery is dying or just disconnected) there should be a triggered routine to save some current data to flash.

    I was considering this:
    Battery power is monitored by an INA226.
    So one way is to regularly poll and detect an undervoltage.
    But this is slow and could only work for a slowly dying battery - no sudden disconnect.

    So another idea is this:
    Put a capacitor parallel to the Vin Pins to gain some time after power loss.
    Put a diode in front so the capacitor only feeds the Pixl, nothing outside.
    Wire the feeding current (before the doide) with a resistor (~10k) to a digital input.
    Put another small diode between 3,3 V and the input so the voltage will not rise above 3,3 V. (Maybe a Z-diode of about 3,3 V between GND and the input would be better?)
    As soon as the outside voltage drops, the pin will change to low and trigger a watch set on that pin.

    Will that work?
    Anyone tried something similar or has a better idea?

    Some ASCII Art sketch for clarification:

    Din  O---+--[R]----+
             |         |
             V diode   |
             -         |
             |         |
    3v3  O---+         |
                diode  |
    Vin  O---+---|<----+-------------O 
            +|                       + LiPo
            === capacitor
             |                       - LiPo
    GND  O---+-----------------------O
  • If you don't have other load just the Pixl and an INA226, it can run several seconds on a relatively small supercap. Bought a 5.5V / 0.something Farad one ages ago, and that can store enough energy to run the Pixl with LCD backlight on for seconds.

    Or modify your Pixl.js to use CR2032 battery as backup:
    CR2032 battery backup - the CR2032 can be used as a backup when Vin/USB power is not present. Cut the trace in the solder jumper below the CR2032 holder, and add a surface mount diode to the two pads to the right of it.
    So it could even run autonomously on the CR2032 after power loss. Altho 2.7V is the lower limit of the INA226, so that might not work reliably if your battery is low...

    I think on your schematic the Din would stay floating around 3.3V, if +LiPo was disconnected.
    If the INA226 doesn't work reliably on battery, I think a simple voltage divider + comparator in the Pixl would be enough. Check then NRF52LL on how to use the comparator.

  • Thanks, @AkosLukacs.

    I was considering the CR2032 backup, but was scared to mess with two inputs.
    Thanks for sorting that our.

    Actually it would be enough to have some cycles left for a EEPROM write, so a powercap or elko could just do.
    What about another pulldown between Din and GND?
    EDIT: I just found input_pulldown in the pinMode() documentation. That would do the trick, wouldn't it?

    It would be nice to use a setWatch() instead of polling external hardware.
    Maybe I'll give t a try later...

    The NRF52LL reference looks interesting, too. Did not know about it.

  • The diode idea sounds good. One other thing you could look at is if you make a potential divider with 2 resistors, you could read the analog value of the pin to get the voltage, but you could also use setWatch on it so you get called back as soon as the voltage drops

  • I just wired it up.
    Used a 10k resistor to pull D7 against Vin (about 12,5 V from my fully charged LiPo).
    Capacitor is a 1000uF / 25V.

    Strange thing: The display flickers and blacks out rhythmically while D7 is not configured.

    I used this script to test powerdown detection:

    function onInit() {
      pinMode( D7, "input_pulldown" );
      setWatch(function(e) {
        console.log( "power out" );
        var t=0;
        setInterval(function() {
          console.log( "t=", t++ );
        }, 1000 );
      }, D7, { edge: "falling"});

    It gives me about 6 seconds console feedback before dying with the LED off:

    power out
    t= 0
    t= 1
    t= 2
    t= 3
    t= 4
    t= 5
    t= 6

    With LED set immediately after power down it is only two seconds (still enough for some cleanup probably).

    So basically it seems to work.

    @Gordon: is it normal that it behaves strange when certain pins are used as inputs bot not configured yet?

  • PS: Without BLE connection and LED off it is even 9 seconds feedback on the LCD!

  • So you're connecting 12v to an input pin via a 10k resistor? I'd expect that to maybe cause issues, yes.

    Basically 10v drop at 10k is 1mA going in to the chip, but the chip itself draws way less than that when idle. While it's caught by the chips diodes, it means that it is powering the 3.3v supply via an IO pin. I reckon if you look at the 3.3v pins you'll find that the voltage is well above 3v

    I would add a permanent resistor to pull down, so that the voltage always stays in the 3.3v range - otherwise it might cause damage to the chip.

  • Not quite.
    The 12V pulls the pin high, but the (external) diode shortens that to 3,3V.
    I just measured the voltage to be about 3.8V on the pin.
    Do you think this is still an issue?

    For pulldown (in case of power loss) I used the internal resistor via pinMode()

    As with the flickering this may have been cause by wrongly wired MOSFETs or a shortage between 3,3V and 5V PIN.
    Prototyping got a bit messy lately... ;-)

  • Something like that gets you to safety (even while recharging when plugged in):

                        .-----------+----| LOW R   |------- 12.5..14.5V
                        |           |    '---------'
                       .-.          |     100..120 R
                       | | 75..100  |
                       | | k        |
                       '-'         ---
    analog .-------.    |           /   13.8 - 15.0          
    in ----+ opt.  +----+          /_\  V Zener 3W
    put    | some  |    |           |   (Good Quality)
           | fil-  |   .-.          |
           | ter   |   | | 22       |
           | (RLC) |   | | k        |
           '---+---'   '-'          |
               |        |           |
    GND -------+--------+-----------+-----------­----------- GND

    You could add additional safety paralleling the zener w/ a fast transient suppression device / circuitry (TVS; google for 'transient voltage suppressor circuit'). 'After' that you can then also use the power to feed a regulator that runs you digital electronics, including Espruino.

  • Thanks for the suggestion, I'll change the resistor setup tomorrow to be on the safer side.
    No voltages above 12,6 V (LiPo 3S) - and no charging while in circuit possible.
    So that should simplify things a bit...

  • @ChristianW, I picked intentionally a bit higher values because you do not want to add additional load on your battery. On the other hand - compared with the power motors consume - current draw is negligible. Important for operating sensitive electronics next to power / servo electronics requires a good 'separation'. The first cars with electronics had two batteries to make sure the power for the controlling electronics is more stable and reliable then the power for driving heavy loads (starter, window motors, etc.). @DrAzzy has quite some experience in - and sells also components for - driving power hungry devices (lights). Search the forum and go to (do see him not that often anymore on the forum, though).

  • @allObjects thanks for the clarification.
    Actually it's not for a motorized vehicle but for a velomobile - a fully faired three wheeled recumbent. Because there is no dynamo you need a battery to power lights, indicators and horn.
    And if that goes down on longer trips you are busted, so I'd like to know how much is left and how much I currently draw.
    Because it is easy to do I'd also like to measure speed, cadence and distance - and store them in an EEPROM (see other thread). And the best time to store is when the battery is pulled.

  • I just measured the voltage to be about 3.8V on the pin.

    Yes, that should be fine, although 3.3v would be 'nicer' to it if you can do that ;)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Detecting power loss (here: Pixl.js)

Posted by Avatar for ChristianW @ChristianW