loading

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.

Step 1: What You Need..!!

1. Particle Photon

2. MPL3115A2

LINK : https://www.controleverything.com/content/Analog-Digital-Converters?sku=MPL3115A2_I2CS

3. I²C Cable

LINK : https://www.controleverything.com/content/I2C-Cab....

4. I²C Shield for Particle Photon

LINK : https://www.controleverything.com/content/I2C-Mas...

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

Here is the link for the same :

https://github.com/ControlEverythingCommunity/MPL3...

The datasheet of MPL3115A2 can be found here:

https://s3.amazonaws.com/controleverything.media/c...

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 available from ControlEverything.com.

// https://www.controleverything.com/content/Analog-...

#include<application.h>

#include<spark_wiring_i2c.h>

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

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

About This Instructable

113views

0favorites

More by Dcube Tech Ventures:Home Automation with Raspberry Pi using Relay board Motion Control with Raspberry Pi and LIS3DHTR, 3-Axis Accelerometer, using Python 3-Axis Accelerometer, ADXL345 with Raspberry Pi using Python 
Add instructable to: