PPK2 API

0.9.2 · active · verified Fri Apr 17

The ppk2-api is a Python library providing an API for Nordic Semiconductor's Power Profiler Kit II (PPK 2). It enables programmatic control and data acquisition from the PPK 2 device, facilitating power consumption measurements and analysis for embedded development. The current version is 0.9.2, with active development and frequent releases (e.g., v0.9.0, v0.9.1, v0.9.2 in quick succession, aiming for v1.0.0).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to discover a PPK2 device, set it to ampere meter mode, start a measurement, wait for a period, and then stop it. It also shows how to retrieve some sample data. Note that actual data streaming involves more continuous polling with `ppk2.get_data()` in a loop.

from ppk2_api import PPK2, PPK2_Port, PPK2_Mode

def main():
    try:
        # Discover and connect to the PPK2 device
        ppk2 = PPK2.discover()
        print(f"Connected to PPK2: {ppk2.get_device_id()}")

        # Set mode to ampere meter and connect to channel 0
        ppk2.set_mode(PPK2_Mode.AMPERE_METER)
        ppk2.connect_to_port(PPK2_Port.PORT_0)
        ppk2.start_measurement()

        print("Measuring for 5 seconds...")
        # In a real application, you'd typically stream data
        # For simplicity, we'll just wait and then stop.
        # Data streaming would involve a loop and calling ppk2.get_data()
        import time
        time.sleep(5)

        ppk2.stop_measurement()
        print("Measurement stopped.")

        # Example of getting some data (note: real streaming is more complex)
        data = ppk2.get_data(num_samples=1000)
        if data:
            print(f"Average current: {sum(data['current'])/len(data['current']):.2f} mA")
            print(f"Average voltage: {sum(data['voltage'])/len(data['voltage']):.2f} V")
        else:
            print("No data collected or available in this simple example.")

    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        # Ensure the device is properly closed
        if 'ppk2' in locals() and ppk2.is_connected():
            ppk2.close()
            print("PPK2 connection closed.")

if __name__ == "__main__":
    main()

view raw JSON →