Particle Photon - MPL3115A2 Precision Altimeter Sensor Tutorial

Introduction: Particle Photon - MPL3115A2 Precision Altimeter Sensor Tutorial

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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 Particle photon.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What You Need..!!

Step 2: Connections:

Take an I2C shield for particle photon and gently push it over the pins of particle photon.

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.

Step 3: Code:

The particle code for MPL3115A2 can be downloaded from our Github repository-DCUBE Store.

Here is the link.

We have used two libraries for particle code, which are application.h and spark_wiring_i2c.h. Spark_wiring_i2c library is required to facilitate the I2C communication with the sensor.

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

#include

// MPL3115A2 I2C address is 0x60(96)

#define Addr 0x60

float cTemp = 0.0, fTemp = 0.0, pressure = 0.0, altitude = 0.0;

int temp = 0, tHeight = 0;long pres = 0;

void setup()

{

// Set variable

Particle.variable("i2cdevice", "MPL3115A2");

Particle.variable("cTemp", cTemp);

Particle.variable("pressure", pressure);

Particle.variable("altitude", altitude);

// 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

tHeight = ((((long)data[1] * (long)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;

// 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();

// Start I2C transmission

Wire.beginTransmission(Addr);

// Select data register

Wire.write(0x00);

// Stop I2C transmission

Wire.endTransmission();

delay(1000);

// 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

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

pressure = (pres / 4.0) / 1000.0;

// Output data to dashboard

Particle.publish("Altitude :", String(altitude));

Particle.publish("Pressure :", String(pressure));

Particle.publish("Temperature in Celsius :", String(cTemp));

Particle.publish("Temperature in Fahrenheit :", String(fTemp));

delay(1000);

}

Step 4: 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.

Be the First to Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020

    Discussions

    0
    trevlaughlin
    trevlaughlin

    3 years ago

    Thanks for the write-up! Have you tried this with the Particle Electron? I'm new to this and trying to get this code to work with an Electron. Will all of the addresses remain the same? I'm using Particle Build for my IDE and the D0 and D1 pins on the electron for my I2C wiring. Thanks again!