Lesson 31 Barometer

Share for us

Introduction

The BMP180 barometer is the new digital barometric pressure sensor, with a very high performance, which enables applications in advanced mobile devices, such as smart phones, tablets and sports devices. It complies with the BMP085 but boasts many improvements, like a smaller size and more digital interfaces.

Components

– 1 * Raspberry Pi

– 1 * Breadboard

– 1 * Network cable (or USB wireless network adapter)

– 1 * Barometer module

– 1 * 4-Pin anti-reverse cable

Experimental Principle

Use a barometer to measure air pressure and temperature.

The schematic diagram:

Experimental Procedures

Step 1: Build the circuit

Raspberry PiBarometer Module
SCLSCL
SDASDA
5VVCC
GNDGND

Step 2: Setup I2C (see Appendix 1. If you have set I2C, skip this step.)

For C language users:

Step 3: Download libi2c-dev

sudo apt-get install libi2c-dev

Step 4: Change directory

cd /home/pi/SunFounder_SensorKit_for_RPi2/C/31_barometer/

Step 5: Compile

gcc barometer.c bmp180.c -lm

Step 6Run

sudo ./a.out

For Python users:

Step 3: Install smbus for I2C

sudo apt-get install python-smbus i2c-tools

Step 4: We’ll need to install some utilities for the Raspberry Pi to communicate over I2C.

sudo apt-get install build-essential python-dev python-smbus

cd ~

git clone https://github.com/adafruit/Adafruit_Python_BMP.git

cd Adafruit_Python_BMP

sudo python setup.py install

Step 5: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/

Step 6: Run

sudo python 31_barometer.py

Now you can see the temperature and pressure value displayed on the screen.

C Code

#include "bmp180.h"
#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv){
	char *i2c_device = "/dev/i2c-1";
	int address = 0x77;

	void *bmp = bmp180_init(address, i2c_device);

	if(bmp != NULL){
		int i;
		for(i = 0; i < 100; i++) {
			float t = bmp180_temperature(bmp);
			long p = bmp180_pressure(bmp);
			float alt = bmp180_altitude(bmp);
			printf("t = %f, p = %lu, a = %f\n", t, p, alt);
			usleep(2 * 1000 * 1000);
		}
		bmp180_close(bmp);
	}
	return 0;
}


Python Code

#!/usr/bin/env python
#---------------------------------------------------------
#
#		This is a program for Barometer Pressure Sensor
#	Module. It could measure the pressure and temperature.
#
#		This program depend on BMP085.py writted by 
#	Adafruit. 
#
#	   Barometer Module			   Pi
#			VCC ----------------- 3.3V
#			GND ------------------ GND
#			SCL ----------------- SCL1
#			SDA ----------------- SDA1
#
#---------------------------------------------------------
import Adafruit_BMP.BMP085 as BMP085
import time

def setup():
	print '\n Barometer begins...'

def loop():
	while True:
		sensor = BMP085.BMP085()
		temp = sensor.read_temperature()	# Read temperature to veriable temp
		pressure = sensor.read_pressure()	# Read pressure to veriable pressure

		print ''
		print '      Temperature = {0:0.2f} C'.format(temp)		# Print temperature
		print '      Pressure = {0:0.2f} Pa'.format(pressure)	# Print pressure
		time.sleep(1)			
		print ''

def destory():
	GPIO.cleanup()				# Release resource

if __name__ == '__main__':		# Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  	# When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destory()