Arduino Nano - MPL3115A2 Precision Altimeter Sensor Tutorial

by Dcube Tech Ventures in Circuits > Electronics

5127 Views, 6 Favorites, 0 Comments

Arduino Nano - MPL3115A2 Precision Altimeter Sensor Tutorial

Arduino Nano - MPL3115A2 Precision Altimeter Sensor 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 Arduino Nano.

What You Need..!!

MPL3115A2.jpg
nano.png

Connections:

MPL3115A2.jpg
nano.png
sensor.jpg
arduino_connect.JPG

Take an I2C shield for Arduino Nano and gently push it over the pins of Nano.

Then connect the one end of I2C cable to MPL3115A2 sensor and the other end to the I2C shield.

Connections are shown in the picture above.

Code:

arduino_connect.JPG

The arduino code for MPL3115A2 can be downloaded from our github repository-DCUBE Store.

Here is the link for the same :

https://github.com/DcubeTechVentures/MPL3115A2/blob/master/Arduino/MPL3115A2.ino

We include library Wire.h to facilitate the I2c communication of the sensor with the Arduino board.

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

#include

// MPL3115A2 I2C address is 0x60(96)

#define Addr 0x60

void setup()

{

// Initialise I2C communication

Wire.begin();

// Initialise Serial Communication, set baud rate = 9600

Serial.begin(9600);

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select control register

Wire.write(0x26);

// Active mode, OSR = 128, altimeter mode

Wire.write(0xB9);

// Stop I2C transmission

Wire.endTransmission();

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select data configuration register

Wire.write(0x13);

// Data ready event enabled for altitude, pressure, temperature

Wire.write(0x07);

// Stop I2C transmission

Wire.endTransmission();

delay(300);

}

void loop()

{

unsigned int data[6];

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select control register

Wire.write(0x26);

// Active mode, OSR = 128, altimeter mode

Wire.write(0xB9);

// Stop I2C transmission

Wire.endTransmission();

delay(1000);

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select data register

Wire.write(0x00);

// Stop I2C transmission

Wire.endTransmission();

// Request 6 bytes of data

Wire.requestFrom(Addr, 6);

// Read 6 bytes of data from address 0x00(00)

// status, tHeight msb1, tHeight msb, tHeight lsb, temp msb, temp lsb

if (Wire.available() == 6)

{

data[0] = Wire.read();

data[1] = Wire.read();

data[2] = Wire.read();

data[3] = Wire.read();

data[4] = Wire.read();

data[5] = Wire.read();

}

// Convert the data to 20-bits

int tHeight = (((long)(data[1] * (long)65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16);

int temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16;

float altitude = tHeight / 16.0;

float cTemp = (temp / 16.0);

float fTemp = cTemp * 1.8 + 32;

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select control register

Wire.write(0x26);

// Active mode, OSR = 128, barometer mode

Wire.write(0x39);

// Stop I2C transmission

Wire.endTransmission();

delay(1000);

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select data register

Wire.write(0x00);

// Stop I2C transmission

Wire.endTransmission();

// Request 4 bytes of data

Wire.requestFrom(Addr, 4);

// Read 4 bytes of data

// status, pres msb1, pres msb, pres lsb

if (Wire.available() == 4)

{

data[0] = Wire.read();

data[1] = Wire.read();

data[2] = Wire.read();

data[3] = Wire.read();

}

// Convert the data to 20-bits

long pres = (((long)data[1] * (long)65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16;

float pressure = (pres / 4.0) / 1000.0;

// Output data to serial monitor

Serial.print("Altitude : ");

Serial.print(altitude);

Serial.println(" m");

Serial.print("Pressure : ");

Serial.print(pressure);

Serial.println(" kPa");

Serial.print("Temperature in Celsius : ");

Serial.print(cTemp);

Serial.println(" C");

Serial.print("Temperature in Fahrenheit : ");

Serial.print(fTemp);

Serial.println(" F");

delay(500);

}

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.