New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries ;-)


  • Mod

    Hi there!

    Inspired by the Blog entry at http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ I decided to write a simple Arduino library to measure VCC level without any external components!
    This library can be used to measure the VCC level from e.g. battery powered sensors that do not use a voltage regulator but are powered directly from the batteries and send the battery level to the gateway.

    The trick is to use the AVR's internal 1.1V reference to measure AVcc. This does not require an external voltage divider.

    The Vcc component can report the VCC level either in volts, or in percentage. Reporting in percentage is a nice way to report the battery level in MySensors!
    For example:

    #include <Vcc.h>
    const float VccExpected   = 3.0;
    const float VccCorrection = 2.860/2.92;  // Measured Vcc by multimeter divided by reported Vcc
    Vcc vcc(VccCorrection);
    
    static int oldBatteryPcnt = 0;
    void loop()
    {
    	int batteryPcnt = (int)vcc.Read_Perc(VccExpected);
    	if (oldBatteryPcnt != batteryPcnt)
    	{
    		gw.sendBatteryLevel(batteryPcnt);
    		oldBatteryPcnt = batteryPcnt;
    	}
    }
    

    Deviations can easily be corrected for by running one of the example sketches and at the same time measure VCC with a multimeter.
    The correction factor should be entered as (VCC multimeter/VCC reported) in the constructor of the Vcc component (the VccCorrection parameter in the example above).
    See the example sketches and code for more info.

    The library can be found at: https://github.com/Yveaux/arduino_vcc
    Or download as ZIP: https://github.com/Yveaux/arduino_vcc/archive/master.zip

    Have fun!


  • Mod

    Hi, nice stuff but...
    What would be the advantage (except for not having to add those 2 resistors)?


  • Mod

    @marceltrapman The resistor divider always draws some current, even when the Arduino is sleeping.
    This just shortens the battery life and doesn't bring anything...
    Furthermore VCC measurement can be added to any hardware, even if the resistor divider is not on the board.


  • Mod

    @Yveaux But it would mean that every board has to be measured is it not?


  • Mod

    @marceltrapman What do you mean? You don't have to measure it 😦 Only when it makes sense, e.g. for battery powered sensors.


  • Mod

    @Yveaux I mean that you have to 'calibrate' the value (sorry for not being more clear) on first time use. And yes, obviously only when you intend to use batteries 🙂


  • Mod

    @marceltrapman you don't have to calibrate it to use it. Only the 1v1 reference is created using a bandgap diode which can have slight deviations for each AVR you buy ( approx. In the 1.0..1.2 volts range) If you don't calibrate it, the reading can be 10% off.
    If you need accuracy, just calibrate it!


  • Mod

    @Yveaux ah ok, that clears things up 🙂 Thanks


  • Mod

    @Yveaux cool job but not technically correct

    the resistor divider is always needed if measured voltage is above the reference voltage used.
    you cannot brake this rule, because by hardware any value above reference voltage will be read as 1023

    actually you do other thing. You use AVCC as a reference instead of an internal 1.1V reference and you do automatic calibration
    this allow you to measure voltage bellow AVCC but higher than 1.1V without resistive devider


  • Mod

    one more comment
    just had a look inside Vcc.cpp

    you use a range from 0 till expected to calculate percentage
    it is incorrect

    actually any battery type is working only inside a certain voltage range
    for example alkaline bellow 0.6V is 0% capacity
    li-on bellow 2.7V is 0%

    to simplify you can treat linear discharge (it is not correct but at least it is much better)
    but you need to use an interval starting from none zero


  • Mod

    @axillent You're right, but I implemented it like the external voltage dividers I've seen in sketches.
    I'll add a configurable lower bound to the interface.


  • Mod

    @Yveaux said:

    @axillent You're right, but I implemented it like the external voltage dividers I've seen in sketches.
    I'll add a configurable lower bound to the interface.

    I recommend you to read atmega328 datasheet though. You will get an original inside on all things 🙂
    you will also be surprised that atmega328p has an internal (but very low precision) analog temperature sensor


  • Mod

    @axillent
    Off-topic: why the heck did @hek add support for this temperature sensor to mysensors 1.4 library??? IMHO It doesn't belong there at all!

    from my experience this temperature sensor is very useless for any sens purpose
    you probably will get an acceptable result after calibration, but calibration itself will kill any benefit
    it is much easier to use an external pre-calibrated sensor


  • Mod

    @axillent Did you just edit my post???
    I didn't type that and most part of my reply got deleted. ….
    Is this forum getting censored?


  • Admin

    @Yveaux said:

    Off-topic: why the heck did @hek add support for this temperature sensor to mysensors 1.4 library??? IMHO It doesn't belong there at all!

    You're right. The temp reading will probably not be there in the final version. It's pretty useless and was added for fun when I was testing.

    Don't know what happened to you post. Will check with Alex... I'm sure he didn't mean to censor anything 😄



  • I think the big question beside all methods, is none take in account the non linear discharge function..

    I mean, considering a Lipo between 4.2V and 3.2V, it's not with 20% at 3.4V (but with 1-1.5% instead).

    For a correct percentage value, we have to interpolate the discharge function of a battery.

    Another alternative is to measure current but we have to know the initial battery charge (mAh).


  • Mod

    @Yveaux sorry, something went wrongly
    I just tried to do a reply


  • Mod

    @BSoft Aargh! Can we just stick to the thread?

    I think measuring VCC in this way is a neat and simple method working IMHO just as fine as an external voltage divider with some added advantages.

    Of course the discharge curve is non-linear and you can indeed model it and compensate for it in software, but that's not the (initial) scope of the library I wanted to present here.

    I just wanted to create a re-usable library for it and think it can be a nice addition to MySensors sensor nodes.


  • Code Contributor

    The tone in here sounds a bit dull..

    Well,
    @yveaux
    I Like the contribution! I will use it. Thanks!


  • Mod

    @Yveaux said:

    I'll add a configurable lower bound to the interface.

    Done. Now you can pass the expected range when reading voltage as a percentage, e.g. [0.6,..,1.5] volts for AA alkaline.
    I also updated the examples.



  • This will only work if the battery is directly connected to the CPU. If using an inverter you need the resistors. They only draw 1uA8.


  • Hero Member

    I don't see how this technique saves any resistors or battery drain. I see there being two different measurements - input to a voltage regulator and input VCC to the uC, which are measured differently.

    By "this technique" I mean using VCC as the analog reference (as usual) and reading the internal 1.1V internal source. That gives you 1.1v as a fraction of VCC, so you can easily calculate VCC..

    The two measurements:

    1. You want to measure regulator INPUT voltage. You typically use a voltage divider to reduce the regulator INPUT voltage to less than VCC, which is used as the ADC reference. The technique describe here cannot be used for that measurement, so it doesn't save resistors or battery drain.

    2. You want to measure regulator OUTPUT voltage - or raw voltage if there is no regulator - as applied to the uC for VCC power. The technique described is the way to go, and a voltage divider is inappropriate.

    If you used a voltage divider on VCC (case 2) and measured it against the normal VCC reference, all you'd get is a fixed value determined by the resistors, not reflecting VCC.

    Summary: if (1) you want to measure regulator input, use a voltage divider, but if (2) you want to measure VCC then use this (well known) technique. Neither approach substitutes for the other. And you can use both, if you want both measurements (eg: if you are concerned about regulator drop-out).

    Side note. There are yet other approaches. You could use the 1.1v source as REFERENCE for the ADC instead of VCC, and on the ADC input use a voltage divider from VCC or from regulator input, with a large enough ratio to bring the measured voltage down to less than 1.1v. If you had a 5v processor fed via regulator from a 7.2v source, you could measure the former with a 4:1 resistor ratio and the latter with an 6:1 or greater ratio. This is not commonly done.

    If you DO use a voltage divider for measuring voltage (when appropriate), you can reduce the current wasted by using large resistors in the hundreds of Kohm range, AND putting a cap across the lower resistor. You need the cap to reduce the effective impedance into the ADC. If you just use large resistors, the internal capacitance of the ADC circuitry will be charged too slowly during measurement.


  • Hero Member

    @Yveaux said:

    Off-topic: why the heck did @hek add support for this temperature sensor to mysensors 1.4 library??? IMHO It doesn't belong there at all!

    I think the internal ATMega temperature can be useful in some circumstances.

    Obviously if your goal is to measure weather, there are much better external sensors. But if your sensor is primarily detecting motion or open doors or controlling relays whatever, you may not always have an external temperature sensor included - but you always have a internal one, for free - albeit low accuracy.

    Or if you do have an external temperature sensor, the internal temp could be used as a sanity check - if in your physical setup the processor should be close to the same temperature as the external sensor, but the two are way off, something needs to be checked. A bit of monitoring both could tell you the normal range of divergence. Again, this is free - no extra board space or cost.

    Another use case is processor monitoring, The internal sensor is actually better at telling you the die temperature, if there's any chance of it overheating (whether or not there is an external sensor), If the board is sealed, just how hot does it get inside the box? If you are putting out a lot of current on a lot of outputs, is the processor heating up? (For weather you'd have the external sensor outside the box and wouldn't know the inside temp).

    Yeah, it can be optional, but but the internal temperature is not worthless.



  • @Zeph Your statement that there are two cases to be considered is right.

    A small ceramic bypass cap (say 0.1 uF) is also important on the Voltage dividers to minimise ADC measurement errors due to any noise on these high impedance nodes. Cheap electrolytics are not applicable.

    Also need to watch the settling time when switching the multiplexor - need at least 5 msec (or more; depends on size of cap connected to Vref pin):

    http://capnbry.net/blog/?p=167

    and see the heading "Changing reference voltage" here:

    http://meettechniek.info/embedded/arduino-analog.html


  • Mod

    @Zeph

    I think the internal ATMega temperature can be useful in some circumstances.

    it is hard to find such circumstances while precision is +-6C
    without calibrations is hard to get better


  • Mod

    @Zeph said:

    1. You want to measure regulator OUTPUT voltage - or raw voltage if there is no regulator - as applied to the uC for VCC power. The technique described is the way to go, and a voltage divider is inappropriate.

    This is exactly the use-case where I wrote the library for, or when you want to add supply level measurement to a battery powered sensor node which doesn't have a voltage divider circuit.

    Possibly I didn't make that clear in the subject.

    Update changed topic subject


  • Mod

    @Zeph said:

    I think the internal ATMega temperature can be useful in some circumstances.

    Again, I did not want to question the (accuracy of the) internal temperature sensor, but had my questions about the location of the sensor readout code.
    MySensors is a wireless networking library, which has in its basic functioning nothing to do with temperature, or any othe measurements.

    It should deliver the messages from A to B and back. Nothing more.

    When it makes sense to use the internal temperature sensor in your sensor node, please do, but put the code in a separate library please! (I know at least @hek understood the point of my remark 😉 )


  • Hero Member

    @Yveaux
    OK, I understand your purpose now. I was thrown off by the comparison to using a voltage divider, which seemed to suggest this approach eliminated the need for a voltage divider and the current it costs. I don't see this technique as having an advantage over the voltage divider technique, but rather that it is used for a different measurement.

    Now that everybody is clear on that - thanks for the contribution!


  • Mod

    @Zeph said:

    Now that everybody is clear on that - thanks for the contribution!

    You're welcome! 😃


  • Hero Member

    , but had my questions about the location of the sensor readout code.

    When it makes sense to use the internal temperature sensor in your sensor node, please do, but put the code in a separate library please! (I know at least @hek understood the point of my remark 😉 )

    OK, thanks for making that very clear and now that I understand, I agree!

    In terms of whether there can be some value to even the uncalibrated internal temp, my point was that there are some reasonable use cases for it. In terms of where to put the code in those cases, yes it makes sense to optionally make this one of the reported values in a sensor node, rather than wiring it into the transport library.


  • Contest Winner

    If I understand this method correctly, you want to read the VCC of the MCU? But this does not have to be the actual battery voltage. Most battery powered designs I have seen here uses a step-up converter to drain the battery as much as possible while maintaining a regulated voltage supply to the MCU. So even if battery voltage drops, MCU gets it's nominal voltage, and then this algorithm would still (incorrectly) report 100% even though the actual battery voltage is dropping significantly.

    I could have misinterpreted things though, so I would appreciate correction 🙂

    I would rather consider using the original resistor divider proposal combined with a FET or similar to be able to control when I want to do the measurement. The original simpler design I find a bit too wasteful as it constantly drains the battery.

    Or perhaps there are battery management circuits that can do both step-up regulation combined with input sensing that can interface with the MCU to be able to get some real meaty metrics of the battery? Might be expensive, but cool 🙂


  • Mod

    @Anticimex Ok, updated the thread subject once more 😉


  • Mod

    @Anticimex said:

    I would rather consider using the original resistor divider proposal combined with a FET or similar to be able to control when I want to do the measurement.

    That sounds like a plan 🙂

    I don't want to hijack this thread but can you give an example (schema) of this?


  • Admin

    @Yveaux said:

    @Anticimex Ok, updated the thread subject once more

    Haha! The subject is quite long now. 😄


  • Contest Winner

    I have not done the maths for it but just off the top of my head, I was imagining something like this:
    schemeit-project (1).png

    The resistance of the FET needs to be taken into account, and R1 and R2 also should be biased to allow the FET to be kept in firm saturation, but this should allow for a minimum current leakage when the "sample_en" output from the MCU is driven low.


    P.S. if you are curious on the schematic, I made it online using SchemeIt by DigiKey. Quite nice if you want to scribble down a decent schematic to do a forum post 🙂



  • Like the schematic drawing program - that's good.

    Not sure that all this effort is needed. We're only looking at 1uA8 with 470K and 1M divider at 2V6. If large value resistors are used, then don't forget the high quality (low leakage) bypass capacitor.


  • Mod

    @Anticimex Thank you very much!!!


  • Contest Winner

    @a-lurker said:

    Like the schematic drawing program - that's good.

    Not sure that all this effort is needed. We're only looking at 1uA8 with 470K and 1M divider at 2V6. If large value resistors are used, then don't forget the high quality (low leakage) bypass capacitor.

    Yes, a bypass of good quality is probably a requirement when tapping the cell directly and with minute currents.

    However, I disagree a bit on the need for the extra effort.
    The current is small, yes, but it is also constant. As months goes by, this will make a difference, I believe, although a small one. But if I could make my battery last a week longer by just adding a transistor to the unit, I would still do it, and not have to sit there with a bad feeling in my stomach that there is a voltage divider somewhere in my vicinity bleeding precious battery charge. Yes, you can call me anal or an OCD-sufferer 😉
    A single transistor is also not that much more work and I believe most sensor nodes won't have a desperate shortage of GPIO. And with or without a FET, some effort will anyway be needed to calibrate the whole setup so the calculation provides a useful result.

    @marceltrapman You are welcome 🙂



  • OCD - I'm obsessive about OCD too!! So no problem with the FET approach, as long it's turned off properly, otherwise it will also leak a similar current. But it should be possible to get it work OK. There will be an increased measuring time. eg turn on FET, wait till circuit settles and then measure. While that is occurring, the CPU (and maybe the radio) are drawing full power (thousands of times as much as the divider does). That's also wasted power that needs to be taken into consideration and may outweigh the savings?


  • Contest Winner

    @a-lurker 🙂 yes, cutoff of the FET needs to be handled as well. If nobody beats me to it, I'll whip a testing circuit together to measure the currents, but considering my current schedule I am afraid it will take a few weeks for me to get around to it. I am going to need the better precision meter in my office since any leaks in the FET will be small and difficult to measure properly. But with pen and paper it should be possible to design around it in theory and only measure the "finished" design to benchmark it. I hope to get some time tomorrow to provide an updated schematic with a few values for somebody adventurous to evaluate.


  • Hero Member

    OK, now we are recapping the investigation JeeLabs did a year ago.

    I recommend these articles:
    http://jeelabs.org/2013/05/15/what-if-we-want-to-know-the-battery-state/
    http://jeelabs.org/2013/05/16/measuring-the-battery-without-draining-it/
    http://jeelabs.org/2013/05/17/zero-powe-battery-measurement/

    He did test the various circuits and values. The conclusion is that using a FET you can get the current drain even lower, but using 10 Mohm resistors and a cap he got the drain to sub-microamp.

    Once you get the drain down that low, the uC sleep current and battery self-discharge and capacitor leakage and any other sensors are going to dominate anyway. An open pin spec'd at +/- 1 uA, so even a sensor pin which you think is disabled may be drawing more current than that.

    Still, it can be fun to really go for the minimum possible.



  • @ ZEPH Good links. Pretty much says it all - including that in the cct posted above the divider could have leakage into the ADC I/P when the FET is off.

    Something else that's probably important is to measure the Voltage just before going into sleep, rather than when the CPU powers up. This gives a better indication of what the battery condition, because the measurement occurs a little while after the load has been applied to the battery. If you measure when the CPU powers up, the battery has the whole sleep time to "recover" from its last wake up. I'm pretty sure this could be demonstrated by making a start and end measurement and seeing what happens over time.


  • Mod

    @Zeph Nice, thank you for those links!


  • Contest Winner

    @Zeph Good. The third "conclusion" is pretty much what I had in mind as well. I am going to simulate it tonight, and see if it is possible to increase the resistor values. I think they are a bit small in my opinion, since it makes little use to reduce current consumption when doing the sampling, if the total consumption will be greater than using a low current divider.
    Regarding when to do the sampling, I agree with @a-lurker that this should not be done during boot. Rather, it could be done on "every hundred sensor sample transmission" or something like that as a first action (i.e. not when radio is running), or as a last action before sleeping.
    Ideally, a lot of samples could be taken and an average calculated, but that would probably defeat the purpose; to reduce current consumption when determining battery level.

    I'll get back with what I find.

    By the way, if we want to go fancy, there are a few commercial alternatives that provide a lot of probably over-the-top features, but they might be quite power saving:
    Texas Instruments: bq2010, bq2018, bq2019, bq2023
    Maxim: MAX1660, MAX1780
    Nat Semi: LM3822, LM3824
    Dallas Semiconductor: DS2438, DS2760


  • Mod

    So, this whole thing turned into something else 🙂
    Maybe the thread should be split so that it is clear where to look later...

    The question I have is this, and it concerns wiring.

    From other discussions I understand it is better to wire the radio separately with the cap as close to the vcc and gnd as possible.

    Now, when doing the supply level resistors etc. is it preferred/better to wire these separately as well or can I wire the board and sensor(s) direct after/to the setup.

    I hope my question is clear...


  • Admin

    I can split, but what should the topic be?


  • Mod

    @hek said:

    I can split, but what should the topic be?

    Something like 'improved wired battery check' or 'better (power saving) battery check'...


  • Contest Winner

    I have run some simulations on a low-power solution using this setup:
    sim_schem.png
    Sorry for the somewhat messy schematic. For those of you unfimiliar with LTspice, this is what the above is:
    V1 is the battery to be measured. It's nominal voltage in this case is set to 3.3V. The voltage is stepped from 3.3V to 0 in 0.5 increments in the simulation.
    For some noise suppression, it is decoupled with C2 but this makes little difference for the simulation I have made.
    The battery voltage is fed through a PMOS FET (M1) before it enters the voltage divider. Below, I will use the term open for the FET when it is not conducting current and closed when it is.
    The FET is by default tied in the open state using R3 as pull up to Vin. M1 is closed by grounding it's gate, or in this example by pulling Enable low from the MCU. V2 in this example serves as a crude simulation of the MCU grounding the Enable signal after 0.05s in the simulation time domain (simulation is executed for 0.1s).
    The neat feature here is C1, which isolates the MCU output pin from the FET, thus preventing leakage through the MCU. When Enable goes low, M1 is closed momentarily, before C1 regains the charge from the pull from R3 and is opened once again. The value of C1 and R3 can be picked to suit the speed of the MCU (the MCU needs to take the sample before M1 opens).
    When Enable is left floating, any charge pushed into C1 is fed back to Vin, and is thus "free".
    This also allows the voltage divider to span the entire ADC range (depending on what ADC is used, internal references and such).

    Below is the resulting waveforms when using a Fairchild FDS9934 as FET (somewhat equivalent breadboard-friendly component could be this).
    sim_wave.png

    Note here the large span of V(sample) as well as the peak current at the largest voltage level (2.4uA). The current remains constant for a brief period of time (decided by C1 and R3) and then dissipates down to 0, even if Enable is kept low. The current can be reduced by manipulating R1 and R2, but the circuit becomes more sensitive to noise as the current used decreases (as can be seen for lower currents in the simulation, where capacitances in the FET start to make a difference.


  • Mod

    @Anticimex nice, thank you!


  • Admin

    Great stuff @Anticimex . But I understand just a small bit of it. 😉


  • Contest Winner

    Thanks guys,
    I'll happily elaborate more on the details, but perhaps it is best if you state some specific questions regarding parts of it that are difficult to comprehend. Imo, working with sensors should not require a degree in engineering, so nobody should "feel stupid" for asking questions 🙂


  • Mod

    @Anticimex said:

    Imo, working with sensors should not require a degree in engineering, so nobody should "feel stupid" for asking questions 🙂

    When I have questions I will ask 🙂
    But, like @hek, I need to let things 'sink in'.
    And I really want to understand.
    To be honest I have learned a lot (and spent a lot) since I stumbled upon MySensors.
    Wonderful new hobby!


  • Hero Member

    As long as you connect VCC direct to the battery I do not understand why people opt for using an external voltage divider optionally with a FET to reduce standby current. Additional components and current consumption while only a possible small improvement in accuracy.

    For the voltage divider the ADC reading is 1023 x R2 / (R1 + R2) x Vcc

    For the 1.1 Volt reference the reading is 1023 x 1.1/Vcc

    If you are using +/- 10% resistors (or the FET resistance is not measured correct or varies), the accuracy is more or less matching the (uncalibrated) bandgap reference method.


  • Mod

    @daulagari I guess the discussion depends on whether you have a software-mindset or a hardware-mindset.
    The software guys currently seem to form a minority on this board...
    I still think, after all discussions and distractions, that this way of measuring can be very usefull, when you understand the limitations.


  • Contest Winner

    I have not studied what support for measuring Vcc is built into the Arduino, but if there are support for doing that, I am sure it should be adequate. Regarding the external circuitry, my point is just that if you are going for an external solution, you might just as well design it to consume a minimum amount of current, as that is a one-shot optimization. Yes, it is a couple of extra components, but to me, that outweigh the limitations of the simple voltage divider in the long run.
    Personally, I would even consider a battery management unit. Such a thing should be able to handle both charging (if you want that feature) as well as readback (with battery health compensation).


  • Hero Member

    @Yveaux

    I guess the discussion depends on whether you have a software-mindset or a hardware-mindset.

    Being on this forum you are likely not having a hardware- or software-mindset only 😉

    @Anticimex:

    I have not studied what support for measuring Vcc is built into the Arduino

    I think a study is not needed, the ADC ref power is VCC and there is a 1.1 V bandgap in the Arduino that you can measure; that's the whole trick.

    Yes, somewhat more funky like a battery management unit can for sure make sense.


  • Contest Winner

    @daulagari I see. Then as the whole topic suggests, it should suffice to use internal functionality to determine battery level if batt level = vcc. But some form of external circuitry is required if vcc is regulated. And I don't see a big reason to put a lot of effort into making a high precision solution for monitoring battery of "our" small nodes. But the current proposal of a simple voltage divider is a bit too wasteful imo (off topic).


  • Hero Member

    @daulagari said:

    As long as you current VCC direct to the battery I do not understand why people opt for using an external voltage divider

    For the voltage divider the ADC reading is 1023 x R2 / (R1 + R2) x Vcc

    For the 1.1 Volt reference the reading is 1023 x 1.1/Vcc

    An external voltage divider is useful only if you are NOT connecting the measured battery directly to VCC (ie: useful only if you are using a regulator of some sort between VBatt and VCC - whether linear, buck or boost).

    Your first calculation doesn't take the reference in to account. The reading is really:
    1023/Vref x R2 / (R1+R2) * VBatt. If VBatt is also VCC and VRef is also VCC, then the ADC reading is based only on the constant resistor ratio, independent of battery power (VBatt and VRef cancel out if both are VCC).

    So when using the default VCC as Vref, the Vbatt voltage divider is only useful when VCC is NOT VBatt.. in that case, if VBatt is the same as VCC, using an external divider is not an alternate technique with wasted components, it's just a non-starter period.


  • Hero Member

    @Zeph: Fully agreed.

    Your first calculation doesn't take the reference in to account. The reading is really:
    1023/Vref x R2 / (R1+R2) * VBatt.

    My formula's were for the case VCC = Vbatt but yes your formula is more generic but reduces to the same when VCC = Vbatt.


  • Code Contributor

    @Yveaux The only comment i have to this lib now then I'm using it is that you should not use floats but int instead, and just have an imaginary decimal point and divide at later stage to save program memory..
    And talking about this, I should probably start a new thread talking about optimizations, There are some to be done in the mysensors-lib also..


  • Mod

    @Damme I'm fully aware of the use of floating point and the penalties that come with it, don't worry.
    But I just poored existing code into a library. I didn't put any effort in optimizing it.
    Btw Arduino sketches tend to be very inefficient on resource usage, starting by using a 16 bit int type...


  • Hero Member

    Thread Rival ... 😉

    So in trying to understand and improve battery life (currently using V div on Step Reg. Vin (VBatt) in my sensors, why can't I just measure the VCC on AO without the V div as VBAT will never exceed VCC,?

    Trying to figure out what fundamental I'm missing here....


  • Mod

    @ServiceXp So you want to connect VCC of the ATMega to ana analog input pin to read the supply level?
    If this is your idea, then the ATMega will measure the voltage on an analog input relative to the supply voltage. If the supply voltage of the ATMega starts to drop, the relative voltage measured on the analog input will not change w.r.t. VCC.
    By using a voltage divider you bring the voltage to be measured within 0,..,1.1V range (roughly). The ATMega has an internal 1.1V voltage reference which will remain stable when VCC drops, and thus can be used to meaure the supply level using a voltage divider.


  • Hero Member

    @Yveaux said:

    @ServiceXp So you want to connect VCC of the ATMega to ana analog input pin to read the supply level?
    If this is your idea, then the ATMega will measure the voltage on an analog input relative to the supply voltage. If the supply voltage of the ATMega starts to drop, the relative voltage measured on the analog input will not change w.r.t. VCC.
    By using a voltage divider you bring the voltage to be measured within 0,..,1.1V range (roughly). The ATMega has an internal 1.1V voltage reference which will remain stable when VCC drops, and thus can be used to meaure the supply level using a voltage divider.

    1. No; vBatt to AO; MCU will be powered by Step Up Reg. vOut.

    2. The MCU supply voltage will never be lower then vBatt. (in the case of 2 AA Batteries). vBatt will always be lower then MCU VCC, In all reality MCU VCC will never change in a significant way, until Step Up Reg drops out.

    3. think it's this v1.1 ref. that may be confusing me, but it just seems like this method should work with out the V div for sensors using 2 AA batteries or any <3.3v power source. 😉


  • Mod

    @ServiceXp OK, I didn't really get your description in the previous post them.
    When VCC is stable due to to the step up converter then it should also work to just reference analog input to vcc. This way your measuring range is 0,..,vcc, which is a lot more then 0,..,1.1v



  • Thread revival.

    I have a sensor powered by 2 AA batteries, does this still stand as a simple way to obtain their status? It was super easy to implement!



  • Would this library also work on a WEMOS D1, which is powered by a esp8266?

    Paai


  • Mod

    @Hans-Paijmans no, it's avr only


  • Mod

    On esp, ESP.getVcc() can be used. More info: https://www.letscontrolit.com/forum/viewtopic.php?t=130

    Edit: use MySensors' hwCPUVoltage() instead. See below for more information.



  • Hi,
    possible to read also internal VCC for SAMD21 and NRF5 platform core?
    MiKa


  • Mod

    @MiKa no, it's avr only


  • Mod

    @MiKa use hwCPUVoltage(), this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.



  • @mfalkvidd said in New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries 😉:

    @MiKa use hwCPUVoltage() this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.

    Thanks ! It works on SAMD21E board 🙂





  • @Yveaux Hi, thanks for your work. Maybe I´m going over something that was covered before, but I need some help, I´m using this code in a door sensor with 2 aa battery, and reporting if battery changes, when the door opens or closes. My problem is that I always get diferent readings from close to open, so I´m always reporting battery level and using more power than needed. Thanks


  • Mod

    @andredts the voltage reference only has limited accuracy, therefore the reported battery voltage might vary slightly. Also the load on the battery can change, causing a variation in battery level reported. Not a lot you can do about that I'm afraid.
    I experimented with sending all decreases in battery level wrt the previous value, and only large increases (eg 10% or more to detect change of batteries). Works quite well.



  • @Yveaux Thanks, that was exactly the insight I was looking, for my door sensor with 2aa battery, your way worked great. For a scene controller with a CR2032 a had to not send also decreases greater then 10%, but hey, 10 steps is more enough.


  • Mod

    @andredts cr2032 is a totally different story. The voltage level fluctuates significantly when sending messages compared to AA powered sensors.
    The 10% is only an example value and used for an increasing voltage level (that normally should only happen when replacing batteries). For a decreasing voltage (regular battery usage) you can just report the level and get much higher resolution. Question is if it will really be useful for cr2032 though...


  • Hardware Contributor

    How about storing last 10 values and send an average every time?


  • Hardware Contributor

    @andredts said in New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries 😉:

    @Yveaux Thanks, that was exactly the insight I was looking, for my door sensor with 2aa battery, your way worked great. For a scene controller with a CR2032 a had to not send also decreases greater then 10%, but hey, 10 steps is more enough.

    For having consistent measurements with a CR2032 you should measure voltage as first action after waking up from sleep.
    I keep the value in a variable and process it after "action" message of the node.

    If possible, run at 1Mhz on internal oscillator so the power consumption of the atmega stays very low (around 1mA instead of 3 at 8MHz/3V).


  • Mod

    I'd like to share my little experience with the 3.2V LiFePO4 AA batteries ad they are a very good solution since a single battery can be used and be directly measured through VCC without requiring any voltage divider or booster



  • @Yveaux I know about that fluctuation, but for my use in a scene controller I would be happy just to know the battery is low. I have it running with a multi-button, were I have click, duble-click and click and hold, I only check battery at one click, because of its fluctuation, but if you use it more than 4 times e a short period that fluctuation is noted, and that is why a added to the 10% increase a 10% decrease gap on battery sent information. Thanks



  • @Nca78 said in New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries 😉:

    @andredts said in New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries 😉:

    @Yveaux Thanks, that was exactly the insight I was looking, for my door sensor with 2aa battery, your way worked great. For a scene controller with a CR2032 a had to not send also decreases greater then 10%, but hey, 10 steps is more enough.

    For having consistent measurements with a CR2032 you should measure voltage as first action after waking up from sleep.
    I keep the value in a variable and process it after "action" message of the node.

    If possible, run at 1Mhz on internal oscillator so the power consumption of the atmega stays very low (around 1mA instead of 3 at 8MHz/3V).

    Hi, I do read the battery first thing after wake up, my problem was when a had various click too close together. It`s good now that I only send battery values that a 10% higher or lower than my last stored value. I will take a look on the 1Mhz bootloader. Thanks


  • Mod

    @andredts you could make an average of the last 3-4 reads before sending the battery value


  • Mod

    is it normal that just swapping a pro mini I am getting a voltage reading difference of 0.12V from same node?


  • Mod

    @gohan the internal reference is specified to be 1.0 to 1.2 V (see "29.5 System and Reset Characteristics" in the datasheet)

    So the voltage will vary between different chips.


  • Mod

    Btw, what is the maximum voltage that you can read with this vcc library?



  • Hi guys, I plan to use a battery powered temperature sensor - 2xAAA batteries i.e. 3V plus a cheap thermistor with a series resistor. I think I shall use the internal 1.1V reference with a resistor divider like 1Meg and 470K to measure the battery level.
    But ... I think I can use two Arduino pins configured as outputs (one output would be HIGH and another would be LOW) to connect the resistor divider instead of connecting the divider directly to VCC and GND - this way the resistor divider would draw current only when the sensor is awake and thus saving power. It would be more simple than using an external transistor to enable the resistor divider.
    Same goes for the temperature measurement, use another pair of pins configured as outputs to connect the thermistor and the series resistor.
    I have tested this idea using the Nodemanager "setPowerPins" function, I can easily measure temperature this way and draw current only when the sensor is awake. After making the measurements all outputs are set LOW (no resistor divider can draw current) then I put sensor to sleep.
    What is your opinion? Is there any "weakness" in this idea?


  • Mod

    @iahim67 I'm missing what your request has to do with the vcc library



  • @gohan sorry, my bad, nothing to do with vcc library ... just realized that vcc lib allows me to measure vcc without resistor divider:-)


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 2
  • 5
  • 3
  • 3

14
Online

11.4k
Users

11.1k
Topics

112.7k
Posts