Installing Bar30 Sensor Library Python

I recently purchased a Bar30 pressure sensor. I am pretty new to raspberry pi’s so I don’t know how to install libraries off of github. Where should I clone the library to and what else do I have to do to install it?

Hi NIW

The Blue Robotics page is fairly straight forward and the sensor works very well on the Raspberry PI 3.

As per the link (GitHub - bluerobotics/ms5837-python: A python module to interface with MS5837-30BA and MS5837-02BA waterproof pressure and temperature sensors.), you just need issue the following command to download the library:

sudo apt-get install python-smbus

After that a simple Python script can be used as per the example (Bar30 Pressure Sensor Documentation). Mine is below - its probably not as technically elegant as it could be but it does the job:

Import ms5837
import time

sensor = ms5837.MS5837_30BA() # Default I2C bus is 1 (Raspberry Pi 3)

# We must initialize the sensor before reading it
if not sensor.init():
        with open ("/home/pi/deepsouth/logs/waterdepth.txt", "w") as waterdepth:
                                waterdepth.write("Not Available")
                                waterdepth.close
        with open ("/home/pi/deepsouth/logs/watertemp.txt", "w") as watertemp:
                                watertemp.write("Not Available")
                                watertemp.close
        with open ("/home/pi/deepsouth/logs/waterpressure.txt", "w") as waterpressure:
                                waterpressure.write("Not Available")
                                waterpressure.close
        print "Water Depth, Temperature, and Pressure not available"
        exit(1)

# Print readings
while True:
        if sensor.read():
                with open ("/home/pi/deepsouth/logs/waterdepth.txt", "w") as waterdepth:
                                waterdepth.write("%s" % (round(sensor.depth(),1)))
                                waterdepth.close
                with open ("/home/pi/deepsouth/logs/watertemp.txt", "w") as watertemp:
                                watertemp.write("%s" % (round(sensor.temperature(),1)))
                                watertemp.close
                with open ("/home/pi/deepsouth/logs/waterpressure.txt", "w") as waterpressure:
                                waterpressure.write("%s" % (round(sensor.pressure(ms5837.UNITS_psi),3)))
                                waterpressure.close
                print ("External sensor recording to logfiles P: %0.3f psi\tT: %0.2f C D: %0.1f m") % (
                sensor.pressure(ms5837.UNITS_psi), # Request psi
                sensor.temperature(), # Default is degrees C (no arguments)
                sensor.depth()) 
                time.sleep(1)

        else:
                print "External sensor read failed!"
                exit(1)

Good luck!

1 Like

I probably should have specified which library, sorry my bad. I have python smbus installed, but idk how to install the ms5837 library.
Thanks.

git clone https://github.com/bluerobotics/ms5837-python

I’ll update the README

Is that all I need to do, or do I need to install it some way?

Edit: Just to clarify, in libraries I have installed previously I had to run a setup.py program before it would work. I am wondering if there are any installation files I should run to get the library to work.

Edit 2: I downloaded the library. When I ran example.py it worked great. However when I tried to import the library from another directory (/home/pi) it could not import.

Also one more thing. I have another i2c device attached (adafruit servo controller hat). I am using it to control my speed controllers. Will that cause any problems with the sensor?

Just to clarify, in libraries I have installed previously I had to run a setup.py program before it would work. I am wondering if there are any installation files I should run to get the library to work.

This repository is not set up as a package, so there is no installation with setup.py.

I downloaded the library. When I ran example.py it worked great. However when I tried to import the library from another directory (/home/pi) it could not import.

As above, it cannot be installed, so you will need to put the files where you want them to live and use them that way. You will need to put them in the same folder as your script if you want to import them as a module.

I have another i2c device attached (adafruit servo controller hat). I am using it to control my speed controllers. Will that cause any problems with the sensor?

There should not be any problems if the devices have unique addresses. Your program will need to arbitrate bus access so that only one sensor is communicating at a time.

Ok thanks for the help!

Guys,

I’m trying to run the python example on Nanopi. But I found this:
Bus 1 is not available.
Available busses are listed as /dev/i2c*
Sensor could not be initialized

I see the device at /dev/i2c-1 any idea ?

Thanks in advance.

Hi Fabio,

Try checking the permissions, maybe your user doesn’t have enough permissions to use it.

I run with root.

Getting error 5:

Traceback (most recent call last):
File “example.py”, line 12, in
if not sensor.init():
File “/home/pi/camera-diver/ms5837-python/ms5837.py”, line 71, in init
self._bus.write_byte(self._MS5837_ADDR, self._MS5837_RESET)
IOError: [Errno 5] Input/output error

I found on dmesg the message below:

[ 1090.848244] i2c i2c-0: mv64xxx_i2c_fsm: Ctlr Error – state: 0x4, status: 0x0, addr: 0x3c, flags: 0x0
[ 1090.865035] i2c i2c-0: mv64xxx_i2c_fsm: Ctlr Error – state: 0x4, status: 0x0, addr: 0x3c, flags: 0x0

Your system has only one i2c bus.
You should use i2c bus zero in that case instead of bus one.

I may be mistaken, it looks like maybe you do have an i2c-1. Try zero, and check your permissions on /dev/i2c.

hey please update the codes for bar 02 sensor there is some error i have fixed and mailed it bluerobotics

here also i’ll put the updated code

import ms5837
import time

sensor = ms5837.MS5837_02BA() # Default I2C bus is 1 (Raspberry Pi 3)

# We must initialize the sensor before reading it
if not sensor.init():
        print("Sensor could not be initialized")
        exit(1)

# Print readings
while True:
        if sensor.read():
                print("P: %0.1f mbar  %0.3f psi\tT: %0.2f C  %0.2f F" % 
                (sensor.pressure(), # Default is mbar (no arguments)
                sensor.pressure(ms5837.UNITS_psi), # Request psi
                sensor.temperature(), # Default is degrees C (no arguments)
                sensor.temperature(ms5837.UNITS_Farenheit))) # Request Farenheit
        else:
                print("Sensor read failed!")
                exit(1)

please update so it can be helpful to the newbies like me

Hi @BSM, welcome to the forum :slight_smile:

The example you’re referring to seems to be fine, but it was written for Python 2, where print was a statement, rather than a function (as it is in Python 3).

That said, Python 2 is old, and adding the print brackets makes it work for Python 3 while still being compatible with Python 2, so I’ve updated that. Thanks for pointing it out :slight_smile:


By the way, I’ve updated your comment to use a code block, so it’s easier to read. There are instructions for how to do that (and much more) in the How to Use the Blue Robotics Forums post :slight_smile:

Sure thanks