### Implement Complete Battery Charging Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Demonstrates full initialization, parameter configuration, and status monitoring for a 750mAh LiPo battery. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); // Initialize PMIC if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Configure input source PMIC.setInputCurrentLimit(2.0); // 2A input limit PMIC.setInputVoltageLimit(3.88); // 3.88V minimum input // Configure system voltage PMIC.setMinimumSystemVoltage(3.5); // 3.5V minimum for MCU // Configure charging parameters for 750mAh battery PMIC.setChargeVoltage(4.2); // 4.2V target voltage PMIC.setChargeCurrent(0.375); // 375mA (C/2 rate) PMIC.setPreChargeCurrent(0.128); // 128mA pre-charge PMIC.setTermChargeCurrent(0.128); // 128mA termination Serial.println("PMIC configured for 750mAh LiPo battery"); Serial.println("Starting charge cycle..."); } void loop() { // Enable charging if (!PMIC.enableCharge()) { Serial.println("Error enabling charge"); delay(5000); return; } // Check for faults int fault = PMIC.getChargeFault(); if (fault != NO_CHARGE_FAULT) { Serial.print("Charge fault: "); Serial.println(fault, HEX); return; } // Monitor charge status int status = PMIC.chargeStatus(); switch (status) { case NOT_CHARGING: Serial.println("Not charging - check battery connection"); break; case PRE_CHARGING: Serial.println("Pre-charging..."); break; case FAST_CHARGING: Serial.println("Fast charging..."); break; case CHARGE_TERMINATION_DONE: Serial.println("Charge complete!"); PMIC.disableCharge(); while (1); // Done } delay(5000); } ``` -------------------------------- ### Enable Boost Mode Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Enables OTG boost mode to generate 5V from the battery. ```APIDOC ## PMIC.enableBoostMode() ### Description Enables OTG boost mode, generating 5V from the battery to power USB devices. ### Response - **bool** - Returns true if successful, false otherwise. ``` -------------------------------- ### Enable OTG Boost Mode Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Enables 5V output on the USB connector to power external devices, allowing the board to function as a USB host. ```cpp #include int usb_mode = UNKNOWN_MODE; void setup() { // Use Serial1 as USB port is busy with guest device Serial1.begin(9600); if (!PMIC.begin()) { Serial1.println("Failed to initialize PMIC!"); while (1); } // Enable boost mode for USB host functionality if (!PMIC.enableBoostMode()) { Serial1.println("Error enabling Boost Mode"); } else { Serial1.println("Boost mode enabled - 5V available on USB connector"); } } void loop() { int actual_mode = PMIC.USBmode(); if (actual_mode != usb_mode) { usb_mode = actual_mode; if (actual_mode == BOOST_MODE) { Serial1.println("Boost mode status confirmed"); // 5V now appears on USB connector for guest devices } } delay(100); } ``` -------------------------------- ### Initialize PMIC Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Initializes I2C communication with the PMIC and verifies the chip. Must be called before other functions. Deinitializes with `end()`. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); // Initialize the PMIC if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); // Halt on failure } Serial.println("PMIC initialized successfully"); } void cleanup() { // Deinitialize when done (optional) PMIC.end(); } ``` -------------------------------- ### Configure Fast Charge Timer Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the safety timer duration for the fast charge phase and verifies the setting. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set fast charge safety timer to 8 hours if (!PMIC.setFastChargeTimerSetting(8.0)) { Serial.println("Error setting fast charge timer"); } // Read back the timer setting float timerSetting = PMIC.getFastChargeTimerSetting(); if (!isnan(timerSetting)) { Serial.print("Fast charge timer: "); Serial.print(timerSetting); Serial.println(" hours"); // Output: Fast charge timer: 8.0 hours } } ``` -------------------------------- ### Check USB Mode Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Determines and prints the current USB power source mode. Requires PMIC initialization. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } } void loop() { int mode = PMIC.USBmode(); switch (mode) { case UNKNOWN_MODE: Serial.println("USB Mode: Unknown"); break; case USB_HOST_MODE: Serial.println("USB Mode: USB Host (SDP/CDP port)"); break; case ADAPTER_PORT_MODE: Serial.println("USB Mode: Adapter Port (DCP)"); break; case BOOST_MODE: Serial.println("USB Mode: Boost Mode (OTG)"); break; } delay(2000); } ``` -------------------------------- ### Check Charge Status Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Monitors and prints the current charging state of the device. Ensure PMIC is initialized and charging is enabled before use. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } PMIC.enableCharge(); } void loop() { int status = PMIC.chargeStatus(); switch (status) { case NOT_CHARGING: Serial.println("Status: Not charging"); break; case PRE_CHARGING: Serial.println("Status: Pre-charging (battery very low)"); break; case FAST_CHARGING: Serial.println("Status: Fast charging"); break; case CHARGE_TERMINATION_DONE: Serial.println("Status: Charging complete"); break; } delay(2000); } ``` -------------------------------- ### Set Input Current Limit Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the maximum input current drawn from the power source. Valid values are discrete steps (0.1A to 3.0A). Use `getInputCurrentLimit()` to read the current setting. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set input current limit to 2.0 A if (!PMIC.setInputCurrentLimit(2.0)) { Serial.println("Error setting input current limit"); } // Read back the current limit float currentLimit = PMIC.getInputCurrentLimit(); if (!isnan(currentLimit)) { Serial.print("Input current limit: "); Serial.print(currentLimit); Serial.println(" A"); // Output: Input current limit: 2.0 A } } ``` -------------------------------- ### Enable Battery Charging Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Activates battery charging mode, enabling charge termination and fault interrupts. Use `disableCharge()` to stop. Monitors charge status in `loop()`. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Enable battery charging if (!PMIC.enableCharge()) { Serial.println("Error enabling Charge mode"); } else { Serial.println("Charge mode enabled"); } } void loop() { // Monitor charging and disable when complete if (PMIC.chargeStatus() == CHARGE_TERMINATION_DONE) { if (!PMIC.disableCharge()) { Serial.println("Error disabling Charge mode"); } else { Serial.println("Charging complete, charge disabled"); } while (1); // Stop loop } delay(1000); } ``` -------------------------------- ### Detect USB Power Source (DPDM) Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Enables D+/D- detection to identify the connected USB power source type. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Enable DPDM detection for USB source identification if (!PMIC.enableDPDM()) { Serial.println("Error enabling DPDM detection"); } else { Serial.println("DPDM detection enabled"); } } void loop() { // After enabling DPDM, check USB mode int mode = PMIC.USBmode(); if (mode == USB_HOST_MODE) { Serial.println("Connected to USB host port"); } else if (mode == ADAPTER_PORT_MODE) { Serial.println("Connected to dedicated charging port"); } delay(2000); } ``` -------------------------------- ### Set Minimum System Voltage Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the minimum voltage required to power the MCU and modules. Valid range is 3.0V to 3.7V. Use `getMinimumSystemVoltage()` to read the current setting. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set minimum system voltage to 3.5 V if (!PMIC.setMinimumSystemVoltage(3.5)) { Serial.println("Error setting minimum system voltage"); } // Read back the setting float minVoltage = PMIC.getMinimumSystemVoltage(); if (!isnan(minVoltage)) { Serial.print("Minimum system voltage: "); Serial.print(minVoltage); Serial.println(" V"); // Output: Minimum system voltage: 3.5 V } } ``` -------------------------------- ### Power and Battery Status Checks Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Performs various checks on power input, battery connection, battery level, and thermal status. Ensure PMIC is initialized. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } } void loop() { // Check power input status if (PMIC.isPowerGood()) { Serial.println("Power Good: Valid input detected"); } else { Serial.println("Power Good: No valid input"); } // Check battery connection if (PMIC.isBattConnected()) { Serial.println("Battery: Connected"); } else { Serial.println("Battery: Not connected"); } // Check if battery can power the system if (PMIC.canRunOnBattery()) { Serial.println("Battery Level: Sufficient for operation"); } else { Serial.println("Battery Level: Below minimum system voltage"); } // Check thermal status if (PMIC.isHot()) { Serial.println("Thermal: In thermal regulation mode"); } else { Serial.println("Thermal: Normal temperature"); } Serial.println("---"); delay(3000); } ``` -------------------------------- ### Fault Detection Checks Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Detects and reports various faults including charge faults, battery over-voltage, temperature issues, and watchdog timer status. Requires PMIC initialization. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } } void loop() { // Check charge faults int chargeFault = PMIC.getChargeFault(); switch (chargeFault) { case NO_CHARGE_FAULT: Serial.println("Charge Fault: None"); break; case INPUT_OVER_VOLTAGE: Serial.println("Charge Fault: Input over voltage (>18V)"); break; case THERMAL_SHUTDOWN: Serial.println("Charge Fault: Thermal shutdown"); break; case CHARGE_SAFETY_TIME_EXPIRED: Serial.println("Charge Fault: Safety timer expired"); break; } // Check battery over-voltage if (PMIC.isBatteryInOverVoltage()) { Serial.println("ALERT: Battery over-voltage detected!"); } // Check temperature fault (NTC based) int tempFault = PMIC.hasBatteryTemperatureFault(); switch (tempFault) { case NO_TEMPERATURE_FAULT: Serial.println("Temperature: Normal"); break; case LOWER_THRESHOLD_TEMPERATURE_FAULT: Serial.println("Temperature: Too cold for charging"); break; case HIGHER_THRESHOLD_TEMPERATURE_FAULT: Serial.println("Temperature: Too hot for charging"); break; } // Check watchdog status if (PMIC.isWatchdogExpired()) { Serial.println("Watchdog: Timer expired - reset required"); PMIC.resetWatchdog(); } Serial.println("---"); delay(2000); } ``` -------------------------------- ### Set Input Voltage Limit Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the minimum input voltage before the PMIC reduces input current. Valid range is 3.88V to 5.08V. Use `getInputVoltageLimit()` to read the current setting. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set input voltage limit to 3.88 V (minimum) if (!PMIC.setInputVoltageLimit(3.88)) { Serial.println("Error setting input voltage limit"); } // Read back the voltage limit float voltageLimit = PMIC.getInputVoltageLimit(); if (!isnan(voltageLimit)) { Serial.print("Input voltage limit: "); Serial.print(voltageLimit); Serial.println(" V"); // Output: Input voltage limit: 3.88 V } } ``` -------------------------------- ### Configure Fault Interrupts Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets up hardware interrupts for charge and battery faults, specifically for MKR GSM 1400 and MKR NB 1500 boards. ```cpp #include volatile bool faultOccurred = false; void faultISR() { faultOccurred = true; } void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } #if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) // Attach interrupt handler for fault detection attachInterrupt(digitalPinToInterrupt(PMIC_IRQ_PIN), faultISR, FALLING); #endif // Enable fault interrupts if (!PMIC.enableChargeFaultINT()) { Serial.println("Error enabling charge fault interrupt"); } if (!PMIC.enableBatFaultINT()) { Serial.println("Error enabling battery fault interrupt"); } Serial.println("Fault interrupts enabled"); } void loop() { if (faultOccurred) { faultOccurred = false; // Check what fault occurred int chargeFault = PMIC.getChargeFault(); if (chargeFault != NO_CHARGE_FAULT) { Serial.println("Charge fault detected!"); } if (PMIC.isBatteryInOverVoltage()) { Serial.println("Battery over-voltage fault!"); } } delay(100); } ``` -------------------------------- ### Set Pre-Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Configures the pre-charge current for low battery voltage states, ranging from 0.128A to 2.048A. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set pre-charge current to 256 mA if (!PMIC.setPreChargeCurrent(0.256)) { Serial.println("Error setting pre-charge current"); } // Read back the pre-charge current float preChargeCurrent = PMIC.getPreChargeCurrent(); if (!isnan(preChargeCurrent)) { Serial.print("Pre-charge current: "); Serial.print(preChargeCurrent * 1000); Serial.println(" mA"); // Output: Pre-charge current: 256 mA } } ``` -------------------------------- ### Manage Watchdog Timer Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Use to disable the I2C watchdog or reset it periodically to prevent the PMIC from reverting to default settings. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Option 1: Disable watchdog entirely if (!PMIC.disableWatchdog()) { Serial.println("Error disabling watchdog"); } else { Serial.println("Watchdog disabled"); } } void loop() { // Option 2: If watchdog is enabled, reset it periodically // to prevent PMIC from reverting to default settings if (!PMIC.resetWatchdog()) { Serial.println("Error resetting watchdog"); } // Your application code here delay(30000); // Reset watchdog every 30 seconds } ``` -------------------------------- ### Set Pre-Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the pre-charge current used when battery voltage is below the threshold. ```APIDOC ## PMIC.setPreChargeCurrent(current) ### Description Sets the pre-charge current. Valid range is 0.128A to 2.048A. ### Parameters - **current** (float) - Required - Pre-charge current in Amperes. ### Response - **bool** - Returns true if successful, false otherwise. ``` -------------------------------- ### Set Battery Charge Voltage Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Configures the target battery charge voltage between 3.504V and 4.512V. Use 4.2V for standard LiPo batteries. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set charge voltage to 4.2 V (standard LiPo) if (!PMIC.setChargeVoltage(4.2)) { Serial.println("Error setting charge voltage"); } // Read back the charge voltage float chargeVoltage = PMIC.getChargeVoltage(); if (!isnan(chargeVoltage)) { Serial.print("Charge voltage: "); Serial.print(chargeVoltage); Serial.println(" V"); // Output: Charge voltage: 4.2 V } } ``` -------------------------------- ### Control Buck Regulator Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Enables or disables the internal buck regulator for efficient voltage step-down. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Enable buck regulator (default) if (!PMIC.enableBuck()) { Serial.println("Error enabling buck regulator"); } else { Serial.println("Buck regulator enabled"); } } void loop() { // Disable buck if needed for power saving // Note: This may affect system operation // PMIC.disableBuck(); delay(1000); } ``` -------------------------------- ### Set Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the fast charge current for the battery. ```APIDOC ## PMIC.setChargeCurrent(current) ### Description Sets the fast charge current. Valid range is 0.512A to 4.544A. ### Parameters - **current** (float) - Required - Charge current in Amperes. ### Response - **bool** - Returns true if successful, false otherwise. ``` -------------------------------- ### Set Charge Voltage Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the target battery charge voltage for the BQ24195 PMIC. ```APIDOC ## PMIC.setChargeVoltage(voltage) ### Description Sets the target battery charge voltage. Valid range is 3.504V to 4.512V. ### Parameters - **voltage** (float) - Required - Target voltage in Volts (e.g., 4.2 for standard LiPo). ### Response - **bool** - Returns true if successful, false otherwise. ``` -------------------------------- ### Control Battery Connection (BATFET) Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Connects or disconnects the battery from the system. Disabling is useful for shipping mode or storage. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Ensure BATFET is enabled for normal operation if (!PMIC.enableBATFET()) { Serial.println("Error enabling BATFET"); } else { Serial.println("BATFET enabled - battery connected to system"); } } void enterShippingMode() { // Disconnect battery for shipping/storage // WARNING: System will lose power if no external supply if (!PMIC.disableBATFET()) { Serial.println("Error disabling BATFET"); } else { Serial.println("BATFET disabled - battery disconnected"); } } ``` -------------------------------- ### Set Fast Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the fast charge current within the 0.512A to 4.544A range. Recommended setting is C/2 based on battery capacity. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // For a 750 mAh battery, set charge current to C/2 = 0.375 A if (!PMIC.setChargeCurrent(0.375)) { Serial.println("Error setting charge current"); } // Read back the charge current float chargeCurrent = PMIC.getChargeCurrent(); if (!isnan(chargeCurrent)) { Serial.print("Charge current: "); Serial.print(chargeCurrent * 1000); Serial.println(" mA"); // Output: Charge current: 375 mA } } ``` -------------------------------- ### Set Termination Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the current threshold at which charging terminates. ```APIDOC ## PMIC.setTermChargeCurrent(current) ### Description Sets the current threshold at which charging terminates. Valid range is 0.128A to 2.048A. ### Parameters - **current** (float) - Required - Termination current in Amperes. ### Response - **bool** - Returns true if successful, false otherwise. ``` -------------------------------- ### Set Termination Charge Current Source: https://context7.com/arduino-libraries/arduino_bq24195/llms.txt Sets the current threshold for terminating the charging process, valid between 0.128A and 2.048A. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); if (!PMIC.begin()) { Serial.println("Failed to initialize PMIC!"); while (1); } // Set termination current to 128 mA if (!PMIC.setTermChargeCurrent(0.128)) { Serial.println("Error setting termination charge current"); } // Read back the termination current float termCurrent = PMIC.getTermChargeCurrent(); if (!isnan(termCurrent)) { Serial.print("Termination current: "); Serial.print(termCurrent * 1000); Serial.println(" mA"); // Output: Termination current: 128 mA } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.