{"id":23835165,"url":"https://github.com/titonbarua/micropython-ms5837-depth-sensor","last_synced_at":"2026-05-13T21:35:21.045Z","repository":{"id":270552305,"uuid":"907265890","full_name":"titonbarua/micropython-ms5837-depth-sensor","owner":"titonbarua","description":"Library to access MS5837 depth sensor from micropython.","archived":false,"fork":false,"pushed_at":"2025-01-01T18:43:14.000Z","size":5945,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T03:29:36.493Z","etag":null,"topics":["depth-sensor","micropython"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/titonbarua.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-23T07:46:06.000Z","updated_at":"2025-01-01T18:42:22.000Z","dependencies_parsed_at":"2025-01-01T09:24:38.430Z","dependency_job_id":"7d8630cb-cc54-4be2-9ec5-938c4f7fe80a","html_url":"https://github.com/titonbarua/micropython-ms5837-depth-sensor","commit_stats":null,"previous_names":["titonbarua/micropython-ms5837-depth-sensor"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/titonbarua/micropython-ms5837-depth-sensor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titonbarua%2Fmicropython-ms5837-depth-sensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titonbarua%2Fmicropython-ms5837-depth-sensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titonbarua%2Fmicropython-ms5837-depth-sensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titonbarua%2Fmicropython-ms5837-depth-sensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/titonbarua","download_url":"https://codeload.github.com/titonbarua/micropython-ms5837-depth-sensor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titonbarua%2Fmicropython-ms5837-depth-sensor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33001254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-13T13:14:54.681Z","status":"ssl_error","status_checked_at":"2026-05-13T13:14:51.610Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["depth-sensor","micropython"],"created_at":"2025-01-02T15:27:08.884Z","updated_at":"2026-05-13T21:35:21.011Z","avatar_url":"https://github.com/titonbarua.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Micropython Library for Reading MS5837-XXXX Pressure/Water-depth Sensors\n\nThis library implements a few micropython classes for reading data out of\npopular MS5837 family pressure sensors directly into a micro-controller. These\ndevices are commonly found in depth sensors made by BlueRobotics.\n\n## Features\n\n- API-s for both blocking and asynchronous reads with asyncio.\n- Configurable oversampling rates, as supported by the sensors.\n- Minimal dynamic memory allocation for predictable performance.\n\n## Supported variants\n\n- MS5837-02BA\n- MS5837-30BA\n\n## Installation\n\nYou can install the library directly into the device using `mpremote` tool -\n\n```bash\nmpremote mip install github:titonbarua/micropython-ms5837-depth-sensor@main\n```\n\n## Example usage\n\n### Pressure and temperature measurement\n\nReading data in blocking mode -\n\n```python\nimport time\nfrom machine import I2C\nfrom ms5837.bar30 import MS5837SensorBar30\n\n_SCL_PIN = \"GP19\"\n_SDA_PIN = \"GP18\"\n_OSR_RATE = 4096\n\n# Create I2C object.\ni2c_obj = I2C(1, freq=400000, sda=_SDA_PIN, scl=_SCL_PIN)\n\n# Create and initialize sensor object.\nsensor = MS5837SensorBar30(\n    i2c_obj,\n    pressure_osr=_OSR_RATE,\n    temperature_osr=_OSR_RATE)\n\nwhile True:\n    pres, temp = sensor.read()\n    print(f\"P: {pres} KPa, T: {temp} deg-C\")\n    time.sleep(1.0)\n```\n\n### Asynchronously reading pressure and temperature\n\nThe `async_read` method can be used to read the sensor data asynchronously.\n\n```python\nimport asyncio\nimport time\nfrom machine import I2C\nfrom ms5837.bar30 import MS5837SensorBar30\n\n_SCL_PIN = \"GP19\"\n_SDA_PIN = \"GP18\"\n_OSR_RATE = 4096\n\n# Create I2C object.\ni2c_obj = I2C(1, freq=400000, sda=_SDA_PIN, scl=_SCL_PIN)\n\n# Create and initialize sensor object.\nsensor = MS5837SensorBar30(\n    i2c_obj,\n    pressure_osr=_OSR_RATE,\n    temperature_osr=_OSR_RATE)\n\nwhile True:\n    task = sensor.async_read()\n    pres, temp = asyncio.run(task)\n    print(f\"P: {pres} KPa, T: {temp} deg-C\")\n    time.sleep(1.0)\n```\n\nThe above example is silly as only a single async task is running. Ideally, you\nshould be having other async co-routines to utilize the time idly spent waiting\nfor the ADC conversion.\n\n\n### Depth estimation\n\nThe `NaiveWaterDepthEstimator` class can be used with a sensor object to estimate\ndepth.\n\n```python\nimport asyncio\nimport time\nfrom machine import I2C\nfrom ms5837.bar30 import MS5837SensorBar30\nfrom ms5837.depth import NaiveWaterDepthEstimator\n\n_SCL_PIN = \"GP19\"\n_SDA_PIN = \"GP18\"\n_OSR_RATE = 4096\n_WATER_DENSITY = 1000\n\n# Create I2C object.\ni2c_obj = I2C(1, freq=400000, sda=_SDA_PIN, scl=_SCL_PIN)\n\n# Create and initialize sensor object.\nsensor = MS5837SensorBar30(\n    i2c_obj,\n    pressure_osr=_OSR_RATE,\n    temperature_osr=_OSR_RATE)\n\ndepth_estimator = NaiveWaterDepthEstimator(sensor, _WATER_DENSITY)\nref_pres = depth_estimator.set_ref_pressure()\nprint(f\"Reference pressure set to: {ref_pres:.3f} KPa\")\n\nwhile True:\n    depth_m = depth_estimator.read_depth()\n    print(\"Depth: {:.2f} cm\".format(depth_m * 100.0))\n    time.sleep(1.0)\n```\n\n`NaiveWaterDepthEstimator` also has an analogous `async_read_depth` method for\nasynchronous reading.\n\n\n## Running the tests\n\n### Hardware setup\n\nThe tests were designed for an RP2040 micro-controller running micropython. Two\nsensors, one MS5837-30BA and another MS5837-02BA were connected to I2C channels\n1 and 0. The pin configuration is described in the beginning of\n[tests/rp2040_tests.py](./tests/rp2040_tests.py) file.\n\n\n### Installation\n\n- Install the requirements with `pip install -r requirements.txt`.\n- Copy the necessary files to micro-controller with command `./tools/copy_files.sh`.\n\n\n### Starting a test\n\nBenchmark blocking reads:\n\n```bash\nmpremote exec 'from rp2040_tests import *; benchmark_blocking_read(SENSOR_BAR30)'\n```\n\nBenchmark asynchronous reads:\n\n```bash\nmpremote exec 'from rp2040_tests import *; benchmark_async_read(SENSOR_BAR02)'\n```\n\nContinuously print pressure and temperature data:\n\n```bash\nmpremote exec 'from rp2040_tests import *; print_data(SENSOR_BAR30)'\n```\n\nContinuously print depth data:\n\n```bash\nmpremote exec 'from rp2040_tests import *; print_data(SENSOR_BAR02)'\n```\n\n\n## Benchmark results\n\n- Micro-controller: RPi Pico 2040 running at stock speed\n- Micropython version: 1.24.1\n\n|Benchmarking setup | Depth sanity checking with a water bottle!|\n|------------------------------------|-------------------------------------------|\n|![Benchmarking Setup](./images/benchmarking_setup.jpg) | ![Depth Sanity Checking](./images/depth_sensor_sanity_checking_rig.jpg)|\n\n\n### Timing of 100 blocking reads for MS5837-02BA\n\n|    OSR |     T_avg[us]|  T_stddev[us]|     T_min[us]|     T_max[us]| Data Rate[Hz]|\n|--------|--------------|--------------|--------------|--------------|--------------|\n|     256|       2516.93|         26.42|       2498.00|       2762.00|        397.31|\n|     512|       3663.57|         21.20|       3644.00|       3851.00|        272.96|\n|    1024|       5800.20|         17.28|       5782.00|       5945.00|        172.41|\n|    2048|      10100.62|         17.86|      10083.00|      10254.00|         99.00|\n|    4096|      18683.49|         17.45|      18666.00|      18832.00|         53.52|\n|    8192|      35553.06|         22.05|      35518.00|      35748.00|         28.13|\n\n\n### Timing of 100 async reads for MS5837-02BA\n\n|    OSR |     T_avg[us]|  T_stddev[us]|     T_min[us]|     T_max[us]| Data Rate[Hz]|\n|--------|--------------|--------------|--------------|--------------|--------------|\n|     256|       2489.46|         64.99|       2446.00|       3118.00|        401.69|\n|     512|       5440.95|        736.74|       4586.00|       7217.00|        183.79|\n|    1024|       7672.49|       1134.89|       6919.00|      11362.00|        130.34|\n|    2048|      10883.84|       1508.17|       9909.00|      15514.00|         91.88|\n|    4096|      18525.77|        597.80|      16941.00|      19479.00|         53.98|\n|    8192|      36372.09|        561.14|      34929.00|      37511.00|         27.49|\n\n\n### Timing of 100 blocking reads for MS5837-30BA\n\n|    OSR |     T_avg[us]|  T_stddev[us]|     T_min[us]|     T_max[us]| Data Rate[Hz]|\n|--------|--------------|--------------|--------------|--------------|--------------|\n|     256|       2606.48|         25.06|       2589.00|       2840.00|        383.66|\n|     512|       3810.43|         18.49|       3792.00|       3969.00|        262.44|\n|    1024|       6033.86|         21.00|       6014.00|       6217.00|        165.73|\n|    2048|      10554.43|         18.85|      10536.00|      10722.00|         94.75|\n|    4096|      19568.32|         23.29|      19546.00|      19784.00|         51.10|\n\n\n### Timing of 100 async reads for MS5837-30BA\n\n|    OSR |     T_avg[us]|  T_stddev[us]|     T_min[us]|     T_max[us]| Data Rate[Hz]|\n|--------|--------------|--------------|--------------|--------------|--------------|\n|     256|       2497.80|         64.99|       2464.00|       3122.00|        400.35|\n|     512|       5255.70|        718.21|       4727.00|       7278.00|        190.27|\n|    1024|       7294.37|        801.82|       6923.00|      10434.00|        137.09|\n|    2048|      11769.16|       1945.96|      10531.00|      19380.00|         84.97|\n|    4096|      20392.47|        640.80|      18933.00|      21213.00|         49.04|\n\nNote that effective data rate is surprisingly low for async reads with some\nnon-extreme OSR values. This is caused by read errors and subsequent retries. My\nsuspicion is on some bugs in the asyncio scheduler or sleep function.\n\n\n## TYMA (Things You May Ask)\n\n### Why not directly use the python library provided by BlueRobotics?\n\nThe library provided by BlueRobotics is not easily modifiable to use in\nmicropython environment, as it depends on `python-smbus` library.\n\n### Do we really need to sample depths at high speeds?\n\nYes. A sensor reading is not the end, rather the beginning of a proper\nestimation. High speed sampling allows one to reduce uncertainty boundary using\nvarious techniques i.e. maximum-a-posteriori(MAP) estimation, extended Kalman\nfilters etc.\n\n### Why is the depth estimation class named `NaiveWaterDepthEstimator`?\n\nDepth estimation with the assumption that water density is constant is a naive\napproach in the context of mobile robotics. The class name prefix is a caution\nagainst forgetting the nuance. In the same spirit, the constructor forces users\nto explicitly specify water density.\n\nFor a proper depth estimation, one should do an initial measurement of salinity\nin the water-body. For each sensor reading, density should be estimated by\nputting salinity and temperature in a model like\n[TEOS-10](https://www.teos-10.org/).\n\nA careful look at the variation of gravitational constant `g` due to location of\nthe experiment might also be necessary.\n\n### Why `enable_sensor_id_check` flag is turned off by default?\n\nSome of the 30BA variant sensors available in the market don't have their sensor\nID properly programmed in the PROM as specified by the data-sheet. I have\nencountered this issue in sensors from BlueRobotics and also from raw sensors\nfrom digikey. Others have reported the same\n[issue](https://github.com/zephyrproject-rtos/zephyr/issues/60950).\n\n\n## License\n\nThis software is distributed under MIT license. Look at the [LICENSE](./LICENSE)\nfile for the terms of distribution.\n\n\n## Copyright\n\n2025 Titon Barua \u003cbaruat@email.sc.edu, titon@vimmaniac.com\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftitonbarua%2Fmicropython-ms5837-depth-sensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftitonbarua%2Fmicropython-ms5837-depth-sensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftitonbarua%2Fmicropython-ms5837-depth-sensor/lists"}