Friday, March 17, 2017

Battery voltage check function on Arduino Feather 32u4

Hardware: Adafruit Feather 32u4 Radio (RFM69HCW)


The folks over at Adafruit have done a nice job with some aspects of documentation on these boards.  This is one of those items that they've documented well, but I find just annoying that they don't drop it in as a simple "best practice" kind of thing.

So here it is!

This is a function you can drop into your code anywhere and get results on the battery voltage on any of the Feather 32u4 devices.

Copy/paste this function directly into your application:

/*
 * This routine checks the voltage on the battery (if it exists) and returns that value.  Should be in the 3.2-4.2 range depending upon the battery used
 */
float checkBattery(){
  //This returns the current voltage of the battery on a Feather 32u4.
  float measuredvbat = analogRead(9);
  measuredvbat *= 2;    // we divided by 2, so multiply back
  measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
  measuredvbat /= 1024; // convert to voltage

  
  return measuredvbat;
}

You can then call this from your routine similar to as follows:

void loop () {
  float batVoltage = checkBattery();  //get battery voltage
...
  serial.println(batVoltage);  //write battery voltage to serial/usb port
}

Note that the voltage will always show at 4.2v if you don't plug in a battery.

No comments:

Post a Comment