/* | |
* AJ Alves (aj.alves@zerokol.com) | |
*/ | |
#define BATERRY_LEVEL_IN A0 // Arduino analogic pin | |
#define ANALOGIC_RESOLUTION 1024 // The analogic board resolution, for example, Arduino Uno is 10 bit (from 0 to 1023) | |
#define REFERENCE_VOLTAGE 5.0 // The reference voltage, for example, Arduino Uno works in 5 V reference voltage by default | |
#define R1 3000000.0 // resistance of R1 (3 MR) | |
#define R2 1000000.0 // resistance of R2 (1 MR) | |
#define EXPECTED_V_OUT 12 // The intented voltage, 12 V | |
void setup() { | |
Serial.begin(9600); // Setup Serial Port | |
} | |
void loop() { | |
int value = analogRead(BATERRY_LEVEL_IN); // Read value at analog input | |
float vOut = (value * REFERENCE_VOLTAGE) / ANALOGIC_RESOLUTION; // Calculate output voltage with rule of three formula | |
float vIn = vOut / (R2 / (R1 + R2)); // Calculate input voltage with voltage divider formula | |
int percent = vIn * 100 / EXPECTED_V_OUT; // Calculating baterry level percentage with rule of three formula | |
Serial.print("Baterry level is: "); | |
Serial.print(vIn); | |
Serial.print("V ("); | |
Serial.print(percent); | |
Serial.println("%);\n"); | |
delay(3000); // Wait 3 seconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment