Phidget22 Python Wrapper

1.25.20260408 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart initializes a `TemperatureSensor` object, registers event handlers for attachment, detachment, and temperature changes, and then waits for a sensor to be connected. It prints temperature readings to the console as they occur. The program includes basic error handling and a clean shutdown.

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()

view raw JSON →