segunda-feira, dezembro 10, 2012

How to set up a Barometric pressure sensor BMP085 on Raspberry Pi with Raspbian

Works with Raspbian hf Aug/Sept 2012

Under Pressure

From previous blog posts, you’ll know I have a Raspberry Pi set up to read two temperature sensors and two light sensors (inside and outside) and log the data online at COSM

Setting up temperature sensors and COSM feed

But, as ever, “we want more than that!” So I thought it would be fun to add a barometric pressure sensor. Looking around, it seemed like the BMP085 was a good bet. Even better than that, Adafruit have written Python libraries for it and some setup instructions here.

Their instructions are great – perfectly tailored for their Occidentalis Linux distro. I use Raspbian hf, so I’m detailing instructions for that. They’re very similar, but a couple of subtle differences. So, first we’ll start with the software…

sudo apt-get update

sudo apt-get install python-smbus

y to confirm

This installed i2-tools as well, which we’ll use a little later. Smile

How to Enable i2c in Raspbian

Then it was necessary to make a tweak to enable i2c in raspbian. A quick google of how to enable i2c in raspbian revealed this page from S K Pang (I bought my Pi Cobbler from S K Pang). Here’s what I did…

sudo nano /etc/modprobe.d/raspi-blacklist.conf

…and comment out line 3 (the one with i2c in) with a # at the start of the line…




commented out the i2c line (3)


sudo nano /etc/modules

you need to add…

i2c-dev on the last line

Add i2c-dev on last line




Now we’re going to add the user pi to the group i2c.

sudo adduser pi i2c

adding pi to i2c group

Now we need to reboot to activate the new settings. Or if your sensor is not yet connected, you could shut down and connect it while the Pi is powered down. (Do disconnect the power after shutting down) Smile

sudo reboot to reboot, or sudo halt to shut down.

Hooking it all up

The board my sensor came on has eight pins. Only four of them are used here. Your board may be different, depending on where you source it from. Here’s a shot of mine…

  1. GND goes to Ground on the Pi

  2. 3.3 goes to 3V3 on the Pi

  3. SDA goes to SDA on the Pi (Rev 1 GPIO 0|Rev 2 GPIO 2)

  4. SCL goes to SCL on the Pi (Rev 1 GPIO 1|Rev 2 GPIO 3)

Login and try it out

When it comes back up, log in as pi and type

i2cdetect -y 0 (change 0 to 1 if you have a Rev. 2 Pi)

If your sensor or other i2c device is connected correctly, you’ll get an indication of its address.

My sensor is at 77

Adding pi to the i2c user group means we no longer need to use sudo for i2c access commands.

Now we need to install some software from the lovely guys at Adafruit. We use git for this. If you don’t have git installed, install that first with sudo apt-get install git. Then,

git clone

cd Adafruit-Raspberry-Pi-Python-Code

cd Adafruit_BMP085

Then you can test out your sensor with…


And if you did everything correctly, you should be rewarded with three readings; temperature, pressure and altitude…

It works Smile

I was elated. This worked straight away for me and the whole thing took about 15-20 minutes. Smile

Let’s go COSMic

I’m not going to fully detail this part, but essentially I took the parts I needed from the script and libraries…

This needs to be near the top of your python script

from Adafruit_BMP085 import BMP085

bmp = BMP085(0x77)
set up resolution mode of sensor

And these files need to be in same directory as your script

And these function calls will get you your data

temp = bmp.readTemperature()

pressure = bmp.readPressure()

altitude = bmp.readAltitude()

Then I incorporated the bits I needed into my weather station Pi’s Python code, to log the new sensor’s data in my COSM feed…

PeterO from the Milton Keynes Raspberry Jam forum pointed out the trace could do with some low pass filtering, to make it a bit less noisy. So I tweaked my script to give a weighted average of the last five readings. It’s a little smoother. Smile Could probably be further improved. I may well tweak it some more.

If you install a BMP085, I hope yours works out as smoothly as mine did. Smile


Sem comentários:


Related Posts Widget for Blogs by LinkWithin