Read the PD status
What this example shows
Once a second, the sketch asks the HUSB238 whether a USB-C PD charger is
attached. If one is, it prints the voltage the charger agreed to and the
charger's current for that voltage. If not, it prints Not attached. The
sketch only reads from the HUSB238 and changes nothing on the board.
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 Basic.ino from the pinned library, without its header comment. The
sketch itself is exactly as shipped.
#include "HUSB238-SOLDERED.h"
Husb238 husb238;
void setup()
{
Serial.begin(115200);
husb238.begin();
}
void loop()
{
if (husb238.isAttached())
{
Serial.print("Attached, voltage: ");
Serial.print(husb238.getPDSrcVoltage());
Serial.print(" V, current: ");
Serial.print(husb238.getPDSrcCurrent());
Serial.println(" A");
}
else
{
Serial.println("Not attached");
}
delay(1000);
}
Expected result: open the Serial Monitor at 115200 baud. With a PD charger
attached, a new line appears every second. It starts with Attached, voltage:,
then gives the agreed voltage in volts and the charger's current in amps. When
no PD charger is attached, each line reads Not attached. The numbers depend on
your charger and on the DIP switches.
With a 65 W USB-C PD charger that offers 20 V at 3.25 A, and every DIP switch off, the Serial Monitor shows:
Attached, voltage: 20.00 V, current: 3.25 A
Attached, voltage: 20.00 V, current: 3.25 A
Attached, voltage: 20.00 V, current: 3.25 A
Attached, voltage: 20.00 V, current: 3.25 A
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.
isAttached()returns boolChecks whether a PD source is attached. It reads the HUSB238's PD_STATUS1 register and tests its attach bit. A read that gets no answer counts as 0, so it returns false.
Returnstrue if a PD source is attached, false otherwise.
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
Husb238 husb238; uses the default address 0x08 and Wire, and begin()
starts the bus. In loop(), isAttached() decides which line to print. When a
source is attached, getPDSrcVoltage() and getPDSrcCurrent() each read the
status register once, and delay(1000) keeps it to one line a second.
To change the voltage from code, see Request a voltage.