Mechatronics LAB
Published © MIT

Digital Pressure Sensor– Arduino Workshop

In this project, we are going to Digital Pressure Sensor– Arduino and learn how to use an MPL3115A2 pressure sensor, the MPL3115A2 digital p

BeginnerFull instructions provided1 hour43,596
Digital Pressure Sensor– Arduino Workshop

Things used in this project

Story

Read more

Schematics

Digital Pressure Sensor– Arduino Workshop

Code

Code snippet #1

Arduino
#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);
}

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.