Building a Sensor Hub based on Raspberry Pi
Scientists rely on sensors to probe things that are not directly perceivable by human senses and thereby advance science. If you are interested in doing some science experiments, you can now build your own sensor hub using the Raspberry Pi platform with a relatively small budget and effort — thanks to the versatility of the incredible platform and the maturity of the supporting community. In this article, I show how to build a sensor hub using a number of breakout boards and their open-source driver code that I found online. I also show the results of some simple but intriguing experiments that you can immediately do with some of these sensors. None of these experiments require complex setups or expensive materials, but they show the true power of science experimentation unleashed by sensors that extend our perception.
Sensor breakout boards
Instead of building my own sensors from scratch, I used existing sensor breakout boards (Figure 1). There are dozens of them available online. Most of them provide a unique I²C address that allows the Raspberry Pi to communicate with them. The I²C interface is quite simple (Figure 2). For starters, it is sufficient to know that it has only four wires: SCL (serial clock), SDA (serial data), GND (ground), and VIN (voltage input). The I²C address of a sensor breakout is usually defined by the manufacturer. Breakout boards of the same type made by different manufacturers typically have the same address to ensure interoperability.
You can connect multiple breakouts to the same master through the I²C bus of the Raspberry Pi, provided that their addresses are different (Figure 3). There is a handy command line tool for finding out the I²C devices that are connected and their addresses:
sudo i2cdetect -y 1
If you don’t see the address of your sensor breakout printed on the console after executing the above command, chance is that it is not properly wired or something gets loose.
Many of these breakout modules have open-source drivers written in Python and Java available in Raspberry Pi. So you can easily get started if you are already familiar with those programming languages.
The only inconvenience of these breakout boards is that you have to solder pin headers to them on your own (there are very few sensor breakouts with pre-soldered headers). But once you go through that step, things should be relatively easy. Let’s see what some of these sensor breakouts can do for us.
Distance measurement based on time of flight
VL53L0X is a sensor breakout that emits infrared light pulses and then figures out the distance of the nearest object it is facing based on the time it receives the reflection bounced back from the object. As this mechanism is sort of like radar, the technology is called lidar.
Because the radiated light is infrared, we can’t see it (so it doesn’t bother us), but the camera of a smartphone can capture it, as shown in the following short video.
The VL53L0X sensor module is useful in measuring the distance of the closest object if it is within the range of 50–1200 mm (some websites claim that it can detect objects up to two meters away). As an application, I tested if it could be used to capture the swing of a pendulum. The result was positive (see Figure 4). The following video shows my setup and experiment.
If you are wondering why I taped a card to the weight of the pendulum, the reason is because the cross-section of the weight was too small to be discerned by the sensor. The downside of enlarging the reflection area using this method is that it significantly increased the air drag, which caused the pendulum to come to a stop sooner (as you can see in Figure 5). The data was logged and graphed using the IoT Workbench software that I am developing, which has a Java version that runs on the Raspberry Pi and a Web-based version that runs everywhere. A graph is what tells the story about the data — a picture is worth 1,000 words. So a sensor hub should typically have a dashboard for processing and graphing the incoming data.
Barometric pressure, temperature, and relative humidity
The BME280 is a module that measures barometric pressure, temperature, and relative humidity. One thing that you can experiment with the temperature sensor is to touch it with a finger briefly and observe how it warms and cools. Note that when you touch the breakout board, you should avoid touching its pins, otherwise you may short-circuit the module by accident and cause it to fail — remember that your finger is not only a thermal conductor, but also an electrical conductor. After you remove your finger, you should see Newton’s Law of Cooling in action, which shows a smooth exponential decrease of the temperature as the sensor cools off (Figure 6).
Most temperature sensors that I have used exhibit similar behavior. The slow equilibration of a temperature sensor with the air may be a problem in some cases. This is something we should bear in mind when using a temperature sensor — the reading is accurate only when it is in equilibrium with the object it measures.
I had a lot of fun with the humidity sensor. It turned out that this sensor is very sensitive, allowing one to detect invisible, minuscule water vapors around us. For example, when I put it above a cup of water, a spike immediately appeared in the humidity graph (Figure 7). A smaller spike popped even when I merely put my finger under the sensor — without touching it at all, revealing that human skin is constantly losing water molecules. It may even be able to detect the water molecules from a plant leaf due to transpiration.
Compared with its companion sensors, the barometric pressure sensor on board of BME280 is a little boring (Figure 8) because we can’t change the air pressure (unless we walk up the stairs of a high-rise building). I used an electric fan to blow air towards the sensor to see if that would increase the pressure a bit, but it didn’t.
The TSL2561 is a sensor module that measures the illuminance, or the luminous flux per unit area, of both visible light and infrared light. As it has both visible and infrared capabilities, one of the things that you can do with this module is to compare the results between an incandescence light and a LED light. The graph immediately tells which one is more energy-efficient in terms of converting electricity into visible light for illumination. Figure 9 shows that an incandescence light emitted more infrared light than visible light, whereas a LED light did the opposite (I used a small LED light connected to my Raspberry Pi for a quick test but I believe a larger LED lamp would behave the same). The invisible infrared light of a lamp is a waste of energy, especially in the summer when it works against your thermal comfort.
And of course, there is the three-axis accelerometer that can measure the gravitational acceleration. This is the sensor that your smartphone uses to detect its orientation. Figure 10 shows the results recorded using the LIS3DH module when I rotated it.
In conclusion, with a variety of sensor breakouts for the Raspberry Pi computer, we can easily create a small science lab that empowers us to visualize and investigate things around us that would otherwise go unnoticed. Perhaps this is one of the most compelling reasons to learn how to use the Raspberry Pi. It really opens a new dimension of exploration for a lot of people who are interested in science.