Altitude, Pressure and Temperature Using Raspberry Pi With MPL3115A2

by Dcube Tech Ventures in Circuits > Sensors

10256 Views, 6 Favorites, 0 Comments

Altitude, Pressure and Temperature Using Raspberry Pi With MPL3115A2

New Image21.jpg
57ce5cc467400c0a7e000fac.jpeg

Sounds interesting. It's quite possible in this time when we all are going into IoT generation. As an electronics freak, we’ve been playing with the Raspberry Pi, and decided to make interesting projects using this knowledge. In this project, we will be measuring altitude, air pressure, temperature using Raspberry Pi. So here goes the documentation(always being modified, and expanded). We recommend starting with following the instructions and copy the code. You can experiment later on. So let's get started.

Imperative Equipment We Need

New Image.jpg
New Image6.jpg
New Image15.jpg
New Image13.jpg

1. Raspberry Pi

The first step was obtaining a Raspberry Pi board. We purchased ours and so can you. Started learning from the tutorials, we understood the scripting and connection concepts and learned afterward. This little genius is common for hobbyists, teachers and in creating innovative environments.

2. I²C Shield for Raspberry Pi

The INPI2(I2C adapter) provides the Raspberry Pi 2/3 an I²C port for use with multiple I2C devices. It's available on Dcube Store

3. Altimeter, Pressure and Temperature Sensor, MPL3115A2.

The MPL3115A2 is a MEMS pressure sensor with an I²C interface to give Pressure/Altitude and Temperature data. This sensor uses the I²C protocol for communicating. We purchased this sensor from Dcube Store

4. Connecting Cable

We had the I2C connecting cable available at Dcube Store

5. Micro USB cable

The micro USB cable Power supply is an ideal choice for powering the Raspberry Pi.

6 . Internet Access Enhancement - Ethernet Cable/WiFi Adapter

In this era, gaining access to anything needs an internet connection(almost as there is life offline as well). So we go take the advice of a LAN cable or a Wireless Nano USB Adapter(WiFi) to build the internet connection so that we can use our Rasp Pi with ease and at no problem at all.

7. HDMI Cable(Optional, Your Choice)

It's a bit tricky. You can have the power to attach another Monitor in case if you want or it's very cost-effective to yourself by making a headless Pi connection with your PC/Laptop.

Hardware Connections to Put Together the Circuit

New Image16.jpg
New Image19.jpg

Make the circuit as per the schematic shown.
In general, the connections are very simple. Follow the instructions and images, and you should have no problems.

While planning, we looked at hardware and coding as well as electronics basics. We wanted to design up a simple electronics schematic for this project. In the diagram, you can notice the different parts, power components and I²C sensor following I²C communication protocols. Hopefully, this illustrates how simple the electronics for this project is.

Connection of the Raspberry Pi and I2C Shield

First of all take the Raspberry Pi and place the I²C Shield on it. Press the Shield gently(See the pic).

Connection of the Sensor and Raspberry Pi

Take the sensor and Connect the I²C cable with it. Make sure that I²C Output ALWAYS connects to the I²C Input. The same to be followed by the Raspberry Pi with the I²C shield mounted over it.
We have the I²C Shield and the I²C connecting cables on our side as a very big advantage as we are left only with the plug and play option. No more pins and wiring issue and hence, confusion is gone. What a relief as just imagine yourself in the web of wires and getting into that. Just the simple process that we have mentioned.

Note : The brown wire should always follow the Ground (GND) connection between the output of one device and the input of another device.

Internet Connectivity is vital

You have a choice here actually. You can Connect Raspberry Pi with the LAN cable or the wireless Nano USB Adapter for WiFi Connectivity. Anyhow, it did the main aim that is to connect to the internet.

Powering of the Circuit

Plug in the Micro USB cable into the power jack of Raspberry Pi. Light it up and we are good to go.

Connection to Screen

We can either have the HDMI cable connected to a new monitor or we can make our headless Pi which is creative and cost-effective using remote access like-SSH/PuTTY.(I know we are not funded like a secret organisation)

Raspberry Pi Programming in Python

Screen Shot 2016-09-02 at 5.35.14 PM.png

