# \[I want a node\] Supply read node for low battery indicator

**URL:** <https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143>\
**Category:** Hardware Interfacing Questions\
**Created:** [December 27, 2019, 10:39am UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143 "2019-12-27T10:39:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [December 27, 2019, 10:39am UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/1 "2019-12-27T10:39:17Z")

</div>

## Supply Low

Reads the boards voltage supply and using a settable threshold below which the condition is true

### Inputs

- `THRES` (number, default=?) — The voltage threshold below which this node outputs true.
- `UPD` (pulse, default=loop) — When the voltage read is updated; never, loop, boot

### Outputs

- `LOW` (boolean, default=0) — True (1) is voltage below THRES node input number.
- `VOLT` (number, default=?) — The supply voltage.

* * *

The use case here is because of an automatic chicken door which is left to do it’s thing day after day, running on a battery.

This node would be able to light an LED when the voltage gets to a settable low threshold, so I know it’ll be time to change the battery soon before the board stops functioning.

Understandably, without having to use external components to do this, the voltage read is not super accurate with the 1.1v variance -+ 0.1v. But certainly good enough for many use cases to indicate low battery.

I do not know how to implement this into a node! I’m not a coder, but I always try to work it out when I need too, so please help. From several sources I’ve got this code below.

Thanks for your help.

```auto
long readVcc() {
  long result; // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

```

[https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/](https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/)  
and  
[https://github.com/sweebee/Arduino-home-automation/blob/master/libraries/readVcc/readVcc.h](https://github.com/sweebee/Arduino-home-automation/blob/master/libraries/readVcc/readVcc.h)

This node is a good compliment to the other node I asked for here: [[I want a node] Sleep and Wake for battery longevity](https://forum.xod.io/t/sleep-and-wake-for-battery-longevity/2825/11)

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [December 27, 2019, 12:51pm UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/2 "2019-12-27T12:51:58Z")

</div>

hi, some time ago I made two schmitt-trigger nodes (analog and digital) if you are interested in _cesars/utils_ library

> [@Schmitt trigger](https://forum.xod.io/t/schmitt-trigger/1357):
>
> I found several patches with hysteresis, I took some ideas and I made a node in C ++ … I’m still experimenting. Possibilities of joining nodes or making a new one, make XOD very interesting [SchmittTrigger.xodball](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/07081a72160bdaaae28918d96d8687250b0a0ec8.xodball) (5.8 KB)

---

<div class="post-metadata">

**Author:** ![cdbradwell](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cdbradwell/32/1606_2.png) [@cdbradwell](https://forum.xod.io/u/cdbradwell)\
**Post date:** [December 28, 2019, 10:10pm UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/3 "2019-12-28T22:10:29Z")

</div>

Thanks @cesars

I’ve tried to implement the code into a simple number out, but it doesn’t work, what is wrong with this? How do I write the result number to the output? Should the first section #endif be contained in a setup?

```auto

struct State {
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {

  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
  #if defined( __AVR_ATmega32U4__ ) || defined( __AVR_ATmega1280__ ) || defined( __AVR_ATmega2560__ )
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined ( __AVR_ATtiny24__ ) || defined( __AVR_ATtiny44__ ) || defined( __AVR_ATtiny84__ )
    ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined ( __AVR_ATtiny25__ ) || defined( __AVR_ATtiny45__ ) || defined( __AVR_ATtiny85__ )
    ADMUX = _BV(MUX3) | _BV(MUX2);
  #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif  

  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA,ADSC)); // measuring

  uint8_t low = ADCL; // must read ADCL first - it then locks ADCH  
  uint8_t high = ADCH; // unlocks both

  long result = (high<<8) | low;

  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  return result; // Vcc in millivolts
    
  emitValue<output_OUT>(ctx, result);
}

```

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [December 29, 2019, 4:26am UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/6 "2019-12-29T04:26:22Z")

</div>

I have deleted the previous messages not to confuse

also update the cesars/utils library, maybe this will help

![EnchantingAjarAlaskanhusky-size_restricted1](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/b/b1cba583198f927b48d13d3e448d1553625e6171.gif)

---

<div class="post-metadata">

**Author:** ![mogplus8](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/mogplus8/32/352_2.png) [@mogplus8](https://forum.xod.io/u/mogplus8)\
**Post date:** [January 1, 2020, 1:55pm UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/7 "2020-01-01T13:55:31Z")

</div>

Do you really want to use a microprocessor for this? I’m no electronics guru but it seems to me a hardware solution might be better. Some good examples here. [https://www.eleccircuit.com/simple-led-display-warn-battery-low/](https://www.eleccircuit.com/simple-led-display-warn-battery-low/)  
How about a rechargeable battery and solar cell? Never change the battery again.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/eb7348baa04192c2ef932d1db54d44f09f8a7888.png) [@system](https://forum.xod.io/u/system)\
**Post date:** [April 21, 2020, 3:46pm UTC](https://forum.xod.io/t/i-want-a-node-supply-read-node-for-low-battery-indicator/3143/8 "2020-04-21T15:46:01Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
