# MicroPressure with Pico

**URL:** <https://community.sparkfun.com/t/micropressure-with-pico/46862>\
**Category:** Force & Weight\
**Created:** [January 26, 2024, 10:09pm UTC](https://community.sparkfun.com/t/micropressure-with-pico/46862 "2024-01-26T22:09:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![a1anm](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@a1anm](https://community.sparkfun.com/u/a1anm)\
**Post date:** [January 26, 2024, 10:09pm UTC](https://community.sparkfun.com/t/micropressure-with-pico/46862/1 "2024-01-26T22:09:48Z")

</div>

I am a beginner with sensors etc and have a question. Can this pressure sensor be used with a RPI Pico?

[https://www.sparkfun.com/products/16476](https://www.sparkfun.com/products/16476)

All of the guides and python library seem to be for Arduino.

---

<div class="post-metadata">

**Author:** ![TS-Russell](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/ts-russell/32/6092_2.png) [@TS-Russell](https://community.sparkfun.com/u/TS-Russell)\
**Post date:** [January 27, 2024, 12:22am UTC](https://community.sparkfun.com/t/micropressure-with-pico/46862/2 "2024-01-27T00:22:58Z")

</div>

We don’t have one ready-made, but my AI thinks this will work:

from machine import I2C, Pin

import time

class SparkFun\_MicroPressure:

def **init** (self, i2c, address=0x18):

self.i2c = i2c

self.address = address

def read\_pressure(self, unit=‘PSI’):

# Read pressure from sensor

data = bytearray(4)

self.i2c.readfrom\_mem\_into(self.address, 0x00, data)

raw\_pressure = int.from\_bytes(data[:3], ‘big’) / 16

# Convert pressure to desired unit

if unit == ‘PSI’:

return raw\_pressure

elif unit == ‘PA’:

return raw\_pressure \* 68.947572932

elif unit == ‘KPA’:

return raw\_pressure \* 0.068947572932

elif unit == ‘TORR’:

return raw\_pressure \* 51.714932572

elif unit == ‘INHG’:

return raw\_pressure \* 0.033863881579

elif unit == ‘ATM’:

return raw\_pressure \* 0.000680459553

elif unit == ‘BAR’:

return raw\_pressure \* 0.006894757293

else:

raise ValueError(‘Invalid unit’)

# Initialize I2C bus and micropressure sensor

i2c = I2C(scl=Pin(5), sda=Pin(4))

mpr = SparkFun\_MicroPressure(i2c)

while True:

print(“{:.4f} PSI”.format(mpr.read\_pressure(‘PSI’)))

print(“{:.1f} Pa”.format(mpr.read\_pressure(‘PA’)))

print(“{:.4f} kPa”.format(mpr.read\_pressure(‘KPA’)))

print(“{:.3f} torr”.format(mpr.read\_pressure(‘TORR’)))

print(“{:.4f} inHg”.format(mpr.read\_pressure(‘INHG’)))

print(“{:.6f} atm”.format(mpr.read\_pressure(‘ATM’)))

print(“{:.6f} bar”.format(mpr.read\_pressure(‘BAR’)))

print()

time.sleep(0.5)

This MicroPython code assumes that you are using an ESP32 or another board with I2C support and have connected the sensor to pins 4 (SDA) and 5 (SCL). You may need to adjust these values depending on your hardware setup.
