We’ll be using this ultra compact AE-BME280 pressure sensor (Dimension: 16x10mm) in this tutorial. I purchased it from a Japanese parts supplier Akizuki Denshi but you can also use Adafruit BME280 which has the same chip. Just note that the pin layout is slightly different on the Adafruit so please make sure you refer to their datasheet for wiring.
The majority of the work is done by the BME280, a tiny, silver chip in the center of the component. It has a very small opening on the surface that is used to read values so you have to be careful not to cover this up.
When I bought it, the AE-BME280 board and the pin headers came disassembled. The easiest way to connect this to the Raspberry Pi is to put it together as shown in Figure 1. This requires some soldering in order to assemble. The pin header I purchased had 10 pins but only 6 pins were required to connect to AE-BME280, so it was trimmed after the 6th pin.
You can choose either I2C or SPI communication. Since I’ve done SPI before, I’m going to try I2C this time.
I2C – Wikipedia I2C (Inter-Integrated Circuit), is a serialbus invented by Philips Semiconductor (now NXP Semiconductors). I2C stands for I-square-C. Due to the character limitations in plain text environments, it is referred to as I2C or IIC. It is typically used for attaching low-speed peripheral ICs to things like motherboards, embedded systems, and mobile phones.
Figure 2: Pin numbers and functions – AE-BME280 datasheet
As indicated in Figure 2, we need to solder J3 when using I2C. We have to set it by filling with solder. Note, this only applies if you’re using the AE-BME280 chip. The Adafruit chip doesn’t require this jumper to be set.
Figure 3: Soldering Equipment
Now, it’s time to solder! First, let’s heat up the soldering iron.
Solder – Wikipedia Solder is an alloy composed mainly of lead and tin that is used with soldering irons. It is used for things like joining metal components and mounting electrical components to circuit boards with electrical circuits. Depending on its composition, it becomes a superconductor at 4-10 degrees.
This is the solder that I used in this project. It’s very soft and pliable and can leak easily so it’s important to only squeeze out little by little. It’s useful to have a desoldering wick ready in case you mess up. If you apply too much, it will wick up the extra solder so be careful!
Figure 4: Soldering J3
First, we’ll solder the J3 jumper indicated on Figure 4. J3 is close to its neighboring pins so be careful not to connect them. Fill in very carefully!
Figure 5: Soldering pin header
The next step is connecting up the pin header. I soldered this from the backside. The gaps between the pins are very small so it was quite difficult to fill them in. Figure 5 is an example of what happens when the solder gets lumpy on both ends.
The trick is to put the soldering iron tip to the pin, warm it up a little, and then lightly apply the solder. If the tip gets too hot, the solder can burn and form lumps. So, I recommend unplugging the soldering iron from the power outlet and cool it down before applying again. There is no need to rush here. Be careful not to connect the pins. Solder slowly!
Figure 6: AE-BME280 connected to breadboard
Done! The pin header is attached and it’s sitting vertically on the breadboard.
When soldering, I accidentally touched the pin header tip and burned it a little bit but thankfully it didn’t affect reading values at all.
Now, let’s wire the Raspberry Pi to the AE-BME280. It should look something like Figure 7 since we’re using I2C.
On the Raspberry Pi, “SDA” connects to GPIO2 (pin 3) and “SCL” connects to GPIO3 (pin 5). VDD connects to pin 1 so these line up in the first 3 GPIO pins (refer to Figure 8). It’s easy to remember when wiring them.
Figure 8: Wiring diagram
I connected pin 5 (SDO) on the AE-BME280 to GND but you can also connect it to VDD. (Please note that the data collection address changes if you do it this way).
Setting up the Raspberry Pi
Initially, the Raspberry Pi has I2C disabled. The way to enable it is the same as enabling SPI described in previous tutorial: Raspberry Pi WebIOPi IOT, Programming Analog Input. From the menu, select [Preferences]-[Raspberry Pi Configuration], and open the Settings screen.
Click the [Interfaces] tab and set [I2C] to [Enable].
In order to enable the setting, the system must be rebooted so click “Yes” on the prompt. After the reboot, I2C transmission will be enabled.
(OS: Confirmed on the 11/21/2015 release version of Raspbian Jessie)
Next, let’s install the packages necessary to use I2C. We need [i2c-tools] to use it from the command line and [python-smbus] to use it in Python.
If you run the command “i2cdetect”, it will detect all devices connected via I2C.
sudo i2cdetect -y 1
76 is an hexadecimal number; it is represented as 0x76.
Note: When selecting the I2C address, it defaults to [0x76] if the pin 5 on the circuit board (SDO) is connected to GND and [0x77] if connected to VDD.
I connected SDO to GND so 0x76 is displayed, but it would show 0x77 if connected to VDD.
Also, it appears that the last parameter in the i2Cdetect command varies based on the version of Raspberry Pi you are using. Revision 1 (Raspberry Pi Model B shipped prior to 10/14/2012) uses 0 but Revision 2 specifies using 1. I’m using a Raspberry Pi 2 Model B so a parameter value of 1 worked.
When I tried running it, this error occurred:
Following the error, I searched in the dev directory and saw that there was a file named [i2c-1] rather than [i2c-0]. I wasn’t able to open the file but I’m assuming it contains the recorded measurements.
sudo i2cdump -y 1 0x76
The above picture shows what happens when I use the i2Cdump command to output the register values. It looks like all sorts of values were read, but I wasn’t able to tell which values came from where and for what just from looking at this.
It was hard to do conversions/calculations on these values, so I borrowed some Python source code from Switch Science’s repository:
Click on the “Download ZIP” link on the upper right and put the “bme_280_sample.py” file from the Python27 folder in the appropriate location on your Raspberry Pi. Note: this program requires the “python-smbus” package in order to run.
Once preparations are complete, run the program! Once you run it successfully, you will see temperature, pressure, and humidity shown as outputs in three lines.
Note: you have to have root access to run the smbus package. As always, I tried running it with PHP and got an error so I gave up. There are a lot of complex calculations in the bme_280_sample.py source code, but I managed to modify the output part even with minimal experience in Python. The portion of code that I customized is as follows:
I didn’t modify the codes too much. I just made some minor adjustments to the main “processing” section. I changed “print” to “return” and made edits so it would return values with commas in CSV format.
I created another py that calls readData() function from the previously modified “bme280_custom.py.” This saves the read values in CSV file. I set it to save the date as the file name and to record a full day’s worth of data in it.
I registered this program with cron and set it up so it runs on a regular schedule. It is now complete! sudo crontab -e
0-59/10 * * * * /home/pi/bme280.py
Note: I set it to run every 10 minutes.
Next, I’ll create a PHP file to display the data so it can be easily read, similar to what I did when I made a thermometer using a DS18B20 temperature sensor.
I created a simple PHP file that displays the data in a table (you have to install the “php5” package to use PHP).
That way, I can view the contents of the CSV file by navigating to http://localhost/bme280.php from the browser. There’s a big pressure change even at 10-minute intervals!
The newest version of Raspbian comes with an office suite called “LibreOffice” pre-installed. If you simply want to look at the data, you can use “LibreOffice Calc” (Just double-click on the CSV file) and you will see something like this:
Today, we created a simple module using AE-BME280 sensor to measure multiple values (pressure, humidity, and temperature). It’s quite amazing to see this small sensor measuring up to three different parameters. It’s tiny, yet very powerful!
This project also allowed me to improve my soldering skills. The pins on AE-BME280 were very small and they were so closely located, and I was worried I might accidently connect them all. I was so happy to see the finished product with pins nicely soldered. Projects requiring soldering can be difficult but very rewarding at the same time!