<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Battery voltage calculation for 18650 batteries]]></title><description><![CDATA[<p dir="auto">I have built a Dallas Temperature sensor around a Pro Mini 8Mhz.</p>
<p dir="auto">I am powering it with two li-ion 18650 cells connected in series (~8.4V maximum voltage when fully charged, 5.4V minimum voltage before cutoff) and powering the Pro Mini via the RAW port.</p>
<p dir="auto">I would like to send the current battery voltage info to the controller. I connected the wiring according to the "Battery Powering" page (1Mohm + 470kohm resistors, the resistor join point also connected to A0). The +V is the RAW port of the arduino (where the battery + is connected)</p>
<p dir="auto">The sketch works but seems to report wrong voltages.</p>
<p dir="auto">When there is no actual voltage (battery disconnected and arduino powered by the serial programmer), it reports 0.17V.<br />
When the battery is connected, it should report ~8V (SOC on the li-ion pack) but it reports 3.44V.</p>
<p dir="auto">Maybe the example sketch was specifically created for two AA batteries in series?</p>
<p dir="auto">My node sketch:</p>
<pre><code>// Enable debug prints to serial monitor
#define MY_DEBUG 

#define MY_NODE_ID 78

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#include &lt;SPI.h&gt;
#include &lt;MySensors.h&gt;  
#include &lt;DallasTemperature.h&gt;
#include &lt;OneWire.h&gt;

// Define sensor node childs
#define CHILD_ID_BATT 1
#define CHILD_ID_TEMP 0


#define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No

#define ONE_WIRE_BUS 7 // Pin where dallase sensor is connected 


#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 1000; // Sleep time between reads (in milliseconds)

// Battery related init
int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
float oldBatteryV = 0;
MyMessage msgBatt(CHILD_ID_BATT, V_VOLTAGE);


// Dallas Temperature related init
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&amp;oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
bool receivedConfig = false;
bool metric = true;
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);



void before()
{
  // Startup up the OneWire library
  sensors.begin();
}

void setup()  
{ 
  // requestTemperatures() will not block current thread
  sensors.setWaitForConversion(false);


  // needed for battery soc
  // use the 1.1 V internal reference
  #if defined(__AVR_ATmega2560__)
      analogReference(INTERNAL1V1);
  #else
      analogReference(INTERNAL);
  #endif

}

void presentation() {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Temperature Sensor", "1.1");

  // Fetch the number of attached temperature sensors  
  numSensors = sensors.getDeviceCount();

  // Present all sensors to controller
  for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {   
     present(i, S_TEMP);
  }
}

void loop()     
{     

  // get the battery Voltage
  int battSensorValue = analogRead(BATTERY_SENSE_PIN);
  Serial.println(battSensorValue);
  float batteryV  = battSensorValue * 0.003363075;
  Serial.print("Battery Voltage: ");
  Serial.print(batteryV);
  Serial.println(" V");
//  if (oldBatteryV != batteryV) {
    // Power up radio after sleep
    send(msgBatt.set(batteryV, 2));
    oldBatteryV = batteryV;
//  }  

  // Fetch temperatures from Dallas sensors
  sensors.requestTemperatures();

  // query conversion time and sleep until conversion completed
  int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  sleep(conversionTime);

  // Read temperatures and send them to controller 
  for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {

    // Fetch and round temperature to one decimal
    float temperature = static_cast&lt;float&gt;(static_cast&lt;int&gt;((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;

    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature &amp;&amp; temperature != -127.00 &amp;&amp; temperature != 85.00) {
    #else
    if (temperature != -127.00 &amp;&amp; temperature != 85.00) {
    #endif

      // Send in the new temperature
      send(msgTemp.setSensor(i).set(temperature,1));
      // Save new temperatures for next compare
      lastTemperature[i]=temperature;
    }
  }
  sleep(SLEEP_TIME);
}
</code></pre>
<p dir="auto">Any help is appreciated.</p>
]]></description><link>https://forum.mysensors.org/topic/7242/battery-voltage-calculation-for-18650-batteries</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Jul 2026 19:11:28 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/7242.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 27 Jul 2017 20:56:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Battery voltage calculation for 18650 batteries on Thu, 27 Jul 2017 21:22:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sola" aria-label="Profile: sola">@<bdi>sola</bdi></a> yes, you are correct that the calculation is form2xAA. See the comment inside loop() on the example at <a href="https://www.mysensors.org/build/battery" rel="nofollow ugc">https://www.mysensors.org/build/battery</a> for the required calculation, and this part for the voltage divider:</p>
<blockquote>
<p dir="auto">The ADC is set to use the internal reference value of 1.1V - so Vmax at ADCmax = 1.1*(16+4703)/4703 = 3.44V</p>
</blockquote>
<p dir="auto">You'll need to change the resistors in the voltage divider to handle voltages as high as 8V.</p>
]]></description><link>https://forum.mysensors.org/post/72309</link><guid isPermaLink="true">https://forum.mysensors.org/post/72309</guid><dc:creator><![CDATA[mfalkvidd]]></dc:creator><pubDate>Thu, 27 Jul 2017 21:22:25 GMT</pubDate></item></channel></rss>