USB-PD Power example script (arduino.py) Asks a USB-PD charger for up to MAX_VOLTS, then prints the board's profiles, the contract and the measured VBUS once a second.
Arduino has no match for this, so this uses mcu.usb_pd() and mcu.uart() from phyduino.py.
A charger takes the only USB-C port, so there is no USB connection to the computer while it is plugged in. Copy this main.py to the board so it runs by itself, and watch the output on UART0 (TX on D1, 115200 baud) with a USB-serial adapter. The green POK2 LED lights for the second profile (9 V), POK3 for the third (the 12, 15 or 20 V of MAX_VOLTS).
- Warning
- VBUS also feeds the VIN pin, so VIN follows the contract: disconnect anything on VIN that can't take the voltage you ask for.
The request lasts until the board loses power.
- Output
Profiles: 5 V 2 A, 9 V 1 A
Contract: offer 2, 1.00 A VBUS 9.01 V
from arduino import *
import phyduino as mcu
MAX_VOLTS = 9
serial = None
usb = None
def report(text):
"""Prints text, and sends it out of UART0 too, as USB is taken by the charger."""
print(text)
serial.write(text + "\r\n")
def setup():
global serial, usb
serial = mcu.uart(0)
usb = mcu.usb_pd()
usb.request(MAX_VOLTS)
report("Profiles: " + ", ".join("%g V %g A" % p for p in usb.profiles()))
def loop():
volts, amps = usb.contract()
position = usb.contract_position()
if position:
report("Contract: offer %d, %.2f A VBUS %.2f V" % (position, amps, volts))
else:
report("No PD contract (plain USB) VBUS %.2f V" % volts)
delay(1000)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()