Raspberry Pi and I2C Air Pressure Sensor BMP180

Raspberry projects

Raspberry Pi and I2C Air Pressure Sensor BMP180

Messaggio da leggereda root » 10/01/2019, 10:03

Immagine

Immagine

I²C Activation

Since the BMP180 sensor works via the I²C bus, it must be enabled first, because it is deactivated by default (if you have already done so, you can jump to the next chapter).

We first process the following file:

sudo nano /etc/modules

At the end of the file these two lines are added:

i2c-bcm2708
i2c-dev


With CTRL + O and CTRL + X we save and exit.
Now we can comment out the blacklist entries (in some versions the file does not exist, then just go to the next command):

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

Here we can use a hash # to comment out the corresponding entries.

#blacklist spi-bcm2708
#blacklist i2c-bcm2708


In addition, Raspbian versions from 2015 onwards require activation of the Devietree. This can be done in the configuration:

sudo raspi-config

Under “Advanced Options” you activate the automatic loading of the I2C bus. Restart your Raspberry.

Now we install three more needed tools:

sudo apt-get install python-smbus i2c-tools git

Then you can by means of

i2cdetect -y 1

test if everything worked. A device should be displayed. For me it looks like this:

pi@raspberrypi ~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

BMP180 Software

The easiest way to address the sensor is to take an existing library that already has all the functions. Adafruit offers such a library, with examples also to other sensors.

We clone the repository of Adafruit (git must be installed).

git clone https://github.com/adafruit/Adafruit-Ra ... n-Code.git
cd Adafruit-Raspberry-Pi-Python-Code/
git checkout 9ff733d59242a02f7ccd0222001ce80f6090a978
cd Adafruit_BMP085


Then we can already do the first test:

sudo python Adafruit_BMP085_example.py


The script will provide an output similar to this one:

Temperature: 25.70 C
Pressure: 1007.08 hPa
Altitude: 50.83
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 453
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Raspberry Pi and I2C Air Pressure Sensor BMP180

Messaggio da leggereda root » 10/01/2019, 10:06

Of course, we can now integrate the file into another project. For this we only copy the file to be included and edit the just called Adafruit_BMP085_example.py, which looks like this:

Codice: Seleziona tutto
#!/usr/bin/python
 
from Adafruit_BMP085 import BMP085
 
# ===========================================================================
# Example Code
# ===========================================================================
 
# Initialise the BMP085 and use STANDARD mode (default value)
# bmp = BMP085(0x77, debug=True)
bmp = BMP085(0x77)
 
# To specify a different operating mode, uncomment one of the following:
# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
# bmp = BMP085(0x77, 1)  # STANDARD Mode
# bmp = BMP085(0x77, 2)  # HIRES Mode
# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode
 
temp = bmp.readTemperature()
 
# Read the current barometric pressure level
pressure = bmp.readPressure()
 
# To calculate altitude based on an estimated mean sea level pressure
# (1013.25 hPa) call the function as follows, but this won't be very accurate
altitude = bmp.readAltitude()
 
# To specify a more accurate altitude, enter the correct mean sea level
# pressure level.  For example, if the current pressure level is 1023.50 hPa
# enter 102350 since we include two decimal places in the integer value
# altitude = bmp.readAltitude(102350)
 
print "Temperature: %.2f C" % temp
print "Pressure:    %.2f hPa" % (pressure / 100.0)
print "Altitude:    %.2f" % altitude


As you can see, there are several modes (Ultralowpower, etc) that you can specify. You are now free to customize the file to your liking and/or change.
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 453
Iscritto il: 10/04/2012, 10:55
Località: Turin


Torna a Raspberry

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron