{"id":10055,"library":"ppk2-api","title":"PPK2 API","description":"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).","status":"active","version":"0.9.2","language":"en","source_language":"en","source_url":"https://github.com/IRNAS/ppk2-api-python","tags":["hardware","nordic","power profiling","ppk2","serial","embedded"],"install":[{"cmd":"pip install ppk2-api","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides serial communication with the PPK2 device. The library uses pySerial internally.","package":"pyserial"}],"imports":[{"symbol":"PPK2","correct":"from ppk2_api import PPK2"},{"symbol":"PPK2_Port","correct":"from ppk2_api import PPK2_Port"},{"symbol":"PPK2_Mode","correct":"from ppk2_api import PPK2_Mode"},{"note":"PowerProfiler is an example implementation, not a core API class directly available under the main ppk2_api namespace. It resides in the examples subdirectory.","wrong":"from ppk2_api import PowerProfiler","symbol":"PowerProfiler","correct":"from ppk2_api.examples.power_profiler import PowerProfiler"}],"quickstart":{"code":"from ppk2_api import PPK2, PPK2_Port, PPK2_Mode\n\ndef main():\n    try:\n        # Discover and connect to the PPK2 device\n        ppk2 = PPK2.discover()\n        print(f\"Connected to PPK2: {ppk2.get_device_id()}\")\n\n        # Set mode to ampere meter and connect to channel 0\n        ppk2.set_mode(PPK2_Mode.AMPERE_METER)\n        ppk2.connect_to_port(PPK2_Port.PORT_0)\n        ppk2.start_measurement()\n\n        print(\"Measuring for 5 seconds...\")\n        # In a real application, you'd typically stream data\n        # For simplicity, we'll just wait and then stop.\n        # Data streaming would involve a loop and calling ppk2.get_data()\n        import time\n        time.sleep(5)\n\n        ppk2.stop_measurement()\n        print(\"Measurement stopped.\")\n\n        # Example of getting some data (note: real streaming is more complex)\n        data = ppk2.get_data(num_samples=1000)\n        if data:\n            print(f\"Average current: {sum(data['current'])/len(data['current']):.2f} mA\")\n            print(f\"Average voltage: {sum(data['voltage'])/len(data['voltage']):.2f} V\")\n        else:\n            print(\"No data collected or available in this simple example.\")\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n    finally:\n        # Ensure the device is properly closed\n        if 'ppk2' in locals() and ppk2.is_connected():\n            ppk2.close()\n            print(\"PPK2 connection closed.\")\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"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."},"warnings":[{"fix":"Review concurrent code that interacts with ppk2-api. Adapt to threading model for internal operations. Consider explicit process management if isolation is critical for your application logic.","message":"The internal concurrency mechanism was changed from `multiprocessing` to `threading` in version 0.9.1. Applications relying on specific process isolation or resource handling behaviors introduced by multiprocessing should review their implementation.","severity":"breaking","affected_versions":">=0.9.1"},{"fix":"Always use version 0.9.0 or newer for accurate current measurements. Re-run experiments if data was collected using affected versions.","message":"Prior to version 0.9.0, current measurements could be incorrect due to 'incorrect spike filtering'. Data collected with older versions may be unreliable.","severity":"gotcha","affected_versions":"<0.9.0"},{"fix":"Wrap `PPK2.discover()` in a `try...except NoPPK2FoundException` block. Verify the PPK2 is powered on and connected. Check USB drivers and permissions. Ensure no other application is using the device.","message":"The `PPK2.discover()` method may raise a `NoPPK2FoundException` if no device is connected or detectable. Ensure the PPK2 device is properly connected and recognized by the operating system.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install ppk2-api` to install the library.","cause":"The `ppk2-api` package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'ppk2_api'"},{"fix":"Ensure the PPK2 device is physically connected via USB, powered on, and its drivers are correctly installed. Check if another application is already using the device. Try restarting the PPK2 or your computer.","cause":"The `PPK2.discover()` method could not locate a connected Power Profiler Kit II device.","error":"ppk2_api.exceptions.NoPPK2FoundException: No PPK2 device found."},{"fix":"On Linux, add your user to the `dialout` group: `sudo usermod -a -G dialout $USER`. Then, log out and log back in for the changes to take effect. On Windows/macOS, ensure proper drivers are installed and no conflicting software is running.","cause":"The current user does not have read/write permissions for the serial port associated with the PPK2 device.","error":"serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyACM0: Permission denied: '/dev/ttyACM0'"},{"fix":"`PowerProfiler` is an example class, not a core API component. If you intend to use the example, import it from `from ppk2_api.examples.power_profiler import PowerProfiler`.","cause":"Attempting to import `PowerProfiler` directly from the top-level `ppk2_api` module.","error":"AttributeError: module 'ppk2_api' has no attribute 'PowerProfiler'"}]}