Go Down

Topic: MKRFOX1200 read battery voltage (Read 283 times) previous topic - next topic

astroman35

Hi guys,

I saw on this page that it's possible to read battery voltage on the MKRZERO board with few lines of code :
Code: [Select]
  /*
  ReadBatteryVoltage
  Reads the analog input connected to the battery output on a MKRZero or MKR1000, converts it to voltage, and prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(ADC_BATTERY);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 4.3V):
  float voltage = sensorValue * (4.3 / 1023.0);
  // print out the value you read:
  Serial.print(voltage);
  Serial.println("V");
}


I tried to put this code in the MKRFOX1200 but the value returned are weird.
I've modified the formula by replacing 4.3 with 3.3 but same problem.

Do you know if it's possible to read battery voltage on this board ?

ManfredDoetsch

This one works:



// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5 / 1023.0);
  // print out the value you read:
  Serial.print(voltage);
}


Did you check the constant ADC_BATTERY ?

astroman35

Thanks for your answer ManfredDoetsch, i will check it in my next code upload.

astroman35

Hi,

I've uploaded ManfredDoetsch code in my board but it doesn't seem to work as expected.
Here is a historical view of the voltage value since may 25 :

Do you have any idea ?

fabricecarrique


Have you advance on the function to read the battery life, I have a similar project.

Go Up