Skip to content

Instantly share code, notes, and snippets.

@fisher0251
Created January 17, 2015 04:54
Show Gist options
  • Save fisher0251/4146446291da0cc3a234 to your computer and use it in GitHub Desktop.
Save fisher0251/4146446291da0cc3a234 to your computer and use it in GitHub Desktop.
Shows how to use the Adafruit library to read pressure and altitude from the BMP180
# sensorexample.py
# Shows how to use the Adafruit library to read pressure and
# altitude from the BMP180
import Adafruit_BMP.BMP085 as BMP085
import time
# This starts the pressure sensor
sensor = BMP085.BMP085()
# This reads the atmospheric pressure in pascals (Pa) when the program starts
starting_pressure = sensor.read_pressure()
# This calculates the altitude compared to where the program was started
relative_altitude = sensor.read_altitude(starting_pressure)
# This loop reads the pressure and calculates the relative altitude
# Notice the {} string formatting so that only two decimal places are printed
while 1:
current_pressure = sensor.read_pressure()
relative_altitude = sensor.read_altitude(starting_pressure)
print 'Pressure = {0:0.2f} Pa'.format(current_pressure)
print 'Relative Altitude = {0:0.2f} m\n\r'.format(relative_altitude)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment