Hello friends, I hope you all are doing well. Today, I am going to share the 8th tutorial of Section-III in our Raspberry Pi Programming Series. In the previous tutorial, we interfaced the temperature sensor DS18B20 with Raspberry Pi 4. In today's guide, we'll discover another temperature sensor BMP180 and will interface it with Raspberry Pi 4.
So, let's get started:
Project Description
In today's tutorial, we will interface the BMP180 sensor with Raspberry Pi 4 and will display the values of temperature, barometric pressure and altitude in the Raspberry Pi Console Window.
Components Required
We will use the following components in today's project:
- Raspberry pi 4
- BMP180 sensor
- Female-Female Jumper wires
- Breadboard
BMP180 Air Pressure Sensor
- BMP180 is a low-cost sensor designed by Bosch to measure the temperature and barometric pressure.
- As it can measure the pressure, so we can also get the altitude value from BMP180.
- It follows the I2C protocol to communicate with microcontrollers.
- Its a successor of the BMP085 sensor, a more accurate and precise model, shown in the below figure:
BMP180 Working Principle
Pressure can be measured with the BMP180 piezoresistive sensor. Semiconducting (often silicon) piezoresistive sensors respond to mechanical forces such as air pressure by shifting their resistance value.
Because air density changes with temperature, the BMP180 collects data on both pressure and temperature. Warmer air is less dense and lighter; therefore, it exerts less force on the sensor. Because the atmosphere at lower temperatures is thicker and heavier, it presses the sensor more. The sensor takes real-time temperature values to adjust for air density variations.
The BMP180 provides raw temperature (UT) and pressure(UP) readings. A reading of the pressure is taken after the temperature has been recorded. Measurement processing at the sensor is depicted in this flowchart:
The 11 individual calibration coefficients for the BMP180 are stored in the sensor's 176-bit EEPROM. Together with the UP and UT, these are needed to determine the actual atmospheric pressure and temperature. It takes some advanced algorithms to determine the exact pressure and temperature:
I2C Working Principle
I2C Protocol is designed to communicate between several devices with a single bus simultaneously. I2C uses the address frame for selecting a slave device to communicate.
Depending on the application, I2C slave addresses might be 7 or 10 bits in length. This requires sending not one but two addresses across the bus in two separate address frames. A specific 7-bit address of 0x92 is used for the first location. A second address frame, intended for 10-bit machines, comes just after the unique address. This is how devices with 10-bit addresses can coexist on a bus with devices having 7-bit addresses without causing any incompatibilities.
If you're using I2C, you can use either of its two unique slave-handling modes. Information can flow in both directions between the Master and the slave. I2C read/write bit comes immediately after the address bit. The Master sends a read bit to the slave by making the line high when it wants to accept data from the slave. A write bit is sent from the Master to the slave by grounding the connection.
The placement of the Raspberry Pi's I2C pins is depicted in the following diagram:
Install Python I2C Tools
First, you'll need to turn the I2C drivers on your Raspberry Pi.
Enter the raspi-config command as sudo. Select Menu Item #8 (Advanced Settings). Choose A7 I2C, confirm your acceptance when prompted to activate the I2C driver, then confirm your acceptance once more to load the drivers automatically. Return to the command prompt and type sudo reboot to restart your Raspberry Pi.
Be sure to include i2c-dev in /etc/modules.
sudo nano /etc/modules
After that, at the prompt, type:
sudo modprobe i2c-dev
Verify that i2c modules have been loaded and are functioning:
lsmod | grep i2c
- Install some i2c tools:
I2c-tools has some neat programs like "i2cdetect", which lists all the slave devices on a bus by their addresses. To test it out, connect the sensor and run "sudo i2cdetect" with the -y parameter set to 1. In addition, "i2cdump" is a tool that may be used to determine the configuration of a single I2C device by reading its registers. Using the below connection details and the sensor's factory settings (address 0x77), you should see the following output:
The BMP180 has been detected on channel 77.
Circuit Diagram of BMP180 with Raspberry Pi 4
You only need a breadboard and some female-to-male jumper wires to hook up the sensor to your Pi 4. Before plugging in any peripherals, you should always shut down your Raspberry Pi. Type the following command and watch for the green LED to turn off:
sudo shutdown -h now
The BMP180 can be linked to a Raspberry Pi by following the instructions below. There are a few variations of the BMP180 boards, so keep that in mind. The BMP180 is the part number for the chip rather than the entire board; therefore, your board may have 4, 5, or 6 pins.
With the Raspberry Pi oriented as indicated above, the diagram below shows how the jumper wires should be connected.
The red jumper connects the input VCC pin of the sensor to the 3v3 pin of the Pi 4.
The yellow jumper connects the Raspberry Pi's SDA pin to the BMP180's SCL pin. This wire is used for communication between the BMP180 and the Raspberry Pi.
Connecting the Raspberry Pi's SCL pin to the BMP180's SCL pin is a blue jumper wire that goes from the third pin on the top row to the right. i2c devices use the clock signal provided by this pin to synchronize their communication with the Raspberry Pi's clock.
Any of the Raspberry Pi's ground (GND) pins can be connected to the BMP180's GND pin with the help of the black jumper.
Verify all wires are securely attached before powering up any devices. While it's doubtful anything horrible will happen, double-checking before pushing the button is still a good idea.
Python Code for BMP180 and RPi4
For Python to understand the BMP180's output, we must install the necessary libraries. The Adafruit libraries were initially developed to be utilized by BMP085, the BMP180's predecessor, but the compatibility between the chips means they can also be used with the BMP180.
Type this into the Raspberry Pi's shell to install the necessary Python and the libraries above.
sudo apt-get install build-essential python-dev python-smbus
Please make a new Py file in your home directory and give it the name BM180.py. Then, paste the code below into the new file. The code is well-commented, so it shouldn't be too difficult to understand.
Complete code
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# BMP180 address, 0x77(119)
# Read data back from 0xAA(170), 22 bytes
data = bus.read_i2c_block_data(0x77, 0xAA, 22)
# Convert the data
AC1 = data[0] * 256 + data[1]
if AC1 > 32767 :
AC1 -= 65535
AC2 = data[2] * 256 + data[3]
if AC2 > 32767 :
AC2 -= 65535
AC3 = data[4] * 256 + data[5]
if AC3 > 32767 :
AC3 -= 65535
AC4 = data[6] * 256 + data[7]
AC5 = data[8] * 256 + data[9]
AC6 = data[10] * 256 + data[11]
B1 = data[12] * 256 + data[13]
if B1 > 32767 :
B1 -= 65535
B2 = data[14] * 256 + data[15]
if B2 > 32767 :
B2 -= 65535
MB = data[16] * 256 + data[17]
if MB > 32767 :
MB -= 65535
MC = data[18] * 256 + data[19]
if MC > 32767 :
MC -= 65535
MD = data[20] * 256 + data[21]
if MD > 32767 :
MD -= 65535
time.sleep(0.5)
# BMP180 address, 0x77(119)
# Select measurement control register, 0xF4(244)
# 0x2E(46) Enable temperature measurement
bus.write_byte_data(0x77, 0xF4, 0x2E)
time.sleep(0.5)
# BMP180 address, 0x77(119)
# Read data back from 0xF6(246), 2 bytes
# temp MSB, temp LSB
data = bus.read_i2c_block_data(0x77, 0xF6, 2)
# Convert the data
temp = data[0] * 256 + data[1]
# BMP180 address, 0x77(119)
# Select measurement control register, 0xF4(244)
# 0x74(116) Enable pressure measurement, OSS = 1
bus.write_byte_data(0x77, 0xF4, 0x74)
time.sleep(0.5)
# BMP180 address, 0x77(119)
# Read data back from 0xF6(246), 3 bytes
# pres MSB1, pres MSB, pres LSB
data = bus.read_i2c_block_data(0x77, 0xF6, 3)
# Convert the data
pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
# Callibration for Temperature
X1 = (temp - AC6) * AC5 / 32768.0
X2 = (MC * 2048.0) / (X1 + MD)
B5 = X1 + X2
cTemp = ((B5 + 8.0) / 16.0) / 10.0
fTemp = cTemp * 1.8 + 32
# Calibration for Pressure
B6 = B5 - 4000
X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
X2 = AC2 * B6 / 2048.0
X3 = X1 + X2
B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
X1 = AC3 * B6 / 8192.0
X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
X3 = ((X1 + X2) + 2) / 4.0
B4 = AC4 * (X3 + 32768) / 32768.0
B7 = ((pres - B3) * (25000.0))
pressure = 0.0
if B7 < 2147483648L :
pressure = (B7 * 2) / B4
else :
pressure = (B7 / B4) * 2
X1 = (pressure / 256.0) * (pressure / 256.0)
X1 = (X1 * 3038.0) / 65536.0
X2 = ((-7357) * pressure) / 65536.0
pressure = (pressure + (X1 + X2 + 3791) / 16.0) / 100
# Calculate Altitude
altitude = 44330 * (1 - ((pressure / 1013.25) ** 0.1903))
# Output data to screen
print "Altitude : %.2f m" %altitude
print "Pressure : %.2f hPa " %pressure
print "Temperature in Celsius : %.2f C" %cTemp
print "Temperature in Fahrenheit : %.2f F" %fTemp
Output
Is it possible to use several BMP sensors on one board?
Usually, only one device on the I2C bus is assigned to address 0x77 because each device on the bus requires its unique address (etc.). It is possible to use numerous devices on the same address, but each device will demand its GPIO pin in exchange. This is useful if you connect multiple I2C devices to the same address, like the BMP085. By holding the XCLR pin low, you can force all devices on the I2C bus to reset while freeing the rest of the one you wish to read and making it respond to any request.
Predicting the weather with a pressure sensor
Forecasting the weather with changes in pressure sensors is possible. An increase in the amount of air above the surface of the Earth causes the barometric pressure to drop. As the air rises, it leaves a vacuum that creates a low-pressure zone on the ground. Air mass cools and condenses as it rises in height. The result is clouds that can eventually turn into the rain because of the condensation of atmospheric moisture. The wind is also often present as the surface air flows into low-pressure areas.
When air from higher in the stratosphere sinks to Earth, the result is a rise in atmospheric pressure. The air pressure beneath the surface is raised as the falling mass exerts pressure on the ground. The air mass is cooler and denser at lower altitudes, but at higher altitudes, it warms up and expands. Because of the low humidity of the heated expanding air, clouds are rarely seen during this weather phenomenon. In general, increasing barometric pressures precede the arrival of pleasant, bright days.
Conclusion
Here, we learned how to connect a BMP180 sensor to a Raspberry Pi 4. We have also researched the 12c protocol's operation to ensure successful sensor-to-pi 4 communication. Several tasks call for the interesting BMP180 sensor. Even though it has been retired from production, a comprehensive data sheet is still accessible online. The values provided by the module can be read and manipulated with only a basic shell script. Even though it's a major undertaking, learning to compile your kernel can help you better grasp Linux. The following tutorial will teach you how to connect the MQ-2 Gas Sensor with a Raspberry Pi 4.