Introduction: Getting Started With Phidgets on the Raspberry Pi
This instructable is an adaptation of one of our blog posts.
Before beginning, you’ll want to have a few things in place. You’ll need:
- A functioning Raspberry Pi (obviously).
- A separately powered USB hub, since the Raspberry Pi isn't capable of delivering enough power on the USB ports to drive most Phidgets or other USB devices that require the full 500mA.
- An internet connection either via the ethernet port or a USB wifi dongle.
- The Phidget you wish to connect.
Step 1: Install a Linux Based OS
Phidgets already come with support for Linux operating systems, and so getting started on the Raspberry Pi is really just a variant of our getting started on Linux guides; You can read a tutorial or watch a video for more background on that.
The first thing to do is ensure you have a Raspberry Pi that is functional and boots into one of the linux based distributions available for it. This means no using RISC OS for those of you who like to go against the grain. There are a couple of ways you can get the Raspberry Pi up and running; you can install one of the distribution images directly, or you can install it from the NOOBS installer. There is already ample documentation on the install process provided by each distribution and the Raspberry Pi foundation itself, so we won’t go into detail on how to complete this step. If in doubt though, use the NOOBS installer as it’s designed to be very straight forward. The rest of this guide will be based upon the Raspbian distribution, so if you do not have a preference you may wish to select that one as it will minimize any potential differences between your own experience and this guide.
Step 2: Install the Phidget Drivers
Okay, so we have a functioning version of Raspbian. We’re working from the LXDE desktop environment, though most of this guide will be completed using the terminal so the instructions apply to a console only install of Raspbian as well. We’re going to want to install some dependencies first. Start by opening the terminal (LXTerminal), then typing;
sudo apt-get install libusb-1.0-0-dev
Now we will download and install the Phidgets libraries.
tar zxvf libphidget.tar.gz
Next we’ll change into that directory. Type “cd libphidget-” then hit [TAB] to complete the line. This will take care of the long string of version numbers. You should see something like:
Press enter to go to that directory, where we’ll start building the drivers
This step will compile the libraries on the Raspberry Pi, which will take some time. Now is a great time to go get some coffee. Once that is complete, finish the installation with:
sudo make install
Step 3: Test the Drivers With the Example Code
Okay, so now it’s time to test if the Phidgets work. We’ll do this by running some of the C examples that are provided on the Phidgets website. First we need to download and unpack the examples:
tar zxvf phidget21-c-examples.tar.gz
Now we do the TAB trick again to change into the correct directory. “cd phidget21-c-” [TAB] to get something like:
Lets start with the hello world example. Compile it with gcc, remembering to link it to the Phidget libraries:
gcc HelloWorld.c -o HelloWorld -lphidget21
And then run it with super user privileges to ensure we have access to the USB ports
You should see something like the first image.
As you can see, it listed a Unipolar Stepper Controller here, though you should only see that if you happened to have one set up too. It’s more likely you’ll see an InterfaceKit if that’s what you’re getting started with, or no devices at all if you haven’t connected anything yet. We realize not everyone will have a Stepper Phidget for this, so you can substitute your own Phidget. For example, the InterfaceKit example is InterfaceKit-simple.c. Let’s see what happens if we run the Stepper-simple.c example though:
gcc Stepper-simple.c -o Stepper-simple -lphidget21
You’ll be prompted to hit a key a few times while it spins the stepper motor around in various directions, as can be seen in the second and third images.
So there you have it, we’ve successfully connected Phidgets to the Raspberry Pi and gotten them to do something real. Suppose you don’t want to use C as your programming language though. Phidgets supports plenty of other languages under Linux such as C++, C#, Python, Java and even Ruby!
Step 4: Trying Other Languages: Python
Installing any of these other languages is similar to the way we installed the Phidgets C drivers. Let’s use Python as an example since the Raspberry Pi already comes with Python installed. Go back to the home directory:
Download the Python libraries:
Because the libraries come as a zipped archive, we’ll likely need to install zip support
sudo apt-get install zip unzip
Now we can install the Python libraries:
sudo python setup.py install
And download the examples:
Lets try the HelloWorld example again, but this time in Python:
sudo python HelloWorld.py
You should see the exact same results as you did using the C example. Most of the other languages listed above will have a similar installation process, with the exact nuances detailed on each of their respective pages in our Programming Resources documentation.
The instructions to get it working with a Raspberry Pi should be identical to the general Linux instructions on each page. If you find you are having trouble installing the libraries for your language and getting the examples working then we encourage you to check our forums or contact support.