Skip to main content

Read the PD status

What this example shows​

Once a second, the example 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 example only reads from the HUSB238 and changes nothing on the board.

The example creates the I2C bus on GPIO 8 (SDA) and GPIO 9 (SCL). Wire those and GND to the board's 3-pin header, or change the pin numbers to match your wiring. Plug a charger into the board with the slide switch on.

The example code​

This is Examples/husb238-basic.py from the pinned module, without its header comment.

from machine import I2C, Pin
import time
from husb238 import HUSB238

# Change these to match how your board wires I2C
i2c = I2C(0, scl=Pin(9), sda=Pin(8))
husb238 = HUSB238(i2c)

while True:
if husb238.isAttached():
print("Attached, voltage: {} V, current: {} A".format(
husb238.getPDSrcVoltage(), husb238.getPDSrcCurrent()))
else:
print("Not attached")

time.sleep(1)

🔗Open the complete example

Examples/husb238-basic.py at commit 7184665

→

Expected result: with a PD charger attached, a new line appears in the shell every second. It starts with Attached, voltage:, then gives the agreed voltage as a whole number of 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 shell shows:

ShellMicroPython
Attached, voltage: 20 V, current: 3.25 A
Attached, voltage: 20 V, current: 3.25 A
Attached, voltage: 20 V, current: 3.25 A
Attached, voltage: 20 V, current: 3.25 A

Functions used​

HUSB238(i2c=None, address=HUSB238_ADDRESS)returns HUSB238

Creates the HUSB238 object on an I2C bus. The examples pass a bus they made themselves. Without one, the module creates I2C(0, scl=Pin(22), sda=Pin(21)) on boards whose uname().sysname is esp32 or esp8266, or one older Soldered board by its MicroPython name, and raises 'Board not recognized, enter I2C pins manually' on any other board.

ReturnsThe HUSB238 object.

Parameters

NameTypeDescription
i2cI2CAn initialized machine.I2C object. Optional.
addressintI2C address. Defaults to HUSB238_ADDRESS, which is 0x08, the board's fixed address.
husb238.isAttached()returns bool

Checks whether a PD source is attached. It reads the HUSB238's PD_STATUS1 register and tests its attach bit. A read that fails counts as 0, so it returns False.

ReturnsTrue if a PD source is attached, False otherwise.

husb238.getPDSrcVoltage()returns int

Reads 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.

husb238.getPDSrcCurrent()returns float

Reads 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.

Putting it together​

HUSB238(i2c) uses the bus the example created and the default address 0x08. In the loop, isAttached() decides which line to print. When a source is attached, getPDSrcVoltage() and getPDSrcCurrent() each read the status register once, and time.sleep(1) keeps it to one line a second.

To change the voltage from code, see Request a voltage.