Using Raspberry Pi, Measure Altitude, Pressure, and Temperature With MPL3115A2

by Dcube Tech Ventures in Circuits > Sensors

2382 Views, 1 Favorites, 0 Comments

Using Raspberry Pi, Measure Altitude, Pressure, and Temperature With MPL3115A2

New Image22-min.jpg
Raspberry Pi  MPL3115A2 Precision Altimeter Sensor Java Tutorial

Know what you own, and know why you own it!

It's intriguing. We are living in the age of Internet Automation as it plunges into a plethora of new applications. As computer and electronics enthusiasts, we’ve been learning a lot with the Raspberry Pi and decided to blend our interests. This project takes about an hour if you’re new to I²C connections and Software setup, and it’s a great way to expand the capabilities of MPL3115A2 with Raspberry Pi in Java.

Indispensable Equipment We Need

New Image4.jpg
New Image5.jpg
New Image21-1.jpg
New Image14.jpg

1. Raspberry Pi

The first step was obtaining a Raspberry Pi board. This little genius is used by hobbyists, teachers and in creating innovative environments.

2. I2C 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²2 protocol for communicating. We purchased this sensor from Dcube Store.

4. Connecting Cable

We used the I²C connecting cable available at Dcube Store.

5. Micro USB cable

The Raspberry Pi is powered by micro USB supply.

6. Internet Access Enhancement - Ethernet Cable/WiFi Module

One of the first things that you will want to do is to get your Raspberry Pi connected up to the Internet. You can connect using an Ethernet cable or with a Wireless USB Nano WiFi adapter.

7. HDMI Cable(Optional, Your Choice)

You can connect Raspberry Pi to a monitor using an HDMI cable. Also, you can remote access your Raspberry Pi by using SSH/PuTTY.

Hardware Connections to Put Together the Circuit

New Image16 (1)-min.jpg
New Image19-min.jpg

Make the circuit as per the schematic shown.
In general, the connections are quite simple. Follow the instructions and images above, 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

For this, 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. As simple as this!

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 Crucial

To make our project a success, we need an internet access for our Raspberry Pi. In this, you have options like connecting an Ethernet(LAN) cable. Also, as an alternative but impressive way to use a WiFi adapter.

Powering of the Circuit

Plug in the Micro USB cable into the power jack of Raspberry Pi. Turn it on and voila, we are good to go!

Connection to Screen

We can either have the HDMI cable connected to a monitor or we can be a little bit innovative to make our headless Pi(using -SSH/PuTTY) which helps to cut the extra cost because we are somehow hobbyists.

When a habit begins to cost money, it's called a hobby.

Raspberry Pi Programming in Java

Screen Shot 2016-09-06 at 11.39.17 AM.png

The Java 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 Java 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 com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;</p><p>public class MPL3115A2
{
	public static void main(String args[]) throws Exception
	{
		// Create I2C bus
		I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
		// Get I2C device, MPL3115A2 I2C address is 0x60(96)
		I2CDevice device = Bus.getDevice(0x60);
		
		// Select control register
		// Active mode, OSR = 128, altimeter mode
		device.write(0x26, (byte)0xB9);
		// Select data configuration register
		// Data ready event enabled for altitude, pressure, temperature
		device.write(0x13, (byte)0x07);
		
		// Select control register
		// Active mode, OSR = 128, altimeter mode
		device.write(0x26, (byte)0xB9);
		Thread.sleep(1000);</p><p>		// Read 6 bytes of data from address 0x00(00)
		// status, tHeight msb1, tHeight msb, tHeight lsb, temp msb, temp lsb
		byte[] data = new byte[6];
		device.read(0x00, data, 0, 6);</p><p>		// Convert the data to 20-bits
		int tHeight = ((((data[1] & 0xFF) * 65536) + ((data[2] & 0xFF) * 256) + (data[3] & 0xF0)) / 16);
		int temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16;
		double altitude = tHeight / 16.0;
		double cTemp = (temp / 16.0);
		double fTemp = cTemp * 1.8 + 32;</p><p>		// Select control register
		// Active mode, OSR = 128, barometer mode
		device.write(0x26, (byte)0x39);
		Thread.sleep(1000);
		
		// Read 4 bytes of data from address 0x00(00)
		// status, pres msb1, pres msb, pres lsb
		device.read(0x00, data, 0, 4);</p><p>		// Convert the data to 20-bits
		int pres = (((data[1] & 0xFF) * 65536) + ((data[2] & 0xFF) * 256) + (data[3] & 0xF0)) / 16;
		double pressure = (pres / 4.0) / 1000.0;
		
		// Output data to screen
		System.out.printf("Pressure : %.2f kPa %n", pressure);
		System.out.printf("Altitude : %.2f m %n", altitude);
		System.out.printf("Temperature in Celsius : %.2f C %n", cTemp);
		System.out.printf("Temperature in Fahrenheit : %.2f F %n", fTemp);
	}
}</p>

The Practicality of the Code(Working)

Screen Shot 2016-09-05 at 11.26.28 PM-min.png

Now, download (or git pull) the code and open it in the Raspberry Pi.
Run the commands to Compile and Upload the code on 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 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. Using this sensor and Rasp Pi, you can build a Digital Visual Altimeter, most important piece of skydiving equipment, that can measure altitude, air pressure, and temperature. You can add wind gauze and other sensors so make more interesting one.

Conclusion

Since the program is amazingly customizable, there are many interesting ways in which you can extend this project and make it even better. For example, An altimeter/interferometer would include several altimeters mounted on masts which would acquire measurements simultaneously, thus providing continuous, single- or multi-altimeter wide-area coverage. We have an interesting video tutorial on YouTube that can help you in the better understanding of this project.

Contemplate about this and more! Don’t forget to follow as more is coming up.