In this project, we will learn how to interface an MPL3115A2 digital pressure sensor with an Arduino to measure atmospheric pressure and temperature. This sensor is ideal for creating your own weather station due to its accuracy and ease of integration with Arduino's I2C (Inter-Integrated Circuit) interface.
Required Components1. Arduino Uno or Arduino Mega
2. MPL3115A2 Pressure Sensor (on breakout board)
4. Breadboard
Overview of the MPL3115A2 SensorThe MPL3115A2 sensor communicates over the I2C protocol and provides accurate readings of pressure and temperature. It operates at a maximum voltage of 3.6V, making it compatible with Arduino's 3.3V logic level.
Circuit Diagram and ConnectionsFor Arduino Uno:
- **GND** of the sensor to **GND** on Arduino
- **VCC** of the sensor to **3.3V** on Arduino
- **SDA** (Serial Data) of the sensor to **A4 (SDA)** on Arduino
- **SCL** (Serial Clock) of the sensor to **A5 (SCL)** on Arduino
For Arduino Mega:
- **GND** of the sensor to **GND** on Arduino
- **VCC** of the sensor to **3.3V** on Arduino
- **SDA** (Serial Data) of the sensor to **20 (SDA)** on Arduino
- **SCL** (Serial Clock) of the sensor to **21 (SCL)** on Arduino
Code Explanation
#include <Wire.h> // Include the Wire library for I2C communication
#define MYALTITUDE 262 // Define altitude at your location in meters for sea level pressure calculation
// Register addresses
const int SENSOR_ADDRESS = 0x60; // MPL3115A1 sensor address
#define SENSOR_CONTROL_REG_1 0x26
#define SENSOR_DR_STATUS 0x00 // DataReady status register
#define SENSOR_OUT_P_MSB 0x01 // Starting address of Pressure Data registers
float baroAltitudeCorrectionFactor = 1 / (pow(1 - MYALTITUDE / 44330.77, 5.255877));
byte I2Cdata[5] = {0, 0, 0, 0, 0}; // Buffer for sensor data
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Initialize serial communication for output
Serial.println("Setup");
// Put sensor in standby mode to configure
I2C_Write(SENSOR_CONTROL_REG_1, 0b00000000);
// Set oversampling to 128 to improve accuracy
I2C_Write(SENSOR_CONTROL_REG_1, 0b00111000);
Serial.println("Initialization Complete");
}
void loop() {
float temperature, pressure, baroPressure;
Read_Sensor_Data(); // Read pressure and temperature data from sensor
temperature = Calc_Temperature(); // Calculate temperature in Celsius
pressure = Calc_Pressure(); // Calculate pressure in Pascals
baroPressure = pressure * baroAltitudeCorrectionFactor; // Calculate barometric pressure
// Output sensor readings to serial monitor
Serial.print("Absolute Pressure: ");
Serial.print(pressure); // Pressure in Pascal (Pa)
Serial.print(" Pa, Barometric Pressure: ");
Serial.print(baroPressure); // Barometric pressure in Pascal (Pa)
Serial.print(" Pa, Temperature: ");
Serial.print(temperature); // Temperature in Celsius
Serial.println(" C");
delay(1000); // Delay before next reading
}
// Read pressure and temperature data from the sensor
void Read_Sensor_Data() {
// Request a single measurement from the sensor in one-shot mode
I2C_Write(SENSOR_CONTROL_REG_1, 0b00111010);
// Wait for measurement to complete
do {
Wire.requestFrom(SENSOR_ADDRESS, 1);
} while ((Wire.read() & 0b00000010) != 0);
I2C_ReadData(); // Read registers from the sensor
}
// Calculate pressure from sensor data
float Calc_Pressure() {
unsigned long m_pressure = I2Cdata[0];
unsigned long c_pressure = I2Cdata[1];
float l_pressure = (float)(I2Cdata[2] >> 4) / 4;
return ((float)(m_pressure << 10 | c_pressure << 2) + l_pressure);
}
// Calculate temperature from sensor data
float Calc_Temperature() {
int m_temp = I2Cdata[3]; // Temperature in whole degrees Celsius
float l_temp = (float)(I2Cdata[4] >> 4) / 16.0; // Fractional portion of temperature
return ((float)(m_temp + l_temp));
}
// Read barometer and temperature data (5 bytes)
void I2C_ReadData() {
byte readUnsuccessful;
do {
byte i = 0;
byte dataStatus = 0;
// Request pressure and temperature data from sensor
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(SENSOR_OUT_P_MSB);
Wire.endTransmission(false);
// Read 5 bytes (3 for pressure, 2 for temperature)
Wire.requestFrom(SENSOR_ADDRESS, 5);
while (Wire.available()) {
I2Cdata[i++] = Wire.read();
}
// Check DataReady status register to ensure clean read
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(SENSOR_DR_STATUS);
Wire.endTransmission(false);
Wire.requestFrom(SENSOR_ADDRESS, 1);
dataStatus = Wire.read();
readUnsuccessful = (dataStatus & 0x60) != 0; // Check if overwrite occurred
} while (readUnsuccessful);
}
// Write data to MPL3115A2 sensor over I2C
void I2C_Write(byte regAddr, byte value) {
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(regAddr);
Wire.write(value);
Wire.endTransmission(true);
}
Code Explanation
1. **Initialization**: We initialize the Wire library for I2C communication and set the serial communication for debugging at 9600 baud rate.
2. **Setup**: The sensor is initialized by setting it to standby mode (`0b00000000`). Then, oversampling is set to 128 (`0b00111000`) for improved accuracy.
3. **Main Loop**:
- **Read Sensor Data**: The sensor data is read using a one-shot measurement request.
- **Calculate Temperature and Pressure**: Functions `Calc_Temperature()` and `Calc_Pressure()` process the raw sensor data to calculate temperature in Celsius and pressure in Pascals.
- **Output**: Serial print statements display the absolute pressure, barometric pressure corrected for altitude, and temperature.
4. **Read Sensor Data Function**: Requests pressure and temperature data from the sensor, ensuring a clean read by checking the DataReady status register.
5. **Calculation Functions**: Convert raw sensor data into meaningful temperature and pressure values using appropriate formulas.
Running the Project
- Upload the code to your Arduino board.
- Open the Serial Monitor at 9600 baud rate.
- You will see continuous readings of absolute pressure, barometric pressure, and temperature.
Conclusion
By following this guide, you have successfully interfaced an MPL3115A2 digital pressure sensor with an Arduino. This project provides accurate pressure and temperature readings, making it suitable for applications like weather stations and environmental monitoring.
For further exploration, consider adapting this project for altitude measurement or integrating additional sensors for comprehensive environmental monitoring.
---
This project guide covers the essentials of setting up and programming the MPL3115A2 digital pressure sensor with Arduino, providing detailed explanations of the circuit connections, code implementation, and data interpretation for atmospheric pressure and temperature measurements.
Comments
Please log in or sign up to comment.