MicroPressure with Pico

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

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

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.