Phidget22 Python Wrapper
Phidget22 is the official Python library for working with Phidgets in Python applications. It provides lightweight bindings to the native phidget22 library. The current version is 1.25.20260408, and the library is actively maintained with frequent updates, often multiple times per year, bundling native libraries for major operating systems.
Common errors
-
ImportError: No module named 'Phidget22' (or similar for submodules like Phidget22.Devices)
cause The Phidget22 Python package is either not installed in the active Python environment, or the import statement refers to the old Phidget21 library (e.g., 'Phidgets' instead of 'Phidget22').fixEnsure `phidget22` is installed in your environment (`pip install phidget22`). Verify your import statements use `from Phidget22...` and not `from Phidgets...`. If using a virtual environment, ensure it's activated. -
PhidgetException: TIMED_OUT (or a similar message containing 'Timed Out')
cause The Phidget device could not be opened within the specified timeout. Common reasons include the device not being plugged in, incorrect addressing parameters (serial number, channel, hub port), the device being busy (opened by another program), or a power issue.fixVerify the Phidget is physically connected, powered on (if needed), and not already in use by another application (e.g., the Phidget Control Panel). Double-check any `DeviceSerialNumber`, `Channel`, or `HubPort` parameters you've set, or increase the `openWaitForAttachment()` timeout. -
PhidgetException: BUSY (or a similar message containing 'Resource Busy')
cause The Phidget device you are trying to open is already open by another process or application. This could be the Phidget Control Panel, another instance of your program, or a different application.fixClose any other programs or instances that might be using the Phidget, including the Phidget Control Panel. If you suspect a background process, check your operating system's task/activity monitor. -
PhidgetException: DEVICE_NOTATTACHED (or a similar message containing 'Device Not Attached')
cause The program is attempting to interact with a Phidget channel that is not currently attached or has become detached. This can occur if the device was unplugged, lost power, or the `openWaitForAttachment()` call failed or was not used, and the program tried to access the device before it was ready.fixEnsure the device is firmly connected and powered. Use `openWaitForAttachment()` with a sufficient timeout before attempting to read from or write to the device. Implement `onAttachHandler` and `onDetachHandler` to manage the device's state.
Warnings
- breaking Upgrading from Phidget21 to Phidget22 involves significant API changes. Object structures have been broken into more modular components, property and method names have changed (e.g., `open()` instead of `open()`), and outputs/settings are no longer persistent across channel opens/closes.
- gotcha HID USB Phidgets on macOS (legacy devices) require special driver installation or the Phidget Control Panel to function correctly, even with the Python library installed via pip. Newer USB and VINT Phidgets do not have this requirement.
- gotcha Phidgets require specific addressing parameters (e.g., `DeviceSerialNumber`, `Channel`, `IsHubPortDevice`) for unique identification. Incorrect or missing parameters are a common cause of devices not attaching or 'Timed Out' errors.
- gotcha Many Phidgets, especially certain VINT devices or hubs, require external power to operate correctly. A lack of power can lead to 'Device Not Attached' or 'Timed Out' errors.
- gotcha The Phidget Network Server (used for remote Phidgets) can be blocked by firewalls, preventing Python applications from connecting to remote devices or discovering them.
Install
-
pip install phidget22
Imports
- *
from Phidgets.Phidget import *
from Phidget22.Phidget import *
- TemperatureSensor
from Phidget22.Devices.TemperatureSensor import TemperatureSensor
Quickstart
import time
from Phidget22.Phidget import *
from Phidget22.Devices.TemperatureSensor import *
def onAttachHandler(self):
print("Attach Event: " + self.getDeviceName())
def onDetachHandler(self):
print("Detach Event: " + self.getDeviceName())
def onTemperatureChangeHandler(self, temperature):
print("Temperature: " + str(temperature) + " °C")
def main():
try:
# Create your Phidget channels
temperatureSensor = TemperatureSensor()
# Set event handlers
temperatureSensor.setOnAttachHandler(onAttachHandler)
temperatureSensor.setOnDetachHandler(onDetachHandler)
temperatureSensor.setOnTemperatureChangeHandler(onTemperatureChangeHandler)
# Open your Phidget and wait for attachment
print("Waiting for Phidget TemperatureSensor to be attached...")
temperatureSensor.openWaitForAttachment(5000)
print("Press Ctrl+C to Exit\n")
while(True):
time.sleep(1)
except PhidgetException as e:
print("Phidget Exception: " + str(e.code) + " - " + e.details)
except KeyboardInterrupt:
pass
finally:
try:
temperatureSensor.close()
except PhidgetException as e:
print("Error closing Phidget: " + str(e.code) + " - " + e.details)
if __name__ == '__main__':
main()