DaelonSuzuka / CPUVolt

Library for Arduino and ATmega series processors to read the processor's Vcc voltage and percentage of capacity (for battery based projects) using *absolutely* no external components or connections!

Home Page:https://github.com/ripred/CPUVolt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CPU Voltage

Measure the voltage of the processor or retrieve the percent of charge for battery based systems.

Measure the voltage applied to Vcc on the Microchip (Atmel) ATMega processors using nothing more than the internal registers. NO external parts or connections necessary!

see this Microchip application note to learn more about the internal features used and supported to implement this feature

Update: Now includes support for reading the voltage as a percentage of total capacity. You can also optionally supply the voltage level that is considered to be 0% and the voltage level considered to be 100%. This is really useful for battery-based projects! You can now specify the lower voltage level you would like to use to indicate when the system needs recharging and the level at which it is fully charged!

Example use:

/*
 * CPUVolt.ino
 * 
 * Example Arduino sketch showing the use of the CPUVolt library.
 * Updated to show the use of the new readPercent(...) methods.
 * 
 */
#include <CPUVolt.h>

void setup() {
    Serial.begin(115200);

    /*
     * Read the current voltage level and convert it
     * to an easier to read floating point value
     */
    float mv = readVcc() / 1000.0;

    // Show the voltage with 2 decimal places
    Serial.print("Voltage: ");
    Serial.println(mv, 2);

    /*
     * Get the voltage level as a percentage of total charge.
     * You can optionally specify the voltage level to be considered
     * as 100%. The default voltage is 5V if not supplied.
     */
    float pct = readPercent(/*5000*/);
    Serial.print("Percent: ");
    Serial.println(pct, 2);

    /*
     * You can also specify both the lower and upper voltage
     * ranges to be considered what is 0% and what is 100%.
     * This is really useful for battery powered projects!
     */
    pct = readPercent(2900, 4700);
    Serial.print("Percent: ");
    Serial.println(pct, 2);
}

void loop() { }

Output:

Voltage: 4.67
Percent: 93.38
Percent: 98.28

About

Library for Arduino and ATmega series processors to read the processor's Vcc voltage and percentage of capacity (for battery based projects) using *absolutely* no external components or connections!

https://github.com/ripred/CPUVolt

License:MIT License


Languages

Language:C++ 77.9%Language:C 22.1%