### Begin CH224X Setup Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Required initialization call within the Arduino setup function. ```cpp CH224X1.begin(); ``` -------------------------------- ### FGV CH224X Arduino Library - Initialization Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Examples of how to initialize the CH224X library using I2C, with and without the power good pin. ```APIDOC ## Initialization Examples ### Using Power Good Pin (CH224A/Q) - **With default Wire object**: `CH224X_I2C CH224X1(Wire, 0x23, PG_PIN);` - **With custom SDA/SCL pins**: `CH224X_I2C CH224X1(2, 3, 0x23, PG_PIN);` ### Without Power Good Pin (CH224A/Q) - **With default Wire object**: `CH224X_I2C CH224X1(Wire, 0x23);` - **With custom SDA/SCL pins**: `CH224X_I2C CH224X1(2, 3, 0x23);` ### Setup Function Ensure you call `CH224X1.begin();` within your Arduino sketch's `setup()` function. ``` -------------------------------- ### FGV CH224X Arduino Library - I2C Wiring Examples Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Wiring diagrams and pinout information for connecting the CH224X module to various Arduino-compatible boards using I2C. ```APIDOC ## I2C Wiring Examples ### NOTE on 5V Logic MCUs For 5V logic MCUs, use the `5L` and `5D` pins on the CH224X for I2C communication, as they include a built-in I2C TTL voltage converter. ### Arduino (ATmega328) / LGT8F328 (5V logic) | CH224X Pin | Board Pin Example | |----------------|-------------------| | SDA (5D) | A5 (Arduino, LGT8F328) → *may differ on other MCUs* | | SCL (5L) | A4 (Arduino, LGT8F328) → *may differ on other MCUs* | | PG (Power Good)| A7 | | 5V | 5V | | 3V | 3.3V | | GND | GND | --- ### Arduino MEGA AND DUE (5V logic) | CH224X Pin | Board Pin Example | |----------------|-------------------| | SDA (5D) | D20 (Arduino MEGA) → *may differ on other MCUs* | | SCL (5L) | D21 (Arduino MEGA) → *may differ on other MCUs* | | PG (Power Good)| D17 | | 5V | 5V | | 3V | 3.3V | | GND | GND | --- ### STM32 (3.3V logic) | CH224X Pin | Board Pin Example | |----------------|-------------------| | SDA (SD) | PB7 (STM32F103xxx) → *may differ on other MCUs* | | SCL (SL) | PB6 (STM32F103xxx) → *may differ on other MCUs* | | PG (Power Good)| PB5 | | 3V | 3.3V | | GND | GND | ``` -------------------------------- ### Get Active Protocol Bitmask with getPowerProtocol() Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Returns a bitmask representing active USB charging protocols. Use the provided protocol constants to verify specific capabilities. ```cpp #include #include CH224X_I2C CH224X1(Wire, 0x23); // Protocol bitmask values: // PROTOCOL_BC = 0x01 (Battery Charging) // PROTOCOL_QC2 = 0x02 (Quick Charge 2.0) // PROTOCOL_QC3 = 0x04 (Quick Charge 3.0) // PROTOCOL_PD = 0x08 (USB Power Delivery) // PROTOCOL_EPR = 0x10 (Extended Power Range active) // PROTOCOL_EPR_EXIST = 0x20 (EPR supported) // PROTOCOL_AVS_EXIST = 0x40 (Adjustable Voltage Supply supported) void setup() { Serial.begin(9600); Wire.begin(); CH224X1.begin(); delay(200); int proto = CH224X1.getPowerProtocol(); Serial.println("Detected protocols:"); if (proto & CH224X_I2C::PROTOCOL_BC) Serial.println(" - BC (Battery Charging)"); if (proto & CH224X_I2C::PROTOCOL_QC2) Serial.println(" - QC2 (Quick Charge 2.0)"); if (proto & CH224X_I2C::PROTOCOL_QC3) Serial.println(" - QC3 (Quick Charge 3.0)"); if (proto & CH224X_I2C::PROTOCOL_PD) Serial.println(" - PD (USB Power Delivery)"); if (proto & CH224X_I2C::PROTOCOL_EPR) Serial.println(" - EPR (Extended Power Range)"); Serial.println("Capabilities:"); if (proto & CH224X_I2C::PROTOCOL_EPR_EXIST) Serial.println(" - EPR available"); if (proto & CH224X_I2C::PROTOCOL_AVS_EXIST) Serial.println(" - AVS available"); } void loop() {} ``` -------------------------------- ### getPowerProtocol() - Get Active Protocol Bitmask Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Returns a bitmask indicating which USB charging protocols are active. Use with protocol constants to check specific protocols. ```APIDOC ## getPowerProtocol() ### Description Returns a bitmask indicating which USB charging protocols are active. Use with protocol constants to check specific protocols. ### Method GET (Implicit) ### Endpoint N/A (Library function) ### Parameters None ### Request Example ```cpp int proto = CH224X1.getPowerProtocol(); ``` ### Response #### Success Response (int) - **proto** (int) - A bitmask representing the active charging protocols. Constants like `PROTOCOL_BC`, `PROTOCOL_QC2`, `PROTOCOL_QC3`, `PROTOCOL_PD`, `PROTOCOL_EPR`, `PROTOCOL_EPR_EXIST`, `PROTOCOL_AVS_EXIST` can be used to check for specific protocols. #### Response Example ``` // Example: If PD and QC3 are active 12 // (0x08 for PD + 0x04 for QC3) ``` ``` -------------------------------- ### Initialize CH224X I2C Instance Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Constructs the I2C object with optional power good pin configuration and initializes the library. ```cpp CH224X_I2C CH224X1(Wire, 0x23, PG_PIN) CH224X_I2C CH224X1(2, 3, 0x23, PG_PIN) CH224X_I2C CH224X1(Wire, 0x23) CH224X_I2C CH224X1(2, 3, 0x23) CH224X1.begin(); ``` -------------------------------- ### Initialize CH224X_I2C with Power Good Pin Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Creates an I2C instance with the active-low Power Good pin enabled for monitoring. ```cpp #include #include // Define the Power Good pin #define PG_PIN A7 // Create CH224X instance with I2C address 0x23 and PG monitoring CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); void setup() { Serial.begin(9600); Wire.begin(); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1); } Serial.println("CH224X initialized successfully"); } ``` -------------------------------- ### Initialize CH224X GPIO Mode Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Configures the CH224X in GPIO mode for voltage control. Requires defining pins for IO and Power Good monitoring. ```cpp #include // Pin definitions #define IO1 A4 #define IO2 A5 #define IO3 A6 #define PG_PIN A7 // For CH224A/Q chips: 0:5V, 1:9V, 2:12V, 3:20V, 4:28V // For CH224K chips: 0:5V, 1:9V, 2:12V, 3:15V, 4:20V // With Power Good monitoring CH224X_IO CH224X1(IO1, IO2, IO3, CH224_AQ, PG_PIN); // Without Power Good monitoring (alternative) // CH224X_IO CH224X1(IO1, IO2, IO3, CH224_AQ); void setup() { Serial.begin(115200); CH224X1.begin(); // Initialize GPIO pins Serial.println("CH224X IO mode initialized"); // Request 12V output CH224X1.setVoltage(2); delay(1000); if (CH224X1.isPowerGood()) { Serial.println("12V output stable"); } else { Serial.println("12V not supported"); } } void loop() {} ``` -------------------------------- ### Initialize CH224X IO Mode Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Commands to instantiate the CH224X_IO class for different chip variants and power good configurations. ```cpp CH224X_IO CH224X1(IO1, IO2, IO3, CH224_AQ, PG_PIN); ``` ```cpp CH224X_IO CH224X1(IO1, IO2, IO3, CH224_AQ); ``` ```cpp CH224X_IO CH224X1(IO1, IO2, IO3, CH224_K, PG_PIN); ``` ```cpp CH224X_IO CH224X1(IO1, IO2, IO3, CH224_K); ``` -------------------------------- ### Request Voltage Output via setVoltage() Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Negotiates a specific voltage output using I2C mode. Requires a delay after initialization for PD negotiation. ```cpp #include #include CH224X_I2C CH224X1(Wire, 0x23, A7); // Voltage selection codes for I2C mode: // 0: 5V 1: 9V 2: 12V 3: 15V // 4: 20V 5: 28V 6: PPS mode (CH224Q only) // 7: AVS mode (CH224Q only) void setup() { Serial.begin(9600); Wire.begin(); CH224X1.begin(); delay(200); // Request 20V output (USB PD required) if (CH224X1.setVoltage(4)) { Serial.println("Requested 20V successfully"); } else { Serial.println("Voltage request failed"); } delay(400); // Wait for PD negotiation // Check if voltage is stable if (CH224X1.isPowerGood()) { Serial.println("20V output is stable"); } else { Serial.println("20V not supported, falling back to 12V"); CH224X1.setVoltage(2); // Request 12V instead } } void loop() {} ``` -------------------------------- ### Initialize CH224X_I2C without Power Good Pin Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Creates an I2C instance without monitoring the Power Good pin. ```cpp #include #include // Create CH224X instance without PG monitoring CH224X_I2C CH224X1(Wire, 0x23); void setup() { Serial.begin(9600); Wire.begin(); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1); } Serial.println("CH224X initialized"); } ``` -------------------------------- ### Monitor Voltage Stability with isPowerGood() Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Checks if the output voltage is stable using the Power Good pin. Requires the pin to be defined during initialization. ```cpp #include #include #define PG_PIN A7 #define LED_PIN 13 CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); Wire.begin(); CH224X1.begin(); CH224X1.setVoltage(2); // Request 12V delay(500); } void loop() { if (CH224X1.isPowerGood()) { Serial.println("Power Good: Output voltage is stable"); digitalWrite(LED_PIN, HIGH); } else { Serial.println("Power Bad: Voltage unstable or unsupported"); digitalWrite(LED_PIN, LOW); } delay(1000); } ``` -------------------------------- ### Check Supported USB Protocols Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Verifies if the connected charger supports specific USB power delivery protocols. ```cpp CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_BC) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC2) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC3) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_PD) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_EPR) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_EPR_EXIST) CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_AVS_EXIST) ``` -------------------------------- ### Complete USB Protocol Tester Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt This Arduino sketch tests USB power delivery capabilities by scanning through predefined voltage profiles and reporting supported protocols, current, and power. It requires the CH224X_I2C library and uses I2C communication. ```cpp #include #include #define PG_PIN A7 #define LED_BUILTIN 13 CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); const uint8_t voltageProfiles[] = {0, 1, 2, 3, 4, 5}; const float voltageValues[] = {5.0, 9.0, 12.0, 15.0, 20.0, 28.0}; const uint8_t numProfiles = 6; void printProtocols() { int proto = CH224X1.getPowerProtocol(); // Print capabilities if (proto & CH224X_I2C::PROTOCOL_AVS_EXIST) Serial.println("AVS (Adjustable Voltage Supply) supported"); if (proto & CH224X_I2C::PROTOCOL_EPR_EXIST) Serial.println("EPR (Extended Power Range) supported"); // Print active protocols Serial.print("Active protocols: "); bool first = true; if (proto & CH224X_I2C::PROTOCOL_BC) { Serial.print("BC"); first = false; } if (proto & CH224X_I2C::PROTOCOL_QC2) { if (!first) Serial.print(", "); Serial.print("QC2"); first = false; } if (proto & CH224X_I2C::PROTOCOL_QC3) { if (!first) Serial.print(", "); Serial.print("QC3"); first = false; } if (proto & CH224X_I2C::PROTOCOL_PD) { if (!first) Serial.print(", "); Serial.print("PD"); first = false; } if (proto & CH224X_I2C::PROTOCOL_EPR) { if (!first) Serial.print(", "); Serial.print("EPR"); first = false; } if (first) Serial.print("None"); Serial.println(); } void setup() { Serial.begin(9600); Wire.begin(); pinMode(LED_BUILTIN, OUTPUT); Serial.println("CH224X USB Protocol Tester"); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); delay(200); } } delay(200); } void loop() { Serial.println("\n===== Charger Analysis ====="); printProtocols(); Serial.println(); for (uint8_t i = 0; i < numProfiles; i++) { CH224X1.setVoltage(voltageProfiles[i]); delay(400); int current_mA = CH224X1.getCurrentProfile(); bool stable = CH224X1.isPowerGood(); Serial.print(voltageValues[i], 0); Serial.print("V: "); if (stable) { Serial.print("OK"); if (current_mA > 0) { Serial.print(" ("); Serial.print(current_mA); Serial.print("mA / "); Serial.print((voltageValues[i] * current_mA) / 1000.0, 1); Serial.print("W)"); } digitalWrite(LED_BUILTIN, HIGH); } else { Serial.print("Not supported"); digitalWrite(LED_BUILTIN, LOW); } Serial.println(); delay(300); } Serial.println("============================"); delay(5000); } ``` -------------------------------- ### isPowerGood() - Check Voltage Stability Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Returns true when the requested voltage output is stable. The PG pin must be connected during initialization. It's an active-low signal: LOW means power is good, HIGH means power is bad. ```APIDOC ## isPowerGood() - Check Voltage Stability ### Description Returns true when the requested voltage output is stable. The PG pin must be connected during initialization. Active-low signal: LOW = power good, HIGH = power bad. ### Method `bool isPowerGood()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include #define PG_PIN A7 #define LED_PIN 13 CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); Wire.begin(); CH224X1.begin(); CH224X1.setVoltage(2); // Request 12V delay(500); } void loop() { if (CH224X1.isPowerGood()) { Serial.println("Power Good: Output voltage is stable"); digitalWrite(LED_PIN, HIGH); } else { Serial.println("Power Bad: Voltage unstable or unsupported"); digitalWrite(LED_PIN, LOW); } delay(1000); } ``` ### Response #### Success Response (200) - **return value** (bool) - True if the output voltage is stable, false otherwise. #### Response Example ``` true ``` ``` -------------------------------- ### FGV CH224X Arduino Library - I2C Commands Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md This section details the available commands for the CH224X library when using the I2C interface. These commands allow for detecting protocols, reading current, and controlling output voltage. ```APIDOC ## Arduino I2C Library Commands ### `isPowerGood()` - **Description**: Reads the power good indicator from CH224A/Q. Active low means voltage is good (0), voltage is bad (1). - **Return Type**: `bool` ### `setVoltage(int voltage_code)` - **Description**: Controls the USB PD/QC output voltage. Accepts an integer code representing the desired voltage. - **Parameters**: - `voltage_code` (int) - Required - Code for the desired voltage: - 0: 5V - 1: 9V - 2: 12V - 3: 15V - 4: 20V - 5: 28V - 6: PPS mode (CH224Q only) - 7: AVS mode (CH224Q only) - **Example**: `CH224X1.setVoltage(4);` // Triggers USB to output 20V (USB PD protocol only) ### `getCurrentProfile()` - **Description**: Reads the current output in milliamps. Returns an integer representing the current in mA. - **Return Type**: `int` - **Example**: If a charger outputs 65W, this might return 3250 (representing 3.25A at 20V, though the function specifically returns mA). ### `hasProtocol(int protocol_code)` - **Description**: Checks if the connected device supports a specific USB protocol. - **Parameters**: - `protocol_code` (int) - Required - Code for the protocol to check: - `CH224X_I2C::PROTOCOL_BC`: USB BC protocol - `CH224X_I2C::PROTOCOL_QC2`: USB QC2 protocol - `CH224X_I2C::PROTOCOL_QC3`: USB QC3 protocol - `CH224X_I2C::PROTOCOL_PD`: USB PD protocol - `CH224X_I2C::PROTOCOL_EPR`: USB EPR (Extended Power Range) protocol - `CH224X_I2C::PROTOCOL_EPR_EXIST`: Checks if the charger supports EPR - `CH224X_I2C::PROTOCOL_AVS_EXIST`: Checks if the charger supports AVS (Adjustable Voltage Supply) - **Return Type**: `bool` - **Example**: `CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC2);` ``` -------------------------------- ### Control CH224K Voltage Profiles via GPIO Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Demonstrates cycling through voltage profiles specific to the CH224K chip variant using GPIO mode. ```cpp #include // ESP32 pin definitions #define IO1 4 #define IO2 6 #define IO3 5 #define PG_PIN 7 // CH224K voltage mapping: 0:5V, 1:9V, 2:12V, 3:15V, 4:20V CH224X_IO CH224X1(IO1, IO2, IO3, CH224_K, PG_PIN); const uint8_t voltageProfiles[] = {0, 1, 2, 3, 4}; const float voltageValues[] = {5.0, 9.0, 12.0, 15.0, 20.0}; void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); CH224X1.begin(); delay(200); } void loop() { Serial.println("Testing CH224K voltage profiles..."); for (uint8_t i = 0; i < 5; i++) { int result = CH224X1.setVoltage(voltageProfiles[i]); Serial.print("Set voltage: "); Serial.print(voltageValues[i]); Serial.println(" V"); delay(1000); // Allow voltage to stabilize if (CH224X1.isPowerGood()) { Serial.println("Output stable"); digitalWrite(LED_BUILTIN, HIGH); } else { Serial.println("Voltage not supported"); digitalWrite(LED_BUILTIN, LOW); } delay(500); } delay(3000); } ``` -------------------------------- ### CH224X_I2C Constructor with Power Good Pin Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Initializes the CH224X_I2C class with Power Good (PG) pin monitoring enabled. The PG pin is active-low and indicates stable voltage output when LOW. ```APIDOC ## CH224X_I2C Class - Constructor with Power Good Pin ### Description Creates an I2C interface instance with the Power Good monitoring pin enabled. The Power Good (PG) pin is active-low, indicating stable voltage output when LOW. ### Method `CH224X_I2C(TwoWire &wire, uint8_t i2c_addr, uint8_t pg_pin)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include // Define the Power Good pin #define PG_PIN A7 // Create CH224X instance with I2C address 0x23 and PG monitoring CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); void setup() { Serial.begin(9600); Wire.begin(); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1); } Serial.println("CH224X initialized successfully"); } ``` ### Response #### Success Response (200) None (Constructor does not return a value, initialization is checked via `begin()`) #### Response Example None ``` -------------------------------- ### setVoltage() - Request Specific Voltage Output Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Requests a specific voltage from the USB power source via PD/QC negotiation. Returns 1 on success, 0 on failure. ```APIDOC ## setVoltage() - Request Specific Voltage Output ### Description Requests a specific voltage from the USB power source via PD/QC negotiation. Returns 1 on success, 0 on failure. ### Method `uint8_t setVoltage(uint8_t voltage_code)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include CH224X_I2C CH224X1(Wire, 0x23, A7); // Voltage selection codes for I2C mode: // 0: 5V 1: 9V 2: 12V 3: 15V // 4: 20V 5: 28V 6: PPS mode (CH224Q only) // 7: AVS mode (CH224Q only) void setup() { Serial.begin(9600); Wire.begin(); CH224X1.begin(); delay(200); // Request 20V output (USB PD required) if (CH224X1.setVoltage(4)) { Serial.println("Requested 20V successfully"); } else { Serial.println("Voltage request failed"); } delay(400); // Wait for PD negotiation // Check if voltage is stable if (CH224X1.isPowerGood()) { Serial.println("20V output is stable"); } else { Serial.println("20V not supported, falling back to 12V"); CH224X1.setVoltage(2); // Request 12V instead } } void loop() {} ``` ### Response #### Success Response (200) - **return value** (uint8_t) - 1 on success, 0 on failure. #### Response Example ``` 1 ``` ``` -------------------------------- ### CH224X_I2C Constructor without Power Good Pin Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Initializes the CH224X_I2C class without Power Good monitoring. Use this when the PG pin is not connected or not required. ```APIDOC ## CH224X_I2C Class - Constructor without Power Good Pin ### Description Creates an I2C interface instance without Power Good monitoring. Use this when the PG pin is not connected or not needed. ### Method `CH224X_I2C(TwoWire &wire, uint8_t i2c_addr)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include // Create CH224X instance without PG monitoring CH224X_I2C CH224X1(Wire, 0x23); void setup() { Serial.begin(9600); Wire.begin(); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1); } Serial.println("CH224X initialized"); } ``` ### Response #### Success Response (200) None (Constructor does not return a value, initialization is checked via `begin()`) #### Response Example None ``` -------------------------------- ### Read Maximum Available Current with getCurrentProfile() Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Retrieves the maximum current in milliamps for the current voltage setting. This method is only valid for USB PD and returns -1 on communication errors. ```cpp #include #include CH224X_I2C CH224X1(Wire, 0x23, A7); const uint8_t voltageProfiles[] = {0, 1, 2, 3, 4}; const float voltageValues[] = {5.0, 9.0, 12.0, 15.0, 20.0}; void setup() { Serial.begin(9600); Wire.begin(); CH224X1.begin(); delay(200); } void loop() { Serial.println("Scanning voltage profiles..."); for (uint8_t i = 0; i < 5; i++) { CH224X1.setVoltage(voltageProfiles[i]); delay(400); // Wait for PD negotiation int current_mA = CH224X1.getCurrentProfile(); Serial.print("Voltage: "); Serial.print(voltageValues[i]); Serial.print("V | Max Current: "); if (current_mA < 0) { Serial.println("N/A (QC or error)"); } else { Serial.print(current_mA); Serial.print(" mA ("); Serial.print((float)current_mA / 1000.0); Serial.println(" A)"); } } delay(5000); } ``` -------------------------------- ### Check for Specific Protocol with hasProtocol() Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt A convenience method that returns true if a specific protocol bit is set. Useful for conditional logic based on charger capabilities. ```cpp #include #include CH224X_I2C CH224X1(Wire, 0x23, A7); void setup() { Serial.begin(9600); Wire.begin(); CH224X1.begin(); delay(200); } void loop() { // Select optimal voltage based on protocol if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_PD)) { // USB PD available - request 20V for maximum power CH224X1.setVoltage(4); Serial.println("Using USB PD at 20V"); Serial.print("Max current: "); Serial.print(CH224X1.getCurrentProfile()); Serial.println(" mA"); } else if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC2) || CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC3)) { // QC available - request 12V (safe for most QC chargers) CH224X1.setVoltage(2); Serial.println("Using Quick Charge at 12V"); // Note: Current reading not available with QC protocol } else if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_BC)) { // Only basic BC protocol - stick with 5V CH224X1.setVoltage(0); Serial.println("Using BC protocol at 5V"); } else { Serial.println("No fast-charging protocol detected"); CH224X1.setVoltage(0); } delay(2000); } ``` -------------------------------- ### Configure CH224X I2C on ESP32 Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Sets up I2C communication for the CH224X, including custom pin assignment for ESP32-S3 boards. ```cpp #include #include // ESP32-S3 custom I2C pins #define SDA_PIN 5 #define SCL_PIN 6 #define PG_PIN 7 CH224X_I2C CH224X1(Wire, 0x23, PG_PIN); void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); // Initialize I2C with custom pins (ESP32 specific) Wire.begin(SDA_PIN, SCL_PIN); Serial.print("I2C pins: SDA="); Serial.print(SDA_PIN); Serial.print(", SCL="); Serial.println(SCL_PIN); if (!CH224X1.begin()) { Serial.println("CH224X not responding!"); while (1) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); delay(200); } } Serial.println("CH224X ready"); delay(200); } void loop() { if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_PD)) { CH224X1.setVoltage(4); // 20V Serial.print("PD 20V - Current: "); Serial.print(CH224X1.getCurrentProfile()); Serial.println(" mA"); } else if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_QC2)) { CH224X1.setVoltage(2); // 12V Serial.println("QC 12V"); } digitalWrite(LED_BUILTIN, CH224X1.isPowerGood() ? HIGH : LOW); delay(2000); } ``` -------------------------------- ### Trigger USB Output Voltage Source: https://github.com/felixardyansyah/fgv_ch224x/blob/main/README.md Sets the desired output voltage profile for the USB PD protocol. ```cpp setVoltage(4) ``` -------------------------------- ### getCurrentProfile() - Read Maximum Available Current Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Reads the maximum current available at the current voltage setting. Returns current in milliamps. Only valid with USB PD protocol (not QC). Returns -1 on communication error. ```APIDOC ## getCurrentProfile() ### Description Reads the maximum current available at the current voltage setting. Returns current in milliamps. Only valid with USB PD protocol (not QC). Returns -1 on communication error. ### Method GET (Implicit) ### Endpoint N/A (Library function) ### Parameters None ### Request Example ```cpp int current_mA = CH224X1.getCurrentProfile(); ``` ### Response #### Success Response (int) - **current_mA** (int) - Maximum available current in milliamps, or -1 on communication error. #### Response Example ``` // If successful 5000 // If error -1 ``` ``` -------------------------------- ### hasProtocol() - Check for Specific Protocol Source: https://context7.com/felixardyansyah/fgv_ch224x/llms.txt Convenience method to check if a specific protocol is active. Returns true if the specified protocol bit is set. ```APIDOC ## hasProtocol() ### Description Convenience method to check if a specific protocol is active. Returns true if the specified protocol bit is set. ### Method GET (Implicit) ### Endpoint N/A (Library function) ### Parameters #### Query Parameters - **protocol_constant** (int) - Required - A constant representing the protocol to check (e.g., `CH224X_I2C::PROTOCOL_PD`). ### Request Example ```cpp if (CH224X1.hasProtocol(CH224X_I2C::PROTOCOL_PD)) { // USB PD is active } ``` ### Response #### Success Response (bool) - **true** (bool) - If the specified protocol is active. - **false** (bool) - If the specified protocol is not active. #### Response Example ``` true ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.