Example Code for Arduino-Read Sensor Data via I2C

Last revision 2026/01/12

Download the program to the UNO, and open Serial Port Monitor to view the air pressure values. This project demonstrates how to read pressure data from the MPX5700AP sensor via I2C. Users can learn how to connect the sensor to Arduino, install the required library, and retrieve pressure values.

Hardware Preparation

  • DFRuino UNO R3 x1: Purchase Link
  • SEN0456 Gravity: I2C MPX5700AP Air Pressure Sensor(15-700kPa) x1:
  • DuPont Wires

Software Preparation

Wiring Diagram

Other Preparation Work

  • Connect the module to Arduino by the connection diagram above. You can certainly use with Gravity I/O to complete the project prototype in a more convenient and faster way.
  • The I2C address defaults to 0x16, corresponding to the ADDRESS_0 in the code. If you need to modify the I2C address, you can first configure the hardware I2C address via the dial-up switch on the module, and modify the definition ADDRESS_X of the I2C address in the sample code. The correspondence between the dial switch and the I2C address parameter is as follows:
    • ADDRESS_0:0x16, A0=0, A1=0
    • ADDRESS_1:0x17, A0=1, A1=0
    • ADDRESS_2:0x18, A0=0, A1=1
    • ADDRESS_3:0x19, A0=1, A1=1
  • Download and install DFRobot_MPX5700 library. (About how to install the library?)
  • Open the Arduino IDE, prepare to upload the program.

Sample Code

/*!
  * @file  getPressureValue.ino
    @n i2c Address select, default I2C address to 0x16, the A1 and A0 combines 4 IIC addresses. 
                | A1 | A0 |
                | 0  | 0  |    0x16
                | 0  | 1  |    0x17
                | 1  | 0  |    0x18
                | 1  | 1  |    0x19   default i2c address  
  * @n Phenomenon: serial print all data 
*/
#include "DFRobot_MPX5700.h"
#define I2C_ADDRESS 0x16
DFRobot_MPX5700  mpx5700(&Wire, I2C_ADDRESS);
void setup()
{
  Serial.begin(115200);
  while (false == mpx5700.begin())
  {
    Serial.println("i2c begin fail,please chack connect!");
    delay(1000);
  }
  Serial.println("i2c begin success");

/*
 *For smoothing data, it is necessary to set to obtain the average value of xx(quantity)data abc.  
 *If it is not set, the system will automatically get the average value of 5 samples. 
 */
  mpx5700.setMeanSampleSize(/*Sample Quantity*/5);
}

void loop()
{
  Serial.print("Pressure Value: ");
/*
 *Get the ambient air pressure. Can set whether to enable calibration.
 *1:Enable calibration 
 *0:Not 
 */
  Serial.print(mpx5700.getPressureValue_kpa(1));
  Serial.println(" kpa");
  delay(1000);
}

Result

Open the serial port monitor to get the final data.

Additional Information

You can certainly use with Gravity I/O to complete the project prototype in a more convenient and faster way.

Was this article helpful?

TOP