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()