In this chapter I will explain how did I connect BMP180 Bosch sensors to RaspberryPi. This is part of my Home Automation project.
The BMP180 is the new digital barometric pressure sensor of Bosch Sensortec, with a very high performance, which enables applications in advanced mobile devices, such as smart phones, tablet PCs and sports devices. It follows the BMP085 and brings many improvements, like the smaller size and the expansion of digital interfaces.
The ultra-low power consumption down to 3 μA makes the BMP180 the leader in power saving for your mobile devices. BMP180 is also distinguished by its very stable behavior (performance) with regard to the independency of the supply voltage.
These sensors use I2C bus to communicate. 2C translates into “Inter IC”. Sometimes the bus is called IIC or I²C bus.
Most significant features include:
Note that BMP180 has 4 pins that we need to connect to RaspberryPi. First locate the following pins: 3v3(to VIN on BMP180), SDA (to SDA on BMP180), SCL(to SCL on BMP180) and Ground (to GND on BMP180) on RaspberryPi. Then connect these pins to corresponding pins on BMP180 board.
Next we need to configure I2C for RaspberryPi. To do that first you need to do some installs. Make sure that RaspberryPi has network connection then find the ip address. Then connect via SSH and use these commands:
sudo su
sudo apt-get install python-smbus i2c-tools
Check the kernel version with the following command:
# uname -a
Linux musicbox 4.0.9+ #807 PREEMPT Fri Jul 24 15:14:04 BST 2015 armv6l GNU/Linux
If kernel version is higher than 3.17 then you need to edit /boot/config.txt
and add:
dtparam=i2c1=on
dtparam=i2c_arm=on
Next we need to install/activate the kernel modules. Modify /etc/modules
file and add these 2 lines:
i2c-bcm2708
i2c-dev
Next update to ckeck if there spi and i2c modules are blacklisted. Open /etc/modprobe.d/raspi-blacklist.conf
and comment the following lines (if exists) by placeing #
in front of them:
blacklist spi-bcm2708
blacklist i2c-bcm2708
All done. Now reboot
# sudo reboot
After reboot, connect again via SSH to RaspberryPi and check if it can read BMP180 device.
# ls /dev/i2c*
/dev/i2c-1
# sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77
Note that if ls /dev/i2c*
will return /dev/i2c-0
then use "0" for i2cdetect argument.
What we can see is that we have 2 addresses listed: 0x77 and 0x39. The sensor for temperature, pressure and altitude can be accessed on 0x77 address. Lights sensor can be accessed on 0x39 address.
Next I used Adafruit Python BMP library to read the sensors (Light and BMP). I wrote a little script that outputs sensor information.
# cat read_sensor.py
#!/usr/bin/python
from libs.BMP085 import BMP085
import os
from libs.TSL2561 import TSL2561
# ===========================================================================
# Example Code
# ===========================================================================
# Init the TSL2561 sensor class (default address is 0x39)
Light = TSL2561(0x39)
# 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 mmHg" % (pressure / (100.0 * 1.33322) )
print "Altitude: %.2f m" % altitude
print "Light: %d lux" % Light.readLux(0)
Running this script will give you the following information:
root@musicbox:/opt/rcontroller/scripts/bmp_sensor# ./read_sensor.py
Temperature: 32.10 C
Pressure: 753.18 mmHg
Altitude: 75.96 m
Light: 0 lux
You can download all the code from here.
If you need more details please send me an email.
Comments