Mkr1010 not charging although charging light is also on

Hi, i am using a MKR1010 and I am experiencing trouble getting a 1420mA LiPo battery to charge. I want the arduino to be running on the battery and then when i connect the USB it detects that it has external power and switches to the charging mode without actually having to reset the arduino after connecting the USB.

I reviewed another similar topic: MKR1010 is not charging the battery - #2 by jojo. and the solution that also worked for me was to reset the arduino. I would like to know if there was a way to write a function that detects the constantly tries to detect the presence of a USB available for charging such that i dont have to reset the arduino everytime i would like it to charge.

Study materials used

I am aware that the BQ24195 has the D+ and the D- pins (2,3) which the datasheet says can be used for usb detection and I have written the following code as an addition to the BQ24195.cpp in an attempt to do so but doesnt seem to be working.

Below is also the most impoertant section i believe of the instructions to detect the different USB states as defined by Texas instruments for the usb detection:

The bq24195L, bq24195 contains a D+/D– based input source detection to program the input current limit. The
D+/D- detection has two steps: data contact detect (DCD) followed by primary detection.
DCD (Data Contact Detection) uses a current source to detect when the D+/D– pins have made contact during
an attach event. The protocol for data contact detect is as follows:
• Detect VBUS present and REG08[2] = 1 (power good)
• Turn on D+ IDP_SRC and the D– pull-down resistor RDM_DWN for 40 ms
• If the USB connector is properly attached, the D+ line goes from HIGH to LOW, wait up to 0.5 sec.
• Turn off IDP_SRC and disconnect RDM_DWN
The primary detection is used to distinguish between USB host (Standard Down Stream Port, or SDP) and
different type of charging ports (Charging Down Stream Port, or CDP, and Dedicated Charging Port, or DCP).
The protocol for primary detection is as follows:
• Turn on VDP_SRC on D+ and IDM_SINK on D– for 40 ms
• If PD is attached to a USB host (SDP), the D– is low. If PD is attached to a charging port (CDP or DCP), the
D– is high.

My code so far

int PMICClass::usbDetection() {
    // Step 1: Data Contact Detection (DCD)
    digitalWrite(usbDPlusPin, HIGH); // Turn on D+ IDP_SRC
    digitalWrite(usbDMinusPin, LOW); // Turn on D- RDM_DWN

    delay(40); // Wait for D+/D- detection

    bool dPlusState = digitalRead(usbDPlusPin); // Read D+ state

    digitalWrite(usbDPlusPin, LOW); // Turn off D+ IDP_SRC
    digitalWrite(usbDMinusPin, HIGH); // Disconnect RDM_DWN

    if (dPlusState == LOW) {
        // D+ transitioned from HIGH to LOW indicating DCD
        return 1; // DCD detected
    }

    // D+ did not transition as expected (no DCD)
    delay(500); // Wait up to 0.5 sec for D- state change

    // Step 2: Primary Detection
    digitalWrite(usbDPlusPin, HIGH); // Turn on VDP_SRC
    digitalWrite(usbDMinusPin, HIGH); // Turn on IDM_SINK

    delay(40); // Wait for primary detection

    bool dMinusState = digitalRead(usbDMinusPin); // Read D- state

    digitalWrite(usbDPlusPin, LOW); // Turn off VDP_SRC
    digitalWrite(usbDMinusPin, LOW); // Turn off IDM_SINK

    // Logic for USB detection based on D- state
    if (dMinusState == LOW) {
        return 2; // CDP detected
    } else {
        return 0; // No detection
    }
}

Any help is much appriciated. Thanks in advance :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.