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

    @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

    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..

    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):

    and see the heading "Changing reference voltage" here:

Log in to reply





Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.