Skip to main content

Request a voltage

What this example shows​

The example asks the charger for a new voltage over I2C, once. It prints whether the charger switched, and if it did, it prints the voltage and current every second. The voltage to ask for is REQUEST_VOLTAGE, 15 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 example.

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-requestVoltage.py from the pinned module, without its header comment.

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

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

# Voltage to request from the source, one of 5, 9, 12, 15, 18, 20
REQUEST_VOLTAGE = 15

print("Requesting {} V...".format(REQUEST_VOLTAGE))

result = husb238.requestPD(REQUEST_VOLTAGE)
requestOk = False

if result == HUSB238_REQUEST_OK:
print("Source switched voltage successfully")
requestOk = True
elif result == HUSB238_REQUEST_UNSUPPORTED_VOLTAGE:
print("Invalid voltage value, use 5/9/12/15/18/20")
elif result == HUSB238_REQUEST_NOT_OFFERED:
print("Charger does not support this voltage")
elif result == HUSB238_REQUEST_REJECTED:
print("Charger rejected the request")

while requestOk:
print("Voltage: {} V, current: {} A".format(
husb238.getPDSrcVoltage(), husb238.getPDSrcCurrent()))
time.sleep(1)

🔗Open the complete example

Examples/husb238-requestVoltage.py at commit 7184665

→

Expected result, with a 65 W USB-C PD charger that offers 15 V at 3 A:

ShellMicroPython
Requesting 15 V...
Source switched voltage successfully
Voltage: 15 V, current: 3.0 A
Voltage: 15 V, current: 3.0 A
Voltage: 15 V, current: 3.0 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 the example ends.

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.requestPD(voltage)returns str

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

ReturnsOne of the four HUSB238_REQUEST_* constants in the table below.

Parameters

NameTypeDescription
voltagefloatVoltage to request: 5, 9, 12, 15, 18 or 20.
Return valueMeaning
HUSB238_REQUEST_OKThe source switched voltage successfully.
HUSB238_REQUEST_UNSUPPORTED_VOLTAGEThe voltage is not one of 5, 9, 12, 15, 18 or 20. Nothing is sent.
HUSB238_REQUEST_NOT_OFFEREDThe source does not advertise this voltage. The library reads the source's offer for it and writes nothing.
HUSB238_REQUEST_REJECTEDThe request was sent, but the source did not report success.
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​

from husb238 import * brings in the HUSB238 class and the HUSB238_REQUEST_* constants the if chain compares against. The request is sent once, so run the example again to send it again. Only HUSB238_REQUEST_OK sets requestOk, and only then does the while loop print the new contract every second.

To ask for another voltage, change REQUEST_VOLTAGE to 5, 9, 12, 15, 18 or 20 and run the example again.