# 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:

```auto
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.

```auto
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:

```auto
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


## Related Guides

- [Sewable NeoPixels](https://learn.adafruit.com/flora-rgb-smart-pixels.md)
- [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)
- [Textable Sensor with FONA and CircuitPython](https://learn.adafruit.com/textable-sensor-with-fona-and-circuitpython.md)
- [AM Radio Morse Code Paddle](https://learn.adafruit.com/am-radio-morse-code-paddle.md)
- [Gemma Color Touch Pendant Necklace](https://learn.adafruit.com/gemma-color-touch-pendant-necklace.md)
- [Daily Cheer Automaton](https://learn.adafruit.com/daily-cheer-automaton.md)
- [Mini VOTE Keyboard](https://learn.adafruit.com/vote-keyboard.md)
- [Talking Musical NeoPixel Clock with Infrared, BLE and Touch Controls](https://learn.adafruit.com/talking-musical-neo-pixel-clock-with-infrared-ble-and-touch-controls.md)
- [Twinkly Earwarmer Headband](https://learn.adafruit.com/twinkly-ear-warmer.md)
- [Introducing Adafruit Feather](https://learn.adafruit.com/adafruit-feather.md)
- [Hidden Ink UV Message Reader](https://learn.adafruit.com/hidden-ink-uv-message-reader.md)
- [NeoPixel LED Mickey Ears](https://learn.adafruit.com/neopixel-led-mickey-ears.md)
- [GEMMA M0 Case](https://learn.adafruit.com/gemma-m0-case.md)
- [Make It Talk](https://learn.adafruit.com/make-it-talk.md)
- [Gemma M0 Vibration Sensor Motion Alarm](https://learn.adafruit.com/motion-alarm.md)
- [Glowing Scale Armor](https://learn.adafruit.com/glowing-scale-armor.md)
- [Screaming Cauldron](https://learn.adafruit.com/screaming-cauldron.md)
