Run Phidgets On The Raspberry Pi
How to get started with phidgets on raspberry pi
IntroductionBefore 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.
Note: The code examples in this article use Phidget21 code and may not work with newer versions of the Phidget library.
Install a Linux based OSPhidgets 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 distributionsavailable 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 here. 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.
Install the Phidgets DriversOkay, 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-devNow we will download and install the Phidgets libraries.
tar zxvf libphidget.tar.gzNext 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:
cd libphidget-18.104.22.16840319/Press enter to go to that directory, where we’ll start building the drivers
makeThis 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
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.gzNow we do the TAB trick again to change into the correct directory. “cd phidget21-c-” [TAB] to get something like:
cd phidget21-c-examples-22.214.171.12440319/Lets start with the hello world example. Compile it with gcc, remembering to link it to the Phidget libraries:
gcc HelloWorld.c -o HelloWorld -lphidget21And then run it with super user privileges to ensure we have access to the USB ports:
sudo ./HelloWorldYou should see something like this: 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
sudo ./Stepper-simpleYou’ll be prompted to hit a key a few times while it spins the stepper motor around in various directions. 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!
Trying Other Languages: PythonInstalling 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:
cd ~Download the Python libraries:
wget http://www.phidgets.com/downloads/libraries/PhidgetsPython.zipBecause the libraries come as a zipped archive, we’ll likely need to install zip support
sudo apt-get install zip unzip
cd PhidgetsPython/Now we can install the Python libraries
sudo python setup.py installAnd download the examples:
cd Python/Lets try the HelloWorld example again, but this time in python:
sudo python HelloWorld.pyYou 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.