Raspberry Pi - MPL3115A2 Precision Altimeter Sensor Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
629 Views, 0 Favorites, 0 Comments
Raspberry Pi - MPL3115A2 Precision Altimeter Sensor Java Tutorial
The MPL3115A2 employs a MEMS pressure sensor with an I2C interface to provide accurate Pressure/Altitude and Temperature data. The sensor outputs are digitized by a high resolution 24-bit ADC. Internal processing removes compensation tasks from the host MCU system. It is capable of detecting a change in only 0.05 kPa which equates to a 0.3m change in altitude. Here is its demonstration with raspberry pi using java code.
What You Need..!!
Connections:
Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to MPL3115A2 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The java code for MPL3115A2 can be downloaded from our github repository- DCUBE Store.
Here is the link for the same :
https://github.com/DcubeTechVentures/MPL3115A2/tree/master/Java
We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:
You can also copy the code from here, it is given as follows:
// Distributed with a free-will license.
// 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
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
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);
// 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);
// 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;
// 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);
// 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);
}
}
Applications:
Various applications of MPL3115A2 includes High Accuracy Altimetry, Smartphones/Tablets, Personal Electronics Altimetry etc. It can also be incorporated in GPS Dead Reckoning, GPS Enhancement for Emergency Services, Map Assist, Navigation as well as Weather Station Equipment.