Request a voltage
What this example shows
The sketch asks the charger for a new voltage over I2C, once, at start-up. It
prints whether the charger switched, and if it did, it prints the voltage and
current every second. The voltage to ask for is set by REQUEST_VOLTAGE, 20 V
in the example; it must be 5, 9, 12, 15, 18 or 20.
A successful request changes the voltage on VOUT. Disconnect anything on
VOUT that cannot take the voltage you request before you run the sketch.
Wire SDA, SCL and GND to your controller's I2C pins as in the
Quick Start,
and plug a charger into the board with the slide switch on.
The example code
This is RequestVoltage.ino from the pinned library, without its header
comment. The sketch itself is exactly as shipped.
#include "HUSB238-SOLDERED.h"
Husb238 husb238;
// Voltage to request from the source, one of 5, 9, 12, 15, 18, 20
#define REQUEST_VOLTAGE 20
bool requestOk = false;
void setup()
{
Serial.begin(115200);
husb238.begin();
Serial.print("Requesting ");
Serial.print(REQUEST_VOLTAGE);
Serial.println(" V...");
switch (husb238.requestPD(REQUEST_VOLTAGE))
{
case HUSB238_REQUEST_OK:
Serial.println("Source switched voltage successfully");
requestOk = true;
break;
case HUSB238_REQUEST_UNSUPPORTED_VOLTAGE:
Serial.println("Invalid voltage value, use 5/9/12/15/18/20");
break;
case HUSB238_REQUEST_NOT_OFFERED:
Serial.println("Charger does not support this voltage");
break;
case HUSB238_REQUEST_REJECTED:
Serial.println("Charger rejected the request");
break;
}
}
void loop()
{
if (!requestOk)
return;
Serial.print("Voltage: ");
Serial.print(husb238.getPDSrcVoltage());
Serial.print(" V, current: ");
Serial.print(husb238.getPDSrcCurrent());
Serial.println(" A");
delay(1000);
}
Expected result, with a 65 W USB-C PD charger that offers 20 V at 3.25 A, in the Serial Monitor at 115200 baud:
Requesting 20 V...
Source switched voltage successfully
Voltage: 20.00 V, current: 3.25 A
Voltage: 20.00 V, current: 3.25 A
Voltage: 20.00 V, current: 3.25 A
After the first two lines, a line starting with Voltage: appears every
second, with the agreed voltage in volts and the charger's current in amps. If
the request fails, the second line is one of Invalid voltage value, use 5/9/12/15/18/20,
Charger does not support this voltage or Charger rejected the request, and
nothing more is printed.
Functions used
Husb238(uint8_t _address = HUSB238_I2C_ADDR, TwoWire &_wire = Wire)returns NoneCreates the HUSB238 object. It only stores the I2C address and the Wire port. Nothing is read from or written to the chip until the first read or request function.
ReturnsConstructor; no return value.
Parameters
| Name | Type | Description |
|---|---|---|
_address | uint8_t | I2C address. Defaults to HUSB238_I2C_ADDR, which is 0x08, the board's fixed address. |
_wire | TwoWire & | The Wire interface to use. Defaults to Wire. |
begin()returns voidStarts I2C communication by calling begin() on the Wire port the object was created with.
ReturnsNothing.
requestPD(float voltage)returns husb238RequestResult_tAsks the source to switch to the given voltage. It first checks that the value is one of the six the HUSB238 can request and that the source advertises it. Then it writes the request to the chip, waits 300 ms and reads back the source's response.
ReturnsThe outcome of the request, one of the four values in the table below.
Parameters
| Name | Type | Description |
|---|---|---|
voltage | float | Voltage to request: 5, 9, 12, 15, 18 or 20. |
| Return value | Meaning |
|---|---|
HUSB238_REQUEST_OK | The source switched voltage successfully. |
HUSB238_REQUEST_UNSUPPORTED_VOLTAGE | The voltage is not one of 5, 9, 12, 15, 18 or 20. Nothing is sent. |
HUSB238_REQUEST_NOT_OFFERED | The source does not advertise this voltage. The library reads the source's offer for it and writes nothing. |
HUSB238_REQUEST_REJECTED | The request was sent, but the source did not report success. |
getPDSrcVoltage()returns floatReads the currently negotiated source voltage from the PD_STATUS0 register.
ReturnsThe voltage in volts: 5, 9, 12, 15, 18 or 20, or 0 if no source is attached.
getPDSrcCurrent()returns floatReads the source's maximum current for the negotiated contract from the PD_STATUS0 register.
ReturnsThe current in amps, one of the HUSB238's steps from 0.5 to 5.0 A.
Putting it together
setup() sends the request once, so reset the controller to send it again.
The switch turns the result of requestPD() into a message, and only
HUSB238_REQUEST_OK sets requestOk. Until it is set, loop() returns at once
and prints nothing. Once it is, getPDSrcVoltage() and getPDSrcCurrent() show
the new contract every second.
To ask for another voltage, change REQUEST_VOLTAGE to 5, 9, 12, 15, 18 or 20
and upload the sketch again.