HOWTO: Get Phidgets working with FreeBSD



Reaction score: 20
Messages: 43

HOWTO: Get Phidgets working with FreeBSD
What are "Phidgets"?
Phidgets are a set of user friendly building blocks for low cost USB sensing and control from your PC.

The purpose of this HOWTO is to demonstrate how to setup phidgets to work with FreeBSD. The target audience for this article is embedded developers who are looking to do / are currently doing hardware/software projects. (E.x. Drones, Robots, Single Application Computer, etc) In essence, if you are thinking of trying something with Rasberry Pi, Aurdino, ARM, small PC, etc then this article is for you.

  • You possess a Phidget device.
  • You know how to use 'vi'.
  • You are a beginner.
  • You have a FreeBSD computer and a Mac (or UNIX-like) computer.
  • You are trying this out on a FreeBSD i386 or amd64 (I haven't tried with ARM so I cannot confirm!)

On your FreeBSD Computer
(Note: Your Robot Computer)
Go to ports and install "libusb" driver
# cd /usr/ports/devel/libusb
# make install clean

On your Apple Macintosh Computer
i) Go to
ii) Download "Phidget Libraries" tar and unzip in your ~/Downloads folder.
iii) SCP the library to your machine.
# scp ~/Downloads/libphidget_2.1.8.20130320.tar [email=admin@]admin@[/email]:/tmp

admin = is the username I used to access the server = is the server IP address of your FreeBSD computer

On your FreeBSD Computer
Make yourself root
<Note: Insert your password >

Then we'll traverse to the directory where we uploaded the tarball and install the contents.
# cd /tmp
# tar -zxvf libphidget_2.1.8.20130320.tar

z -- unzip
x -- extract the file
v -- verbose
f -- forcefully done

Alright! Let's go into the Phidget Library (which is written in C) and install it!
# cd /tmp/libphidget-
# ./configure
# make
# make install

4) Confirm everything is working.
i) Create a file called "HelloWorld.c"
mkdir /tmp/test
cd /tmp/test
touch /tmp/HelloWorld.c

ii) Populate the "HelloWorld.c"
# vi /tmp/HelloWorld.c"

with the following contents:
 * Phidget Hello World Program for all devices
 * (c) Phidgets 2012

#include <stdio.h>
#include <phidget21.h>

// -------------------- Event Functions ---------------------------------------

int CCONV AttachHandler (CPhidgetHandle device, void *userptr) {

    int serialNumber;
    const char *name;
        CPhidget_getDeviceName(device, &name));
        CPhidget_getSerialNumber(device, &serialNumber));
    printf("Hello Device %s, Serial Number: %d\n", name, serialNumber);
    return 0;

int CCONV DetachHandler (CPhidgetHandle device, void *userptr) {

    int serialNumber;
    const char *name;
        CPhidget_getDeviceName(device, &name));
        CPhidget_getSerialNumber(device, &serialNumber));
    printf("Goodbye Device %s, Serial Number: %d\n", name, serialNumber);
    return 0;

// When using an error handler with the manager, it takes a
// CPhidgetManagerHandle, when using an individual object, the
// object serves as its own handle.
int CCONV LibraryErrorHandler (CPhidgetManagerHandle device, void *usrptr, 
                    int errorCode, const char *errorDescription) {
    printf("Error Event: %d - %s\n", errorCode, errorDescription);
    return 0;

// This error handler can handle any CPhidget function that returns an int
int LocalErrorCatcher (int errorCode) {
	const char *errorDescription;
	// If the error code is 0, everything is okay
    if (errorCode != 0) {
        // Otherwise, you can print specific messages or perform actions by error value.
        switch (errorCode) {
               printf("Error: An error occurred with code %d.\n", errorCode);
                   CPhidget_getErrorDescription (errorCode, &errorDescription));
               printf("The description for this error is: %s\n", errorDescription);
	return 0;

// -------------------- Main Code ---------------------------------------------

int main(int argc, char* argv[]) {

    int result; 
    const char *err;

    CPhidgetManagerHandle device = 0;
        CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle) device, 
                                         AttachHandler, NULL));
        CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle ) device,
                                         DetachHandler, NULL));
        CPhidgetManager_set_OnError_Handler((CPhidgetManagerHandle) device,
                                        LibraryErrorHandler, NULL));
    // Most opening and closing would be via a cast to
    // (CPhidgetHandle), however, this manager has its
    // own handle struct to cast to.
        CPhidgetManager_open((CPhidgetManagerHandle) device));
    printf("Phidget Simple Playground (plug and unplug devices)\n");
    printf("Press Enter to end anytime...\n");
        CPhidgetManager_close((CPhidgetManagerHandle) device));
        CPhidgetManager_delete((CPhidgetManagerHandle) device));

    return 0;
Note: I did not write this file, it was written by Phidgets and not myself!

iii) Run:
# gcc HelloWorld.c -o HelloWorld -lphidget21
# ./HelloWorld

Note: The application will run and ask you to plug into the FreeBSD computer a single Phidget device. Do this, if everythign works, the application will print the device name that is connected and exit - if this happens then everything is working! Congratulations! :beergrin

5) Trouble?

6) Next?
  • If you want to create an application in a different language (C++, Java, Objective-C, etc, etc) for any of the devices. Just look at this link where all examples & various languages exist.
  • If you're looking to build some-sort of hardware server, like for example a weather station with FreeBSD, now you can. The key idea is that whatever you can do on Linux, you can do on FreeBSD as well.