The Python code for the Raspberry Pi and MPL3115A2 Sensor. It's available in our Github repository.

Before going on to the code, make sure you read the instructions given in the Readme file and Setup your Raspberry Pi according to it. It will take just take a moment to do so.

The altitude is calculated from the pressure using the equation below:

h = 44330.77 {1 - (p / p0) ^ 0.1902632} + OFF_H (Register Value)

Where p0 = sea level pressure (101326 Pa) and h is in meters. The MPL3115A2 uses this value since the offset register is defined as 2 Pascals per LSB.

The code is clearly in front of you and it's in the simplest form that you can imagine of and you should have no problems.

You can copy the working Python code for this sensor from here also.

<p># Distributed with a free-will license.<br># Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# MPL3115A2
# This code is designed to work with the MPL3115A2_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/products</p><p>import smbus
import time</p><p># Get I2C bus
bus = smbus.SMBus(1)</p><p># MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#		0xB9(185)	Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)
# MPL3115A2 address, 0x60(96)
# Select data configuration register, 0x13(19)
#		0x07(07)	Data ready event enabled for altitude, pressure, temperature
bus.write_byte_data(0x60, 0x13, 0x07)
# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#		0xB9(185)	Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)</p><p>time.sleep(1)</p><p># MPL3115A2 address, 0x60(96)
# Read data back from 0x00(00), 6 bytes
# status, tHeight MSB1, tHeight MSB, tHeight LSB, temp MSB, temp LSB
data = bus.read_i2c_block_data(0x60, 0x00, 6)</p><p># Convert the data to 20-bits
tHeight = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16
altitude = tHeight / 16.0
cTemp = temp / 16.0
fTemp = cTemp * 1.8 + 32</p><p># MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#		0x39(57)	Active mode, OSR = 128, Barometer mode
bus.write_byte_data(0x60, 0x26, 0x39)</p><p>time.sleep(1)</p><p># MPL3115A2 address, 0x60(96)
# Read data back from 0x00(00), 4 bytes
# status, pres MSB1, pres MSB, pres LSB
data = bus.read_i2c_block_data(0x60, 0x00, 4)</p><p># Convert the data to 20-bits
pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
pressure = (pres / 4.0) / 1000.0</p><p># Output data to screen
print "Pressure : %.2f kPa" %pressure
print "Altitude : %.2f m" %altitude
print "Temperature in Celsius  : %.2f C" %cTemp
print "Temperature in Fahrenheit  : %.2f F" %fTemp</p>

The Practicality of the Code(Testing)

Screen Shot 2016-09-02 at 5.23.35 PM.png

Now, download (or git pull) the code and open it in the Raspberry Pi.

Run the commands to Compile and Upload the code in the terminal and see the output on Monitor. After few seconds, it will display all the parameters. After making sure that everything works smoothly, you can take this project into a bigger project.

Applications and Features

The common use of the MPL3115A2 Precision Altimeter I²C sensor is in applications like Map(Map Assist, Navigation), Magnetic Compass, Or GPS(GPS Dead Reckoning, GPS Enhancement For Emergency Services), High Accuracy Altimetry, Smartphones/Tablets, Personal Electronics Altimetry and Satellites(Weather Station Equipment/Forecasting).

For e.g. a project for making Personal Electronics Altimeter that measures altitude, air pressure, temperature using Raspberry Pi. The Personal Electronics Altimeter is a total a pretty quick project to build. It will take only a few moments if you have all parts and don’t improvise(of course you can !). A pressure altimeter is an altimeter found in most aircraft, and skydivers use wrist-mounted versions for similar purposes. Hikers and mountain climbers use wrist-mounted or hand-held altimeters.

Conclusion

Hope this project inspires further experimentation. This I²C sensor is incredibly versatile, cheap and accessible. Since it's an extremely mutable program, there are interesting ways you can extend this project and make it even better. For example, the altimeter is an instrument optional in off-road vehicles to aid in navigation. Some high-performance luxury cars that were never intended to leave paved roads, use this technology. For your convenience, we have an interesting video tutorial on YouTube which might lend a hand to your exploration. Hope this project inspires further experimentation.

Keep pondering! Don’t forget to pursue as more is always coming up.