MicroPressure Sensor connect to Raspberry pi

Hi,

I purchased a SparkFun Qwiic MicroPressure Sensor (SEN-16476 ROHS) and am trying to connect it to my Raspberry Pi. I understand that I need to install a couple of SparkFun libraries on the Raspberry Pi for the sensor to work. However, when I attempt to do so, I keep receiving the message: “The environment is managed externally,” which prevents me from installing the libraries.

Does SparkFun support Raspberry Pi, and can you provide guidance on how to connect this sensor to the Raspberry Pi?

Thank you.

The Sparkfun libraries for that sensor are designed for use of an Arduino MCU as the sensor controller, and won’t work using a Raspberry Pi running linux as the sensor controller.

Is that what you had in mind? If not, which RPi board do you have?

In principle the C++ library code could be adapted to run under linux, but that would require considerable technical expertise with both systems.

Yes, that seems to be the case. I tried to proceed without the library, basing my work on code I found on GitHub for Arduino. With some help from ChatGPT, I developed some code that partially works, but it only returns a single value of 6291456 PSI, which does not change even when I apply more pressure.

I’m a mechanical engineering student and have only been working with Python, Raspberry Pi, and these types of sensors for about three weeks, so my technical expertise is limited. Would you recommend switching to a different type of pressure sensor, or should I stick with this one and try to figure it out?

I’m working on a project that requires me to use a Raspberry Pi to read pressure data from a pressure sensor.

Thank you for your help.

Here is the code if you want to take a look:

import smbus
import time

Define constants for I2C communication

I2C_BUS = 1
SENSOR_ADDRESS = 0x18 # Default sensor address, adjust if necessary

Define constants for pressure units

PSI = 0
PA = 1
KPA = 2
TORR = 3
INHG = 4
ATM = 5
BAR = 6

Define constants for pressure range

MINIMUM_PSI = 0
MAXIMUM_PSI = 25

Conversion factors for pressure units (adjust as per sensor datasheet)

CONVERSION_FACTORS = {
PSI: 1.0,
PA: 6894.76,
KPA: 6.89476,
TORR: 51.7149,
INHG: 2.03602,
ATM: 0.068046,
BAR: 0.0689476
}

Initialize I2C bus

bus = smbus.SMBus(I2C_BUS)

def read_status():
“”“Reads and returns the status byte from the sensor.”“”
try:
status = bus.read_byte_data(SENSOR_ADDRESS, 0x00) # Replace 0x00 with actual status register address
return status
except Exception as e:
print(f"Error reading status: {e}")
return None

def read_pressure(units=PSI):
“”“Reads pressure from the sensor and returns in specified units.”“”
try:
# Example: Start measurement command
bus.write_byte(SENSOR_ADDRESS, 0x48) # Replace 0x48 with actual measurement command

    time.sleep(0.1)  # Adjust delay based on sensor's measurement time
    
    # Read pressure data (example: 3 bytes)
    data = bus.read_i2c_block_data(SENSOR_ADDRESS, 0x00, 3)  # Adjust 0x00 and 3 as per your sensor's data format
    
    # Convert data to raw pressure value (example: combine bytes into a single integer)
    raw_pressure = (data[0] << 16) | (data[1] << 8) | data[2]  # Adjust based on actual data format
    
    # Convert raw pressure to specified units
    if units in CONVERSION_FACTORS:
        pressure = raw_pressure * CONVERSION_FACTORS[units]
        return pressure
    else:
        print(f"Unsupported pressure unit: {units}")
        return None
except Exception as e:
    print(f"Error reading pressure: {e}")
    return None

try:
while True:
# Example: Read pressure in PSI
pressure_psi = read_pressure(units=PSI)

    if pressure_psi is not None:
        print(f"Pressure (PSI): {pressure_psi:.2f}")
    else:
        print("Failed to read pressure data.")
    
    time.sleep(1)  # Adjust delay between readings

except KeyboardInterrupt:
print(“Program interrupted by user.”)
finally:
bus.close()

Please edit your post to add code tags (<> editor button). You can see what a mess the forum software made of it.

Sorry, I have limited experience with Python, and do not use RPi for low level sensor access. You are more likely to get help on a Raspberry Pi forum.

An option would be to use an Arduino, as described in the Sparkfun tutorial, to collect the basic pressure data and send it via serial to the RPi for whatever else needs to be done.

I do not recommend using chatGPT, as it has no understanding (more accurately, an extremely limited data base) of low level coding details and more often than not, produces completely unusable code.

Here are a few ideas, this should at least get it to where you know if the sensor if detected…then move on to the regular code:

  1. Environment Issue:
    The “The environment is managed externally” message typically appears when you’re trying to install packages using pip in a Python environment that’s managed by another tool. To resolve this:

    a) If you’re using a virtual environment, activate it before installing packages.
    b) If you’re not intentionally using a managed environment, you might want to use the system’s default Python. Try using sudo before your pip command:

    sudo pip3 install sparkfun-qwiic
    
  2. Connecting the Sensor:

    • Connect the Qwiic MicroPressure Sensor to your Raspberry Pi using the Qwiic connector or by wiring it directly to the I2C pins.
    • Enable I2C on your Raspberry Pi if you haven’t already (use sudo raspi-config).
  3. Installing Required Libraries:
    Try installing the required libraries using these commands:

    sudo pip3 install sparkfun-qwiic
    sudo pip3 install sparkfun-qwiic-micropressure
    
  4. Using the Sensor:
    After installation, you can use a Python script like this to read from the sensor:

    import qwiic_micropressure
    import time
    
    sensor = qwiic_micropressure.QwiicMicroPressure()
    
    if sensor.begin() == False:
        print("Sensor not connected. Please check wiring.")
        exit()
    
    while True:
        pressure = sensor.pressure()
        print(f"Pressure: {pressure} kPa")
        time.sleep(1)
    
  5. Troubleshooting:

    • Ensure your Raspberry Pi is up to date: sudo apt update && sudo apt upgrade
    • Check I2C is enabled: ls /dev/i2c* should show at least one device
    • Verify the sensor is detected: i2cdetect -y 1

If you continue to have issues, you might want to consider using a virtual environment for your project