├── .gitattributes ├── .github └── workflows │ └── compile-sketch.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── examples └── SparkFun_MS5803_Demo │ └── SparkFun_MS5803_Demo.ino ├── keywords.txt ├── library.properties └── src ├── SparkFun_MS5803_I2C.cpp └── SparkFun_MS5803_I2C.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.github/workflows/compile-sketch.yml: -------------------------------------------------------------------------------- 1 | name: Compile Sketch 2 | 3 | on: 4 | # - push 5 | pull_request: 6 | workflow_dispatch: 7 | 8 | jobs: 9 | compile-sketch: 10 | runs-on: ubuntu-latest 11 | 12 | strategy: 13 | fail-fast: false 14 | 15 | matrix: 16 | board: 17 | # Uno 18 | # https://github.com/arduino/ArduinoCore-avr/blob/master/boards.txt 19 | - fqbn: arduino:avr:uno 20 | platforms: | 21 | - name: arduino:avr 22 | source-url: https://downloads.arduino.cc/packages/package_index.json 23 | 24 | # ESP32 25 | # https://github.com/espressif/arduino-esp32/blob/master/boards.txt 26 | - fqbn: esp32:esp32:esp32 27 | platforms: | 28 | - name: esp32:esp32 29 | source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 30 | 31 | # ESP32-S2 32 | # https://github.com/espressif/arduino-esp32/blob/master/boards.txt 33 | - fqbn: esp32:esp32:esp32s2 34 | platforms: | 35 | - name: esp32:esp32 36 | source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 37 | 38 | # ESP32-C3 39 | # https://github.com/espressif/arduino-esp32/blob/master/boards.txt 40 | - fqbn: esp32:esp32:esp32c3 41 | platforms: | 42 | - name: esp32:esp32 43 | source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 44 | 45 | # Artemis / Apollo3 46 | # https://github.com/sparkfun/Arduino_Apollo3/blob/main/boards.txt 47 | - fqbn: SparkFun:apollo3:sfe_artemis_atp 48 | platforms: | 49 | - name: SparkFun:apollo3 50 | source-url: https://raw.githubusercontent.com/sparkfun/Arduino_Apollo3/master/package_sparkfun_apollo3_index.json 51 | 52 | # ESP8266 53 | # https://github.com/esp8266/Arduino/blob/master/boards.txt 54 | - fqbn: esp8266:esp8266:thingdev 55 | platforms: | 56 | - name: esp8266:esp8266 57 | source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json 58 | 59 | # SAMD21 60 | # https://github.com/arduino/ArduinoCore-samd/blob/master/boards.txt 61 | - fqbn: arduino:samd:mkr1000 62 | platforms: | 63 | - name: arduino:samd 64 | # source-url: https://downloads.arduino.cc/packages/package_index.json 65 | 66 | # Nano BLE 33 / nRF52840 67 | # https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt 68 | - fqbn: arduino:mbed:nano33ble 69 | platforms: | 70 | - name: arduino:mbed 71 | # source-url: https://downloads.arduino.cc/packages/package_index.json 72 | 73 | # RP2040 74 | # https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt 75 | - fqbn: rp2040:rp2040:sparkfun_promicrorp2040 76 | platforms: | 77 | - name: rp2040:rp2040 78 | source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json 79 | 80 | # STM32 81 | # https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt 82 | - fqbn: STMicroelectronics:stm32:GenF4 83 | platforms: | 84 | - name: STMicroelectronics:stm32 85 | source-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json 86 | 87 | steps: 88 | - name: Checkout 89 | uses: actions/checkout@v2 90 | 91 | - name: Branch name 92 | run: echo running on branch ${GITHUB_REF##*/} 93 | 94 | - name: Compile Sketch 95 | uses: arduino/compile-sketches@v1 96 | with: 97 | platforms: ${{ matrix.board.platforms }} 98 | fqbn: ${{ matrix.board.fqbn }} 99 | libraries: | 100 | - source-path: ./ 101 | sketch-paths: | 102 | - examples/SparkFun_MS5803_Demo 103 | enable-warnings-report: true 104 | enable-deltas-report: true 105 | # verbose: true 106 | 107 | # outputs: 108 | # report-artifact-name: ${{ steps.report-artifact-name.outputs.report-artifact-name }} 109 | 110 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files 58 | *.b#? 59 | *.s#? 60 | 61 | 62 | ################# 63 | ## Visual Studio 64 | ################# 65 | 66 | ## Ignore Visual Studio temporary files, build results, and 67 | ## files generated by popular Visual Studio add-ons. 68 | 69 | # User-specific files 70 | *.suo 71 | *.user 72 | *.sln.docstates 73 | 74 | # Build results 75 | [Dd]ebug/ 76 | [Rr]elease/ 77 | *_i.c 78 | *_p.c 79 | *.ilk 80 | *.meta 81 | *.obj 82 | *.pch 83 | *.pdb 84 | *.pgc 85 | *.pgd 86 | *.rsp 87 | *.sbr 88 | *.tlb 89 | *.tli 90 | *.tlh 91 | *.tmp 92 | *.vspscc 93 | .builds 94 | *.dotCover 95 | 96 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 97 | #packages/ 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opensdf 104 | *.sdf 105 | 106 | # Visual Studio profiler 107 | *.psess 108 | *.vsp 109 | 110 | # ReSharper is a .NET coding add-in 111 | _ReSharper* 112 | 113 | # Installshield output folder 114 | [Ee]xpress 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish 128 | 129 | # Others 130 | [Bb]in 131 | [Oo]bj 132 | sql 133 | TestResults 134 | *.Cache 135 | ClientBin 136 | stylecop.* 137 | ~$* 138 | *.dbmdl 139 | Generated_Code #added for RIA/Silverlight projects 140 | 141 | # Backup & report files from converting an old project file to a newer 142 | # Visual Studio version. Backup files are not needed, because we have git ;-) 143 | _UpgradeReport_Files/ 144 | Backup*/ 145 | UpgradeLog*.XML 146 | 147 | 148 | ############ 149 | ## Windows 150 | ############ 151 | 152 | # Windows image file caches 153 | Thumbs.db 154 | 155 | # Folder config file 156 | Desktop.ini 157 | 158 | 159 | ############# 160 | ## Python 161 | ############# 162 | 163 | *.py[co] 164 | 165 | # Packages 166 | *.egg 167 | *.egg-info 168 | dist 169 | build 170 | eggs 171 | parts 172 | bin 173 | var 174 | sdist 175 | develop-eggs 176 | .installed.cfg 177 | 178 | # Installer logs 179 | pip-log.txt 180 | 181 | # Unit test / coverage reports 182 | .coverage 183 | .tox 184 | 185 | #Translations 186 | *.mo 187 | 188 | #Mr Developer 189 | .mr.developer.cfg 190 | 191 | # Mac crap 192 | .DS_Store 193 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | SparkFun License Information 2 | ============================ 3 | 4 | SparkFun uses two different licenses for our files — one for hardware and one for code. 5 | 6 | Hardware 7 | --------- 8 | 9 | **SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** 10 | 11 | Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). 12 | 13 | You are free to: 14 | 15 | Share — copy and redistribute the material in any medium or format 16 | Adapt — remix, transform, and build upon the material 17 | for any purpose, even commercially. 18 | The licensor cannot revoke these freedoms as long as you follow the license terms. 19 | Under the following terms: 20 | 21 | Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 22 | ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. 23 | No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 24 | Notices: 25 | 26 | You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. 27 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 28 | 29 | 30 | Code 31 | -------- 32 | 33 | **SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).** 34 | 35 | The MIT License (MIT) 36 | 37 | Copyright (c) 2015 SparkFun Electronics 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all 47 | copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 55 | SOFTWARE. 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SparkFun Pressure Sensor Breakout - MS5803-14BA Arduino Library 2 | =============================================================== 3 | 4 | [![SparkFun Pressure Sensor Breakout - MS5803-14BA](https://cdn.sparkfun.com//assets/parts/9/8/1/1/12909-01a.jpg)](https://www.sparkfun.com/products/12909) 5 | 6 | [*SparkFun Pressure Sensor Breakout - MS5803-14BA (SEN-12909)*](https://www.sparkfun.com/products/12909) 7 | 8 | Arduino library gives user basic communication from sensor to microcontroller via I2C protocol. 9 | 10 | Repository Contents 11 | ------------------- 12 | 13 | * **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. 14 | * **/src** - Source files for the library (.cpp, .h). 15 | * **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. 16 | * **library.properties** - General library properties for the Arduino package manager. 17 | 18 | Documentation 19 | -------------- 20 | 21 | * **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. 22 | * **[Product Repository](https://github.com/sparkfun/MS5803-14BA_Breakout)** - Main repository (including hardware files) for the MS5803-14BA sensor. 23 | * **[Hookup Guide](https://learn.sparkfun.com/tutorials/ms5803-14ba-pressure-sensor-hookup-guide)** - Basic hookup guide for the MS5803-14BA sensor. 24 | 25 | Products that use this Library 26 | --------------------------------- 27 | 28 | * [SEN-12909](https://www.sparkfun.com/products/12909)- Pressure sensor breakout. 29 | 30 | Version History 31 | --------------- 32 | 33 | * [v1.1.4](https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library/releases/tag/v1.1.4) - Fix the crash caused by calling reset before begin 34 | * [v1.1.3](https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library/releases/tag/v1.1.3) - Add TwoWire as begin parameter. Library can now use any TwoWire port 35 | * [V_1.1.2](https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library/releases/tag/V_1.1.2) - Fix issue with initializing I2C Bus on SAMD21, ESP8266 36 | * [V_1.1.1](https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library/releases/tag/V_1.1.1) - Fixes a few integer math bugs in previous version 37 | * [V_1.1.0](https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library/releases/tag/V_1.1.0) - Initial Arduino library manager version 38 | 39 | License Information 40 | ------------------- 41 | 42 | This product is _**open source**_! 43 | 44 | Please see [LICENSE.md](./LICENSE.md) for more details. 45 | 46 | Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license. 47 | 48 | Distributed as-is; no warranty is given. 49 | 50 | - Your friends at SparkFun. 51 | -------------------------------------------------------------------------------- /examples/SparkFun_MS5803_Demo/SparkFun_MS5803_Demo.ino: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | SparkFun_MS5803_Demo.ino 4 | Demo Program for MS5803 pressure sensors. 5 | Casey Kuhns @ SparkFun Electronics 6 | 7/20/2014 7 | https://github.com/sparkfun/MS5803-14BA_Breakout/ 8 | 9 | The MS58XX MS57XX and MS56XX by Measurement Specialties is a low cost I2C pressure 10 | sensor. This sensor can be used in weather stations and for altitude 11 | estimations. It can also be used underwater for water depth measurements. 12 | 13 | Resources: 14 | This library uses the Arduino Wire.h to complete I2C transactions. 15 | 16 | Development environment specifics: 17 | IDE: Arduino 1.0.5 18 | Hardware Platform: Arduino Pro 3.3V/8MHz 19 | T5403 Breakout Version: 1.0 20 | 21 | **Updated for Arduino 1.8.8 5/2019** 22 | 23 | License: Please see LICENSE.md for more details. 24 | 25 | Distributed as-is; no warranty is given. 26 | ******************************************************************************/ 27 | 28 | #include 29 | #include // Click here to get the library: http://librarymanager/All#SparkFun_MS5803-14BA 30 | 31 | // Begin class with selected address 32 | // available addresses (selected by jumper on board) 33 | // default is ADDRESS_HIGH 34 | 35 | // ADDRESS_HIGH = 0x76 36 | // ADDRESS_LOW = 0x77 37 | MS5803 sensor(ADDRESS_HIGH); // Instantiate the sensor using ADDRESS_HIGH 38 | //MS5803 sensor; // Or, from v1.1.3, we can also do this. The address will default to ADDRESS_HIGH 39 | 40 | //Create variables to store results 41 | float temperature_c, temperature_f; 42 | double pressure_abs, pressure_relative, altitude_delta, pressure_baseline; 43 | 44 | // Create Variable to store altitude in (m) for calculations; 45 | double base_altitude = 1655.0; // Altitude of SparkFun's HQ in Boulder, CO. in (m) 46 | 47 | void setup() { 48 | 49 | // Start your preferred I2C object 50 | Wire.begin(); 51 | 52 | //Initialize Serial Monitor 53 | Serial.begin(9600); 54 | 55 | //Retrieve calibration constants for conversion math. 56 | sensor.reset(); 57 | 58 | // Begin communication with the sensor 59 | sensor.begin(); // Begin the sensor using Wire 60 | //sensor.begin(Wire, 0x76); // Or, from v1.1.3, we can also do this 61 | 62 | pressure_baseline = sensor.getPressure(ADC_4096); 63 | 64 | } 65 | 66 | void loop() { 67 | 68 | // To measure to higher degrees of precision use the following sensor settings: 69 | // ADC_256 70 | // ADC_512 71 | // ADC_1024 72 | // ADC_2048 73 | // ADC_4096 74 | 75 | // Read temperature from the sensor in deg C. This operation takes about 76 | temperature_c = sensor.getTemperature(CELSIUS, ADC_512); 77 | 78 | // Read temperature from the sensor in deg F. Converting 79 | // to Fahrenheit is not internal to the sensor. 80 | // Additional math is done to convert a Celsius reading. 81 | temperature_f = sensor.getTemperature(FAHRENHEIT, ADC_512); 82 | 83 | // Read pressure from the sensor in mbar. 84 | pressure_abs = sensor.getPressure(ADC_4096); 85 | 86 | // Let's do something interesting with our data. 87 | 88 | // Convert abs pressure with the help of altitude into relative pressure 89 | // This is used in Weather stations. 90 | pressure_relative = sealevel(pressure_abs, base_altitude); 91 | 92 | // Taking our baseline pressure at the beginning we can find an approximate 93 | // change in altitude based on the differences in pressure. 94 | altitude_delta = altitude(pressure_abs , pressure_baseline); 95 | 96 | // Report values via UART 97 | Serial.print("Temperature C = "); 98 | Serial.println(temperature_c); 99 | 100 | Serial.print("Temperature F = "); 101 | Serial.println(temperature_f); 102 | 103 | Serial.print("Pressure abs (mbar)= "); 104 | Serial.println(pressure_abs); 105 | 106 | Serial.print("Pressure relative (mbar)= "); 107 | Serial.println(pressure_relative); 108 | 109 | Serial.print("Altitude change (m) = "); 110 | Serial.println(altitude_delta); 111 | 112 | Serial.println(" ");//padding between outputs 113 | 114 | delay(1000); 115 | 116 | } 117 | 118 | 119 | // Thanks to Mike Grusin for letting me borrow the functions below from 120 | // the BMP180 example code. 121 | 122 | double sealevel(double P, double A) 123 | // Given a pressure P (mbar) taken at a specific altitude (meters), 124 | // return the equivalent pressure (mbar) at sea level. 125 | // This produces pressure readings that can be used for weather measurements. 126 | { 127 | return (P / pow(1 - (A / 44330.0), 5.255)); 128 | } 129 | 130 | 131 | double altitude(double P, double P0) 132 | // Given a pressure measurement P (mbar) and the pressure at a baseline P0 (mbar), 133 | // return altitude (meters) above baseline. 134 | { 135 | return (44330.0 * (1 - pow(P / P0, 1 / 5.255))); 136 | } 137 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Datatypes (KEYWORD1) 3 | ####################################### 4 | 5 | MS5803 KEYWORD1 6 | 7 | ####################################### 8 | # Methods and Functions (KEYWORD2) 9 | ####################################### 10 | 11 | getTemperature KEYWORD2 12 | getPressure KEYWORD2 13 | reset KEYWORD2 14 | 15 | ####################################### 16 | # Constants (LITERAL1) 17 | ####################################### 18 | 19 | 20 | FAHRENHEIT LITERAL1 21 | CELSIUS LITERAL1 22 | 23 | ADDRESS_HIGH LITERAL1 24 | ADDRESS_LOW LITERAL1 25 | 26 | ADC_256 LITERAL1 27 | ADC_512 LITERAL1 28 | ADC_1024 LITERAL1 29 | ADC_2048 LITERAL1 30 | ADC_4096 LITERAL1 31 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SparkFun MS5803-14BA Pressure Sensor 2 | version=1.1.4 3 | author=SparkFun Electronics 4 | maintainer=SparkFun Electronics 5 | sentence=Library for MS5803-14BA Pressure Sensor. 6 | paragraph=Provides I2C communication protocol for measuring wather depth, altitude, or other pressure readings. 7 | category=Sensors 8 | url=https://github.com/sparkfun/SparkFun_MS5803-14BA_Breakout_Arduino_Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/SparkFun_MS5803_I2C.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | MS5803_I2C.cpp 3 | Library for MS5803 pressure sensor. 4 | Casey Kuhns @ SparkFun Electronics 5 | 6/26/2014 6 | https://github.com/sparkfun/MS5803-14BA_Breakout 7 | 8 | The MS58XX MS57XX and MS56XX by Measurement Specialties is a low cost I2C pressure 9 | sensor. This sensor can be used in weather stations and for altitude 10 | estimations. It can also be used underwater for water depth measurements. 11 | 12 | In this file are the functions in the MS5803 class 13 | 14 | Resources: 15 | This library uses the Arduino Wire.h to complete I2C transactions. 16 | 17 | Development environment specifics: 18 | IDE: Arduino 1.0.5 19 | Hardware Platform: Arduino Pro 3.3V/8MHz 20 | MS5803 Breakout Version: 1.0 21 | 22 | **Updated for Arduino 1.8.8 5/2019** 23 | 24 | License: Please see LICENSE.md for more details. 25 | 26 | Distributed as-is; no warranty is given. 27 | ******************************************************************************/ 28 | 29 | #include "SparkFun_MS5803_I2C.h" 30 | 31 | MS5803::MS5803(ms5803_addr address) 32 | // Base library type I2C 33 | { 34 | _address = (uint8_t)address; // set interface used for communication 35 | } 36 | 37 | void MS5803::reset(void) 38 | // Reset device I2C 39 | { 40 | if (_i2cPort == nullptr) 41 | return; 42 | 43 | sendCommand(CMD_RESET); 44 | sensorWait(3); 45 | } 46 | 47 | uint8_t MS5803::begin(TwoWire &wirePort, uint8_t address) 48 | { 49 | _address = address; // set interface used for communication 50 | return begin(wirePort); 51 | } 52 | 53 | uint8_t MS5803::begin(TwoWire &wirePort) 54 | // Initialize library for subsequent pressure measurements 55 | { 56 | _i2cPort = &wirePort; // Grab which port the user wants us to use 57 | 58 | reset(); // Reset the sensor to ensure the coefficients are loaded correctly 59 | 60 | uint8_t i; 61 | for (i = 0; i <= 7; i++) 62 | { 63 | sendCommand(CMD_PROM + (i * 2)); 64 | _i2cPort->requestFrom(_address, (uint8_t)2); 65 | uint8_t highByte = _i2cPort->read(); 66 | uint8_t lowByte = _i2cPort->read(); 67 | coefficient[i] = (highByte << 8) | lowByte; 68 | // Uncomment below for debugging output. 69 | // Serial.print("C"); 70 | // Serial.print(i); 71 | // Serial.print("= "); 72 | // Serial.println(coefficient[i]); 73 | } 74 | 75 | return 0; 76 | } 77 | 78 | float MS5803::getTemperature(temperature_units units, precision _precision) 79 | // Return a temperature reading in either F or C. 80 | { 81 | getMeasurements(_precision); 82 | float temperature_reported; 83 | // If Fahrenheit is selected return the temperature converted to F 84 | if (units == FAHRENHEIT) 85 | { 86 | temperature_reported = _temperature_actual / 100.0f; 87 | temperature_reported = (((temperature_reported)*9) / 5) + 32; 88 | return temperature_reported; 89 | } 90 | 91 | // If Celsius is selected return the temperature converted to C 92 | else 93 | { 94 | temperature_reported = _temperature_actual / 100.0f; 95 | return temperature_reported; 96 | } 97 | } 98 | 99 | float MS5803::getPressure(precision _precision) 100 | // Return a pressure reading 101 | { 102 | getMeasurements(_precision); 103 | float pressure_reported; 104 | pressure_reported = _pressure_actual; // Units: 0.1mbar 105 | pressure_reported = pressure_reported / 10.0f; // Convert to mbar (float) 106 | return pressure_reported; 107 | } 108 | 109 | void MS5803::getMeasurements(precision _precision) 110 | 111 | { 112 | // Retrieve ADC result 113 | int32_t temperature_raw = getADCconversion(TEMPERATURE, _precision); 114 | int32_t pressure_raw = getADCconversion(PRESSURE, _precision); 115 | 116 | // Create Variables for calculations 117 | int32_t temp_calc; 118 | int32_t pressure_calc; 119 | 120 | int32_t dT; 121 | 122 | // Now that we have a raw temperature, let's compute our actual. 123 | dT = temperature_raw - ((int32_t)coefficient[5] << 8); 124 | temp_calc = (((int64_t)dT * coefficient[6]) >> 23) + 2000; 125 | 126 | // TODO TESTING _temperature_actual = temp_calc; 127 | 128 | // Now we have our first order Temperature, let's calculate the second order. 129 | int64_t T2, OFF2, SENS2, OFF, SENS; // working variables 130 | 131 | if (temp_calc < 2000) 132 | // If temp_calc is below 20.0C 133 | { 134 | T2 = 3 * (((int64_t)dT * dT) >> 33); 135 | OFF2 = 3 * ((temp_calc - 2000) * (temp_calc - 2000)) / 2; 136 | SENS2 = 5 * ((temp_calc - 2000) * (temp_calc - 2000)) / 8; 137 | 138 | if (temp_calc < -1500) 139 | // If temp_calc is below -15.0C 140 | { 141 | OFF2 = OFF2 + 7 * ((temp_calc + 1500) * (temp_calc + 1500)); 142 | SENS2 = SENS2 + 4 * ((temp_calc + 1500) * (temp_calc + 1500)); 143 | } 144 | } 145 | else 146 | // If temp_calc is above 20.0C 147 | { 148 | T2 = 7 * ((uint64_t)dT * dT) / pow(2, 37); 149 | OFF2 = ((temp_calc - 2000) * (temp_calc - 2000)) / 16; 150 | SENS2 = 0; 151 | } 152 | 153 | // Now bring it all together to apply offsets 154 | 155 | OFF = ((int64_t)coefficient[2] << 16) + (((coefficient[4] * (int64_t)dT)) >> 7); 156 | SENS = ((int64_t)coefficient[1] << 15) + (((coefficient[3] * (int64_t)dT)) >> 8); 157 | 158 | temp_calc = temp_calc - T2; 159 | OFF = OFF - OFF2; 160 | SENS = SENS - SENS2; 161 | 162 | // Now lets calculate the pressure 163 | 164 | pressure_calc = (((SENS * pressure_raw) / 2097152) - OFF) / 32768; 165 | 166 | _temperature_actual = temp_calc; 167 | _pressure_actual = pressure_calc; // 10;// pressure_calc; 168 | } 169 | 170 | uint32_t MS5803::getADCconversion(measurement _measurement, precision _precision) 171 | // Retrieve ADC measurement from the device. 172 | // Select measurement type and precision 173 | { 174 | if (_i2cPort == nullptr) 175 | return 0; 176 | 177 | uint32_t result; 178 | uint8_t highByte = 0, midByte = 0, lowByte = 0; 179 | 180 | sendCommand(CMD_ADC_CONV + _measurement + _precision); 181 | // Wait for conversion to complete 182 | sensorWait(1); // general delay 183 | switch (_precision) 184 | { 185 | case ADC_256: 186 | sensorWait(1); 187 | break; 188 | case ADC_512: 189 | sensorWait(3); 190 | break; 191 | case ADC_1024: 192 | sensorWait(4); 193 | break; 194 | case ADC_2048: 195 | sensorWait(6); 196 | break; 197 | case ADC_4096: 198 | sensorWait(10); 199 | break; 200 | } 201 | 202 | sendCommand(CMD_ADC_READ); 203 | _i2cPort->requestFrom(_address, (uint8_t)3); 204 | 205 | while (_i2cPort->available()) 206 | { 207 | highByte = _i2cPort->read(); 208 | midByte = _i2cPort->read(); 209 | lowByte = _i2cPort->read(); 210 | } 211 | 212 | result = ((uint32_t)highByte << 16) | ((uint32_t)midByte << 8) | lowByte; 213 | 214 | return result; 215 | } 216 | 217 | void MS5803::sendCommand(uint8_t command) 218 | { 219 | if (_i2cPort == nullptr) 220 | return; 221 | 222 | _i2cPort->beginTransmission(_address); 223 | _i2cPort->write(command); 224 | _i2cPort->endTransmission(); 225 | } 226 | 227 | void MS5803::sensorWait(uint8_t time) 228 | // Delay function. This can be modified to work outside of Arduino based MCU's 229 | { 230 | delay(time); 231 | } 232 | -------------------------------------------------------------------------------- /src/SparkFun_MS5803_I2C.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | MS5803_I2C.h 3 | Library for MS5803 pressure sensors. 4 | Casey Kuhns @ SparkFun Electronics 5 | 6/26/2014 6 | https://github.com/sparkfun/MS5803-14BA_Breakout 7 | 8 | The MS58XX MS57XX and MS56XX by Measurement Specialties is a low cost I2C pressure 9 | sensor. This sensor can be used in weather stations and for altitude 10 | estimations. It can also be used underwater for water depth measurements. 11 | 12 | In this file are the function prototypes in the MS5803 class 13 | 14 | Resources: 15 | This library uses the Arduino Wire.h to complete I2C transactions. 16 | 17 | Development environment specifics: 18 | IDE: Arduino 1.0.5 19 | Hardware Platform: Arduino Pro 3.3V/8MHz 20 | MS5803 Breakout Version: 1.0 21 | 22 | **Updated for Arduino 1.6.4 5/2015** 23 | 24 | License: Please see LICENSE.md for more details. 25 | 26 | Distributed as-is; no warranty is given. 27 | ******************************************************************************/ 28 | 29 | #ifndef SparkFun_MS5803_I2C_h 30 | #define SparkFun_MS5803_I2C_h 31 | 32 | #include 33 | #include "Wire.h" 34 | 35 | // Define units for conversions. 36 | enum temperature_units 37 | { 38 | CELSIUS, 39 | FAHRENHEIT, 40 | }; 41 | 42 | // Define measurement type. 43 | enum measurement 44 | { 45 | PRESSURE = 0x00, 46 | TEMPERATURE = 0x10 47 | }; 48 | 49 | // Define constants for Conversion precision 50 | enum precision 51 | { 52 | ADC_256 = 0x00, 53 | ADC_512 = 0x02, 54 | ADC_1024 = 0x04, 55 | ADC_2048 = 0x06, 56 | ADC_4096 = 0x08 57 | }; 58 | 59 | // Define address choices for the device (I2C mode) 60 | enum ms5803_addr 61 | { 62 | ADDRESS_HIGH = 0x76, 63 | ADDRESS_LOW = 0x77 64 | }; 65 | 66 | // Commands 67 | #define CMD_RESET 0x1E // reset command 68 | #define CMD_ADC_READ 0x00 // ADC read command 69 | #define CMD_ADC_CONV 0x40 // ADC conversion command 70 | 71 | #define CMD_PROM 0xA0 // Coefficient location 72 | 73 | class MS5803 74 | { 75 | public: 76 | MS5803(ms5803_addr address = ADDRESS_HIGH); 77 | void reset(void); // Reset device 78 | uint8_t begin(TwoWire &wirePort = Wire); // Collect coefficients from sensor 79 | uint8_t begin(TwoWire &wirePort, uint8_t address); // Collect coefficients from sensor 80 | 81 | // Return calculated temperature from sensor 82 | float getTemperature(temperature_units units, precision _precision); 83 | // Return calculated pressure from sensor 84 | float getPressure(precision _precision); 85 | 86 | private: 87 | int32_t _temperature_actual; 88 | int32_t _pressure_actual; 89 | 90 | TwoWire *_i2cPort = nullptr; // The generic connection to user's chosen I2C hardware 91 | uint8_t _address; // Variable used to store I2C device address. 92 | uint16_t coefficient[8]; // Coefficients; 93 | 94 | void getMeasurements(precision _precision); 95 | 96 | void sendCommand(uint8_t command); // General I2C send command function 97 | uint32_t getADCconversion(measurement _measurement, precision _precision); // Retrieve ADC result 98 | 99 | void sensorWait(uint8_t time); // General delay function see: delay() 100 | }; 101 | 102 | #endif --------------------------------------------------------------------------------