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 pressure sensor from Freescale. This is a great sensor that is easily interfaced with an Arduino and provides accurate pressure and temperature readings.

The device needs to be connected to the Arduino’s I2C (inter-integrated circuit or I-Squared-C, also known as a two-wire interface (TWI)) bus for data to be exchanged. I2C is a new concept for you, and although this chapter will not cover it in great detail, you will learn the basic concepts of the I2C and how to use it to get data from the MPL3115A2 sensor. Digital pressure sensors are ideal for making your own weather station.

Required Component  :

1.Arduino Uno Or Maga

2. MPL3115A2 Pressure Sensor

3. Connecting wire

4. Breadboard

This book will help you to gain more knowledge about Arduino

Beginning Arduino

Circuit diagram Digital Pressure Sensor Arduino :

Digital Pressure Sensor

I have used an Arduino Mega instead of an Uno for these projects as it has extra pins free for adding additional devices, if you wish. If you do use an Arduino Uno for your project, the SDA and SCLsignals are on the analog-in 4 and analog-in 5 pins, respectively. This is different from the Arduino Mega, which uses pins 20 and 21 instead.

This project simply requires the Uno, the MPL3115A2 on a breakout board, and a few wires. Connect the GND pin on the sensor to the GND pin on the Arduino. Connect the VCC pin on the sensor to the 3.3V pin on the Uno. Then connect the SDA (serial data) and SCL(serial clock) pins on the Uno to the corresponding SDA and SCLpins on the sensor.

Be aware that the wire library turns on the internal pull-ups to 5V and the MPL3115A2 requires a maximum voltage of 3.6 volts on the SDA and SCLpins. The Sparkfun breakout board has pull-up resistors to bring that down to a safe 3.3V. If the MPL3115A2 is the only device on the I2C bus, the pull-ups to 3.3V on the breakout board will keep the signal levels within a safe range.

If you add any other devices to the I2C bus that operate at 5V and have on-board pull-ups like the MPL3115A2 breakout board does, you could easily damage the MPL3115A2 and any other 3.3V-only I2C devices on the bus. Therefore, do not add any other I2C devices to this project without also providing some protection.

One way to avoid these problems is to clamp the SCLand SDA lines using low-capacitance 3.6-
volt Zener diodes to the ground (3.6V because of the nonlinear knee on the Zener curve). Another way is
to use LEDs with a 3.1 – 3.4-volt forward voltage to clamp the lines. (Most blue and white LEDs
have a forward voltage in that range.)

Code Digital Pressure Sensor Arduino :

#include <Wire.h> // so we can use I2C communication
#define MYALTITUDE 262 //define altitude at your location to
calculate mean sea level pressure in meters
// Register addresses
const int SENSORADDRESS = 0x60; // MPL3115A1 address from the
datasheet
#define SENSOR_CONTROL_REG_1 0x26
#define SENSOR_DR_STATUS 0x00 // Address of 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(); // join i2c bus
Serial.begin(9600); // start serial for output at 9600 baud
Serial.println("Setup");
I2C_Write(SENSOR_CONTROL_REG_1, 0b00000000); // put in standby
mode
// these upper bits of the control register
// can only be changed while in standby
I2C_Write(SENSOR_CONTROL_REG_1, 0b00111000); // set oversampling
to 128
Serial.println("Done.");
}
void loop(){
float temperature, pressure, baroPressure;
Read_Sensor_Data();
temperature = Calc_Temperature();
pressure = Calc_Pressure();
baroPressure = pressure * baroAltitudeCorrectionFactor;
Serial.print("Absolute pressure: ");
Serial.print(pressure); // in Pascal
Serial.print(" Pa, Barometer: ");
Serial.print(baroPressure); // in Pascal
Serial.print(" Pa, Temperature: ");
Serial.print(temperature); // in degrees C
Serial.println(" C");
delay(1000);
}
// Read the pressure and temperature readings from the sensor
void Read_Sensor_Data(){
// request a single measurement from the sensor
I2C_Write(SENSOR_CONTROL_REG_1, 0b00111010); //bit 1 is one shot
mode
// Wait for measurement to complete.
// One-shot bit will clear when it is done.
// Rread the current (sensor control) register
// repeat until sensor clears OST bit
do {
Wire.requestFrom(SENSORADDRESS,1);
} while ((Wire.read() & 0b00000010) != 0);
I2C_ReadData(); //reads registers from the sensor
}
// This function assembles the pressure reading
// from the values in the read buffer
// The two lowest bits are fractional so divide by 4
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);
}
// This function assembles the temperature reading
// from the values in the read buffer
float Calc_Temperature(){
int m_temp;
float l_temp;
m_temp = I2Cdata[3]; //temperature in whole degrees C
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;
Wire.beginTransmission(SENSORADDRESS);
Wire.write(SENSOR_OUT_P_MSB);
Wire.endTransmission(false);
// read 5 bytes. 3 for pressure, 2 for temperature.
Wire.requestFrom(SENSORADDRESS,5);
while(Wire.available()) I2Cdata[i++] = Wire.read();
// in some modes it is possible for the sensor
// to update the pressure reading
// while we were in the middle of reading it,
// in which case our copy is garbage
// (parts of two different readings)
// We can check bits in the DR (data ready)
// register to see if this happened.
Wire.beginTransmission(SENSORADDRESS);
Wire.write(SENSOR_DR_STATUS);
Wire.endTransmission(false);
Wire.requestFrom(SENSORADDRESS,1); //read 5 bytes. 3 for
pressure, 2 for temperature.
dataStatus = Wire.read();
readUnsuccessful = (dataStatus & 0x60) != 0;
// This will be unsuccessful if overwrite happened
// while we were reading the pressure or temp data.
// So keep reading until we get a successful clean read
} while (readUnsuccessful);
}
// This function writes one byte over I2C
void I2C_Write(byte regAddr, byte value){
Wire.beginTransmission(SENSORADDRESS);
Wire.write(regAddr);
Wire.write(value);
Wire.endTransmission(true);
}

After you have uploaded the code, open up the serial monitor window, and ensure that your baud rate is set to 9600. You will see a stream of data from the sensor showing the pressure in Pa (Pascals) and the temperature in Celsius. Pascals are the unit of pressure commonly used in weather forecasts

All Arduino tutorial available Click here

 ALL ARDUINO TUTORIAL 

 114 total views,  1 views today