The Startup
Published in

The Startup

DIY IoT Project in Python — Raspberry Pi + Phidget Sensor

1. Introduction

Internet of Things (IoT) is not a new paradigm any more, rather it’s even an old school now. Anyone can easily find all kinds of IoT-related products such as voice assistants, single board computers (SBC), sensors, smart bulbs, on the internet. This applies not just to hardware platforms. You can find so many software or cloud platforms to facilitate IoT projects with regard to connectivity, data analysis, and so on. Using these public IoT platforms, anyone can develop their own IoT environments if they are willing to. (And of course if they can afford it.)

In this article, I want to talk about how to enable environment sensing by using IoT products in a DIY manner. Of course there exist tons of high-quality and fancy sensing platforms including commercial wireless sensors and dedicated smart phone apps. However, they are mostly expensive and provide limited control over the sensor data. If you are capable of programming and software development, it should be a better choice to make your own sensors and play with the sensed data. And most importantly, it gives you more fun!

For this DIY project, I decided to use Raspberry Pi and Phidget wired sensors. Raspberry Pi board has been considered a standard SBC for numerous IoT projects thanks to its low price, powerful spec, and handy peripherals. Arduino is another lingua franca to IoT developers, but its general purpose OS (i.e., Linux) support makes Raspberry Pi more powerful choice to high-level language programmers. Phidget ( is a wired sensor/actuator platform which provides developers and learners with well-packaged hardware modules and corresponding software frameworks. Pros of Phidget framework is it supports various high-level programming languages and operating systems, while its high cost compared to raw sensor modules is cons. Anyway, the combination of Raspberry pi and Phidget sensors/actuators is very popular and handy, but its development is not as simple as expected for the beginners. They will encounter more error messages than they expected in the beginning. So I want to describe a step-by-step instruction to develop a simple Python project which prints out Phidget sound sensor values in a Raspbian Linux terminal.

2. Prerequisite

Before starting the project, you should:

  • Be able to python programming
  • Be aware of basic Linux commands

I will do my best to help you make the sensor program even without any prerequisite knowledge of python and linux, but it will be much better to have knowledge on them for your understanding and further extensions.

3. Hardware Setup

In this project, I will use three types of hardwares:

  • Phidget Sound Sensor — SND1000_0 (link)
  • Phidget VINT Hub — HUB0000_0 (link)
  • Raspberry Pi 3 (now RPi 4 is available)
Phidget Sound Sensor, Phidget VINT Hub, and Raspberry Pi Board

Since Raspberry Pi and Phidget offer easy-to-follow installation guides, I am not going to deal with the installation details in this article. Instead, I put mandatory installation references as below:

  • First, install Raspbian Linux on your Raspberry Pi board. This is the installation guide link: Installation guide. I recommend you to use Raspberry Pi Imager. It is super easy to use.
  • If you complete Raspbian installation, you need to proceed initial OS setup. I recommend you to go through this initial setup using GUI since we didn’t jump into the ‘developer mode’ yet and it makes your life easier.
  • When you work on any DIY IoT projects with Raspberry Pi, it is strongly recommended to remotely access to the Linux terminal using SSH. Otherwise you need to bring your keyboard, mouse, and heavy monitor whenever your program encounters even a simple error. To do so, you need to complete two basic setups: automatic WiFi connection and SSH activation.
  • Automatic WiFi Connection — It will be annoying if your Raspberry Pi connects to a random access point whenever it is rebooted. Or, even though it is always connected to your intended access point, it will be annoying if the IP address changes every time. To avoid these issues and help your SSH connection stick to a fixed known IP address, please visit this site and setup your configuration.
  • SSH activation — Raspbian deactivated SSH by default. To activate SSH, please visit this site and follow the instruction.
  • To play with Phidget modules in Linux, you need to install Phidget library first. To do so, please visit this site and follow the instruction.

You are all set. Now connect three hardware modules and let’s turn ourselves to a ‘programmer mode’. In my case, I connected the Phidget sound sensor to the port 0 of the Phidget VINT hub.

4. Python-Phidget Programming: Printing Sensor Values

Before writing the python program, I don’t want to be bothered by text editor-specific issues. Therefore in this project, I will use the very basic text editor, nano, to write the simple python program. With respect to python version, I will use python3.

Let’s first make a workspace for this simple project, which will be our virtual environment:

workspace creation

Next, to manage packages for this project better, let’s configure our python virtual environment using venv:

python3 -m venv .
source ./bin/activate
virtual environment setup

Now the current directory, PhidgetSoundProject, is the home directory for our virtual environment in this project and you can see corresponding files and directories are created. To activate the virtual environment, you can use source command. Now we setup our virtual environment! So all the required packages or modules will be installed and referred to in this environment, while not affecting the other system libraries.

Since we setup our virtual environment, we need to install Phidget22 module which we will import in our Python program. In the virtual environment, if you download any modules using pip or pip3, they will be downloaded to lib/python3.x/site-packages directory and that is what we need to do now:

pip3 install Phidget22
Phidget22 module installation

It’s all set, and we can start our python programming:


To develop a python Phidget sensor program, you need to understand the basic concept, channel and addressing. In Phidget programming, each sensor/actuator module is considered as a separate channel and it should be addressed correctly to extract sensor data from a specific sensor module or to transmit a command to a specific actuator module. For example, in this project, our Phidget sound sensor is a channel which can be addressed by its connected Phidget VINT hub and its corresponding port, which can be addressed by serial number and port number, respectively. We can open the channel and wait for the sound sensor to be attached to the channel so that we can start playing with the sensor in our program. When we finish the sensor reading, we can close the channel and release its assigned resources. You can see this process through the python program below:

Sound sensor reading via its channel

In this program, we print out sensed sound level in dB scale every second. After declaring the SoundSensor channel, we address it with the serial number of the Phidget VINT hub (540054) and its connected port number (0). Then, after waiting for five seconds, we read the sensor value by calling ch.getdB() method. For more details on the channel and internal mechanisms, you can visit a Phidget Programming Basics website and follow its step-by-step instruction.

One thing you need to remember before running this simple python program is, you need to run it under sudo permission. The sudo is required to access the hardware connection between Phidget modules and Raspberry pi. If you run the program without sudo, you will meet this error message:

Phidget22.PhidgetException.PhidgetException: PhidgetException 0x03 (Timed Out)
No Phidgets were detected at all. Make sure your device is attached.

Okay, then let’s run this program with sudo!

sudo python3
‘No module named xx’ Error

What happened? Did you get the same error? We installed the Phidget22 module, but it says it cannot find the module. What is wrong? This happens because we are working in a virtual environment but now we tried a system’s python3 which lives outside of our environment. To prevent this and run the right python3 in our virtual environment, you need to try:

sudo ./bin/python3
Successfully printing out sound sensor values

Yay! we succeeded!! We printed out sound sensor values every seconds and the channel is closed without an error.

But don’t you think this output is too simple and elementary, especially considering the price of Phidget hardware? Actually, you can enjoy much richer functionalities with Phidget hardware and you can see the possible functions of each hardware module here.

Phidget product page links

If you visit the website, you can see product page links. If you click the module of your interest (‘SND 1000 — Sound Phidget’ in this project), you can see the details of the module. If you click the module and move to the module’s page, you can see the ‘API’ button.

Sound Phidget page

If you click the button and select ‘Python’ as the programming language, you can see the list of functions that you can call in your python program.

API selection for Python language
Methods of Sound Phidget

Now you know that Sound Phidget is not about reading a simple digit but it can provide much more advanced functionalities! I will try an interesting method, getOctaves(), as an extra trial and finish this article.

5. Advanced Function: Octaves Visualization

As the description says, getOctaves() method returns a list of unweighted values for ten different frequency bands. Using this information, we can develop an advanced service which values different frequency bands in a different manner.

getOctaves() method description

In this section, I will visualize the sound values of the ten different frequency bands in Linux terminal using numpy and termplotlib modules. Visualization is really effective in IoT domain, especially the data can fall into time-series domain. The visualized data can give us clear intuition and further help us with its analysis. Below is the expected output of this section:

Visualized octaves values

While Linux terminal is a limited platform for data visualization, this much of data visualization can tell us how sound levels corresponding to separate frequency bands change over time effectively. Without this visualization, the output will be just a list of ten hard-to-read floating numbers.

Here is the python program code which enables such data visualization:

Python code for octaves visualization

In this code, numpy is used just for transforming the lists so that they fit to termplotlib module’s function. Before running this code, please don’t forget to install termplotlib and numpy module in our virtual environment:

pip3 install numpy
pip3 install termplotlib
Installation of termplotlib and numpy modules

If you run your python program, you will encounter multiple lines of error messages you cannot understand. That happens because our imported modules have their own dependencies on the other libraries. To remove the error message, you can install two more libraries:

sudo apt-get install libatlas-base-dev
sudo apt-get install gnuplot

If you run the python program, you will finally be able to see the visualized sound octaves in your terminal!

6. Now, play with other Phidget modules!

With this sample project, I believe that you understand how Phidget module works in python and how to retrieve other Phidget modules’ APIs. One more concern of mine is, as Phidget platform has evolved, its hardware and software classes have been also evolved, which entails annoying programming library incompatibility issues. Especially, in Phidget22 library, sensor modules fall into different classes such as SoundSensor, VoltageRatioInput, and VoltageInput. This separation is due to the hardware evolution and can confuse developers a lot. Especially, Phidget does not provide enough samples to Python programmers. If you need any samples with regard to this issue, you can visit my git repository to download a sample code in which I dealt with three additional sensor channels.

With the sensor data, you can turn your home into a senseable home, and you can develop smart functionalities on top of it.

Closing remarks

I hope this article is a helpful material for developers and learners who wanted to begin DIY IoT projects and look forward to seeing a lot of interesting advanced projects!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Heesuk Son

Heesuk Son

A computer scientist enthusiastic about enabling our surroundings smart and even autonomous! KAIST PhD, now at MIT as a Postdoc