SecretVoltmeter in SparkCore [Closed]

Hi,

I’m trying to understand how to implement this feature, which is available in Arduino 168 and 328. It is possible to measure it own input voltage through it internal Vref, and i notice that the STM32 Spark Core have available a similar internal reference. ADC_CR2_TSVREFE ((uint32_t)0x00800000). Any help to migrate this code to Spark?

Arduino code version:
https://code.google.com/p/tinkerit/wiki/SecretVoltmeter

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}```

That is not possible on the Spark core without an external voltage reference due to the differences in how the ADCs work.

There are lots of current and battery monitoring ICs these days that might be a better fit for your project. Here is one:

Hi @bko, thanks for your explanation, and sorry for the re-post subject. I’m trying to free some analog pins, because i’m using all available analog pin, i known that your solution was through I2C but i’m avoiding add more hardware into my project for dimension issues, so i ask if is possible to configure in the firmware any digital pins (D0-D7) to use as analog, to increase the analog pins for a simple analogRead().