Hello, I have a problem with arduino mkr wifi 1010. I have arduino connected with lipo battery and I want to know when the board is connected with USB. So I connected 5V pin directly to A1 pin and reading A1 analog value I'm able to detect USB connection. But when battery goes down and I connect for battery recharge arduino doesn't start until I cut wire between 5V and A1. Did someone have same problem? any idea on how to solve it?
thank you in advance
Hi andreaboschetti1971,
It's best not to connect your 5V pin directly to A1, as the SAMD21 microcontroller's inputs are only 3.3V tolerant. Any voltage above 3.3V (or thereabouts) risks damaging the device.
On the MKR Wifi 1010 board, the SAMD21 microcontroller is able to monitor the battery on its ADC_BATTERY pin. The battery voltage is reduced from 4.2V maximum to 3.3V by a 330K (R1) and 1.2M (R2) resistor voltage divider, as shown on the MKR Wifi 1010 schematic diagram: https://content.arduino.cc/assets/MKRWiFi1010V2.0_sch.pdf.
Here's the circuit for a resistor voltage divider:

To read the voltage is necessary to reconstruct and reverse the voltage divider in software:
// Monitor the battery level on the MKR Wifi 1010
const float REFERENCE_VOLTS = 3.3f; // +3.3V regulated supply voltage
const float R1 = 330000.0f; // Resistor values of voltage divider
const float R2 = 1200000.0f;
const float RESISTOR_FACTOR = 4095.0f * (R2 / (R1 + R2)); // Using 12-bit resolution: multiply by 4095
void setup() {
SerialUSB.begin(115200); // Initialise the native USB port
while(!SerialUSB); // Wait for the console to open
analogReadResolution(12); // Set the analog resolution to 12-bits
int batteryLevel = analogRead(ADC_BATTERY); // Read the battery level from the ADC
float volts = (batteryLevel / RESISTOR_FACTOR) * REFERENCE_VOLTS; // Calculate the battery voltage
SerialUSB.print(F("Battery Level: ")); // Display the results
SerialUSB.print(volts);
SerialUSB.println(F("V"));
}
void loop() {}
Measuring the 5V pin on A1 also requires the same principle. The 5V pin needs to first pass through a resistor voltage divider, to bring the voltage down to below 3.3V at the microcontroller's A1 input. For example just two 100K resistors for R1 and R2 would do and provide some margin for error.