Tutorial: Using Differential Pressure and Flow Sensors with Raspberry Pi
In this tutorial we show how to connect Sensirion's differential pressure sensors SDP5xx and flow sensors SFM3xxx to Raspberry Pi.
Both, the SFM3xxx and SDP6xx have an I2C interface. We use the Sensirion sfxx Linux kernel driver to access them via Raspberry Pi. This driver has to be compiled as a module for Raspberry, which usually includes recompiling the complete kernel. Here we use a method that only requires compiling the specific modules and can be run entirely on Raspberry Pi.
In the end, we will be able to read out sensor values from a Python application and display them on the console.
You will need
- A Raspberry Pi. This tutorial was tested with the Pi 3, Pi 2 Model B and the Model B+.
- An SD card with a current version of the Raspbian operating system. We used the version from 2016-03-18. Make sure you have at least 1Gb of free space. This means you might have to expand the file system using raspi-config.
- A Sensirion flow or differential pressure sensor. In this tutorial we connect one SFM3000 to the I2C port 0 and one SDP631 to the I2C port 1. However, any compatible sensor with an I2C interface should work.
- Wires and pull up resistors to connect the sensors to Raspberry Pi
- An internet connection on Raspberry Pi. We will download the source code and install packages directly on the Pi.
2. Hardware Assembly
Connect the sensors to Raspberry Pi, this is easiest done using some jumper cables. Take care to chose the correct supply voltage for your sensor.
|Signal||Pin on SFM3xxx||Pin on Raspberry Pi|
|Signal||Pin on SDP6xx||Pin on Raspberry Pi|
3. Software Setup
All of the following commands run on console on Raspberry Pi. In general, there are two methods to run a shell:
- Connect a monitor and a keyboard to Raspberry Pi before turning it on.
- Set up the networking on Raspberry Pi and connect over the network using ssh.
By default the I2C buses on Raspberry Pi are disabled. If you already enabled the bus you need (e.g. using raspi-config) you can skip this step.
echo dtparam=i2c_arm=on | sudo tee -a /boot/config.txt sudo reboot
The first two commands will configure the Raspberry Pi to use the I2C busses 0 and 1 on the GPIOs 27 and 28, respectively 2 and 3.
The last command will reboot the system.
Install Linux Kernel Headers
Because Raspian doesn't follow the Debian standard for packaging kernels and the corresponding headers, we use a different method to install them. This is necessary to compile modules which should be loaded into the kernel.
sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source && sudo chmod +x /usr/bin/rpi-source && /usr/bin/rpi-source -q --tag-update sudo apt-get install libncurses5-dev rpi-source
The first command installs the rpi-source tool from Github.
The second command installs a dependency of the kernel build process.
The last command will download the source code for the kernel version you are running. This step will take a while, depending on your connection speed.
Download and Compile the Driver
Clone the driver from GitHub and compile it.
mkdir drivers cd drivers/ git clone https://github.com/Sensirion/sfxx.git git clone https://github.com/Sensirion/crc8.git echo obj-y := crc8/ sfxx/ > Makefile make -C ~/linux M=$PWD
The sfxx driver depends on the crc8 module, which is part of the mainline Linux kernel. However, it is currently not included in the Raspbian images. That's why we have to download it as a separate module and compile it together with the sfxx module.
More information about using symbols from another external module can be found on https://www.kernel.org/doc/Documentation/kbuild/modules.txt, section 6.3.
Load and Instantiate Driver
Load the modules and instantiate the driver.
sudo insmod crc8/crc8.ko sudo insmod sfxx/sfxx.ko echo sfxx 0x40 | sudo tee /sys/bus/i2c/devices/i2c-1/new_device
The first two commands load the driver modules into the kernel.
The last command probes for the sensors on the I2C bus 1.
Read out Values
Read out values using the libsensors-python by Sensirion. For more advanced usage of the library, such as streaming the data over the network using MQTT, consult the .readme file and the different example scripts.
cd ~ git clone https://github.com/Sensirion/libsensors-python.git libsensors-python/sync_example.py
The last command should give you a live output of the measured values to the console.
timestamp[s] SFxxSensor[sl/min] SFxxSensor[Pa] 0.001 s 0.000 sl/min -0.017 Pa 0.101 s -0.028 sl/min 0.000 Pa 0.201 s -0.028 sl/min 0.000 Pa 0.301 s 0.000 sl/min 0.000 Pa 0.401 s 0.057 sl/min 0.000 Pa 0.501 s -0.057 sl/min 0.000 Pa 0.601 s -0.028 sl/min 0.000 Pa 0.701 s 0.028 sl/min 0.000 Pa
Second I2C bus
If you need to connect more than one sensor, you will need to connect them to different I2C buses, because all sensors have the same I2C address. There are two ways of doing that:
- Use the bus 0 on pins 27 and 28. This is not recommended but works on versions of the Raspberry Pi prior to 3. You will need to set dtparam=i2c0=on in your boot config and install your own pull-up resistors.
- Use any two GPIOs to emulate an I2C bus. See the i2c-gpio-param module to configure such buses dynamically. Again, you will need to install pull-up resistors to VCC.