Sparkfun: BMP085 Barometric Pressure Sensor

From eLinux.org
Revision as of 00:04, 17 November 2012 by Jessebrannon (Talk | contribs)

Jump to: navigation, search


Overview:  1, show a picture of the device that shows what pins it has.
Wiring:   0, Give a specific example of how to wire it.  What pins go where?  Which bone header are you using?  P8, P9?
Code:     2
git:    0 put in git
Demo:     0
Total:    3/10
Comments:  More details are needed.  I think someone else in the class would have trouble
reproducing what you have done.

I've not seen echo bmp085 0x77 > /sys/class/i2c-adapter/i2c-3/new_device, could you give more details about what it does?

Introduction

The BMP085 Barometric Pressure Sensor is a sensor that can measure the atmospheric pressure as well as the temperature at its location. It communicates with host devices via I2C.

Image of the breakout board.


Connecting the Hardware

To connect the BMP085 to a BeagleBone, first supply the Vdd and GND from the Beagle to the BMP085 Breakout Board. Then connect SDA and SCLK from the BMP085 to one of the I2C bus pins on the Beagle. The XCLR and EOC pins do not have to be connected to the BMP085. If I2C bus 3 is used, the connections are as follows:

Signal | BeagleBone Pin     | BMP085 Pin

VCC    | 3 or 4 (P9 Header) | 1
GND    | 1 or 2 (P9 Header) | 2
SCL    | 19 (P9 Header)     | 5
SDA    | 20 (P9 Header)     | 6


Reading the Pressure and Temperature from the Command Line

The Angstrom distribution for the BeagleBone comes with a driver for the BMP085 already installed. This makes interfacing to the sensor easy.

First, you need to find the I2C address of the device. Instructions on how to do this can be found here. My BMP085 showed up at I2C address 0x77. To initilialize that sensor, type the following in your terminal:

echo bmp085 0x77 > /sys/class/i2c-adapter/i2c-3/new_device

We are simply telling the BeagleBone that a BMP085 device is located at I2C location 0x77. It will then initialize the driver and interface to the device.

To check if this was successful, enter:

dmesg | grep bmp

You should see:

[10420.903490] i2c i2c-3: new_device: Instantiated device bmp085 at 0x77
[10420.927608] bmp085 3-0077: BMP085 ver. 2.0 found.
[10420.927659] bmp085 3-0077: Successfully initialized bmp085!

Congratulations! You're sensor is connected and ready to read the temperature and pressure at its location.

To read the temperature (in degrees C), type:

echo scale=1 \; $(cat /sys/bus/i2c/drivers/bmp085/3-0077/temp0_input) / 10 | bc

To read the pressure (in millibars), type:

echo scale=2 \; $(cat /sys/bus/i2c/drivers/bmp085/3-0077/pressure0_input) / 100 | bc

Reading the Temperature and Pressure Programmatically

I wrote some simple code that will read the temperature and pressure every 2 seconds and display them on the command line. The code is shown below and is also in my Git Repository.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#define MAX_BUF 64

int main(int argc, char *argv){
	FILE *fp;
	char path[MAX_BUF];
       snprintf(path,sizeof path, "/sys/class/i2c-adapter/i2c-3/new_device");
	if((fp = fopen(path,"w")) == NULL){
		printf("file open failed");
		return 1;
	        }
       rewind(fp);
       fprintf(fp, "bmp085 0x77\n");
       fflush(fp);
       fclose(fp);
       while(1){
              sleep(2);
              char buf[MAX_BUF];

              snprintf(path, sizeof path, "/sys/bus/i2c/drivers/bmp085/3-0077/temp0_input");
              if((fp = fopen(path, "r")) == NULL){
	            printf("cannot read device");
	            return 1;
	            }
              if((fgets(buf, MAX_BUF, fp)) == NULL){
	            printf("cannot read device");
	           }
              fclose(fp);
              float temp = atoi(buf);
              float tempd = temp / 10;
              printf("Current Temperature: %f\n",tempd);

             snprintf(path, sizeof path, "/sys/bus/i2c/drivers/bmp085/3-0077/pressure0_input");
             if((fp = fopen(path, "r")) == NULL){
	          printf("cannot read");
	          return 1;
	          }
             if((fgets(buf, MAX_BUF, fp)) == NULL){
	          printf("cannot read");
	          }
             fclose(fp);
             float pressure = atoi(buf);
             float pressured = pressure / 100;
             printf("Current Pressure: %f\n\n",pressured);
     } 
}