# Using MPL3115A2 with CircuitPython

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/051/042/medium800/sensors_IMG_6871.jpg?1518739643)

The [MPL3115A2](https://www.adafruit.com/product/1893) is a handy high precision barometric pressure that you can read from CircuitPython and Python code!&nbsp; This sensor provides pressure, temperature, and even calculates altitude (based on pressure) automatically for you.&nbsp; Best of all with a simple CircuitPython module you can read data from the sensor with ease.&nbsp; This guide shows how to connect a MPL3115A2 to a CircuitPython board and read data from it with Python code!

# Using MPL3115A2 with CircuitPython

## Hardware

# Hardware

To follow this guide you'll need the following parts:

- [**MPL3115A2 barometric pressure sensor.**](https://www.adafruit.com/product/1893)
- **A board running CircuitPython.** &nbsp; You'll need a board capable of running CircuitPython like the&nbsp;[Feather M0 Express, Trinket M0 etc!](https://www.adafruit.com/category/957).
- [**Breadboard**](https://www.adafruit.com/product/65)**&nbsp;and&nbsp;[jumper wires](https://www.adafruit.com/product/153).**&nbsp; You'll need these parts to connect components to your development board.

# Wiring

Connect your MPL3115A2 to your development board using a standard I2C connection.&nbsp; Here's an example of wiring a MPL3115A2 to a Feather M0 board:

![](https://cdn-learn.adafruit.com/assets/assets/000/051/039/medium800/sensors_m0_mpl3115a2_bb.png?1518738068)

- **Board 3.3V output** to **MPL3115A2 Vin**
- **Board GND/ground** to **MPL3115A2 GND**
- **Board SCL** to **MPL3115A2 SCL/I2C clock**
- **Board SDA** to **MPL3115A2 SDA/I2C data**

# Using MPL3115A2 with CircuitPython

## CircuitPython

# Installing Library

To use the MPL3115A2 you'll need to install the&nbsp;[Adafruit CircuitPython MPL3115A2](https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2)&nbsp;library on your CircuitPython board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](../../../../welcome-to-circuitpython/installing-circuitpython)&nbsp;for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle).&nbsp; Our introduction guide has&nbsp;[a great page on how to install the library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)&nbsp;for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

- **adafruit\_mpl3115a2.mpy**
- **adafruit\_bus\_device**

You can also download the&nbsp; **adafruit\_mpl3115a2.mpy** &nbsp;from&nbsp;[its releases page on Github](https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2/releases).

Before continuing make sure your board's lib folder or root filesystem has the&nbsp; **adafruit\_mpl3115a2.mpy,&nbsp;** and **&nbsp;adafruit\_bus\_device**** &nbsp; **files and folders** &nbsp;**copied over.

Next&nbsp;[connect to the board's serial REPL&nbsp;](../../../../welcome-to-circuitpython/the-repl)so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

# Usage

To demonstrate the usage of the sensor we'll initialize it and read the pressure and other values from the Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

```python
import board
import adafruit_mpl3115a2
i2c = board.I2C()
sensor = adafruit_mpl3115a2.MPL3115A2(i2c)
```

Remember if you're using a board that doesn't support hardware I2C (like the ESP8266) you need to use the&nbsp; **bitbangio** &nbsp;module instead:

```
import board
import bitbangio
import adafruit_mpl3115a2
i2c = bitbangio.I2C(board.SCL, board.SDA)
sensor = adafruit_mpl3115a2.MPL3115A2(i2c)
```

Now you're ready to read values from the sensor using any of these properties:

- **pressure** &nbsp;- The barometric pressure in Pascals.
- **temperature -** &nbsp;The temperature in degrees Celsius.
- **altitude** &nbsp;- The altitude as calculated using barometric pressure and returned in meters.

```
print('Pressure: {0:0.3f} pascals'.format(sensor.pressure))
print('Altitude: {0:0.3f} meters'.format(sensor.altitude))
print('Temperature: {0:0.3f} degrees Celsius'.format(sensor.temperature))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/051/040/medium800/sensors_Screen_Shot_2018-02-15_at_3.51.43_PM.png?1518738724)

To make the altitude calculation more accurate you'll want to set the pressure at sea level for your current location and weather conditions.&nbsp; Barometric pressure sensors can only calculate altitude based on the pressure of air they detect and that pressure changes daily and with weather conditions.&nbsp; Look up your local weather forecast and it typically includes the pressure at sea level.&nbsp; Set the **sealevel\_pressure** property (to a value in Pascals) to tell the chip this information and get the most accurate altitude measurements:

```
sensor.sealevel_pressure = 103040
```

![](https://cdn-learn.adafruit.com/assets/assets/000/051/041/medium800/sensors_Screen_Shot_2018-02-15_at_3.54.29_PM.png?1518738893)

That's all there is to using the MPL3115A2 with CircuitPython!

Here's a complete example that will print the pressure, altitude, and temperature every second.&nbsp; Save this as **main.py** on your board and open the REPL to view the output.&nbsp; Remember to switch to the **bitbangio** module if necessary for your board:

https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2/blob/main/examples/mpl3115a2_simpletest.py


## Featured Products

### MPL3115A2 - I2C Barometric Pressure/Altitude/Temperature Sensor

[MPL3115A2 - I2C Barometric Pressure/Altitude/Temperature Sensor](https://www.adafruit.com/product/1893)
This pressure sensor from Freescale is a great low-cost sensing solution for precision measurement of barometric pressure and altitude. The MPL3115A2 has a typical 1.5 Pascal resolution, which can resolve altitude at 0.3 meters (compare to the BMP180 which can do 0.17m). <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1893)
[Related Guides to the Product](https://learn.adafruit.com/products/1893/guides)
### Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0

[Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0](https://www.adafruit.com/product/2772)
Feather is the new development board from Adafruit, and like its namesake it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Feather M0 Basic Proto** ,&nbsp;it has a bunch of prototyping space...

In Stock
[Buy Now](https://www.adafruit.com/product/2772)
[Related Guides to the Product](https://learn.adafruit.com/products/2772/guides)
### Adafruit Feather M0 Express

[Adafruit Feather M0 Express](https://www.adafruit.com/product/3403)
At the Feather M0's heart is an ATSAMD21G18 ARM Cortex M0+ processor, clocked at 48 MHz and at 3.3V logic, the same one used in the new&nbsp;[Arduino Zero](https://www.adafruit.com/products/2843). This chip has a whopping 256K of FLASH (8x more than the Atmega328 or 32u4) and...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3403)
[Related Guides to the Product](https://learn.adafruit.com/products/3403/guides)
### Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE

[Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE](https://www.adafruit.com/product/3500)
The&nbsp;Adafruit Trinket M0 may be small, but do not be fooled by its size! It's a tiny microcontroller board, built around the Atmel ATSAMD21, a little chip with _a lot_ of power. We wanted to design a microcontroller board that was small enough to fit into any project, and low...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3500)
[Related Guides to the Product](https://learn.adafruit.com/products/3500/guides)
### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
### Adafruit METRO M0 Express - designed for CircuitPython

[Adafruit METRO M0 Express - designed for CircuitPython](https://www.adafruit.com/product/3505)
Metro is our series of microcontroller boards for use with the Arduino IDE. This new **Metro M0 Express** board looks a whole lot like our&nbsp;[original Metro 328](https://www.adafruit.com/product/2488), but with a huge upgrade. Instead of the ATmega328, this Metro...

In Stock
[Buy Now](https://www.adafruit.com/product/3505)
[Related Guides to the Product](https://learn.adafruit.com/products/3505/guides)

## Related Guides

- [Adafruit Feather M0 Express](https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-circuitpython.md)
- [Adafruit Metro M0 Express](https://learn.adafruit.com/adafruit-metro-m0-express.md)
- [Adafruit Trinket M0](https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino.md)
- [Princess Peach LED Crown](https://learn.adafruit.com/bowsette.md)
- [Cyberpunk Spikes](https://learn.adafruit.com/cyberpunk-spikes.md)
- [Adding an Accelerometer to your FeatherWing Crickit](https://learn.adafruit.com/adding-an-accelerometer-to-you-cricket-featherwing.md)
- [CircuitPython Hardware: Charlieplex LED Matrix](https://learn.adafruit.com/micropython-hardware-charlieplex-led-matrix.md)
- [Using Piezo Buzzers with CircuitPython & Arduino](https://learn.adafruit.com/using-piezo-buzzers-with-circuitpython-arduino.md)
- [CircuitPython with Jupyter Notebooks](https://learn.adafruit.com/circuitpython-with-jupyter-notebooks.md)
- [Neon LED Signs](https://learn.adafruit.com/led-neon-signs.md)
- [CircuitPython Hardware: PCA9685 PWM & Servo Driver](https://learn.adafruit.com/micropython-hardware-pca9685-pwm-and-servo-driver.md)
- [CircuitPython I2C and SPI Under the Hood](https://learn.adafruit.com/circuitpython-basics-i2c-and-spi.md)
- [MakeCode Maker](https://learn.adafruit.com/makecode-maker.md)
- [Proximity Based Lighting](https://learn.adafruit.com/proximity-based-lighting.md)
- [Mystical LED Halloween Hood](https://learn.adafruit.com/mystical-led-halloween-hood.md)
- [NeoPixel Basketball Hoop](https://learn.adafruit.com/neopixel-mini-basketball-hoop.md)
- [CircuitPython Hardware: SSD1306 OLED Display](https://learn.adafruit.com/micropython-hardware-ssd1306-oled-display.md)
- [NeoPixel Spats with Gemma and MakeCode](https://learn.adafruit.com/neopixel-spats.md)
