How does this program work?

Hello,

I am working with the SparkFun Pressure Sensor Breakout - MS5803-14BA.

There is an example code on the site that i have attatched, could anyone giving me step by step details on how it works as i'm very intrigued?

Thankyou,

#include <SparkFun_MS5803_I2C.h>


#include <Wire.h>
#include <SparkFun_MS5803_I2C.h>



MS5803 sensor(ADDRESS_HIGH);

float temperature_c, temperature_f;
double pressure_abs, pressure_relative, altitude_delta, pressure_baseline;


double base_altitude = 1655.0; // Altitude of SparkFun's HQ in Boulder, CO. in (m)

void setup() {
    Serial.begin(9600);
    
    sensor.reset();
    sensor.begin();
    
    pressure_baseline = sensor.getPressure(ADC_4096);
    
}

void loop() {
  
  
  temperature_c = sensor.getTemperature(CELSIUS, ADC_512);
  
  
  temperature_f = sensor.getTemperature(FAHRENHEIT, ADC_512);
  
  
  pressure_abs = sensor.getPressure(ADC_4096);
  
 
  pressure_relative = sealevel(pressure_abs, base_altitude);
  
    
  altitude_delta = altitude(pressure_abs , pressure_baseline);
  
 
  Serial.print("Temperature C = ");
  Serial.println(temperature_c);
  
  Serial.print("Temperature F = ");
  Serial.println(temperature_f);
  
  Serial.print("Pressure abs (mbar)= ");
  Serial.println(pressure_abs);
   
  Serial.print("Pressure relative (mbar)= ");
  Serial.println(pressure_relative); 
  
  Serial.print("Altitude change (m) = ");
  Serial.println(altitude_delta); 


  delay(1000);

  }
  

 double sealevel(double P, double A)

{
  return(P/pow(1-(A/44330.0),5.255));
}


double altitude(double P, double P0)

{
  return(44330.0*(1-pow(P/P0,1/5.255)));
}

Code 2.txt (1.43 KB)

Please post your code using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpreted by the forum code as italics or funny emoticons.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

Since you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

A major reason why you are asking this question, is that the programmer did not include any comments (internal documentation) to explain what is going on. That is a big no-no. Your current situation is an example of why it is a no-no. The sealevel() and altitude() functions, in particular, really need some documentation.

What part of it don't you understand?

The program itself is very, very simple - the interesting stuff is done in the library "SparkFun_MS5803_I2C.h".

(It's normally sufficient to include the header files only once)

Your situation is that you have got a sensor SparkFun Pressure Sensor Breakout - MS5803-14BA - SEN-12909 - SparkFun Electronics , hooked it up to an Aruino, demonstrated that it all works, and are now wondering how does its magic ?

Yes, most of it is self explanatory. Which illustrates another principle - self documentation. Usually that just means that the variable names are well chosen so that it is fairly obvious what they are used for.

I'm rather tempted to say that the code itself is the step-by-step details of how it does what it does.

What don't you understand? As with any other sketch, setup() gets done once when the chip is initialized, then loop() is called repeatedly for as long as the chip is turned on.

setup() does some housekeeping for the USB port and your sensor.

loop() reads the sensor, does some calculations, then prints out the results and waits for one second.

Is the issue that you dont understand

return(P/pow(1-(A/44330.0),5.255));

?

This line takes the sensed pressure and the base altitude (in feet, I imagine). it divides the altitude by a magic number, subtracts that from 1, raises that to the power 5.255, and divides the sensed pressure by that to get the sea level.

Why? I don't know. That's down to the details of how this particular sensor responds to temperature and pressure - what particular numbers it gives back.

Personally, I'd go

return exp(ln(P) - ln(1-(A/44330.0) * 5.255)),

which replaces a multiplication by a subtraction. But that's just me.

Does the arduino have a fast floating point log2() function?

See
http://forum.arduino.cc/index.php?topic=308629.0
for discussion on the values in the equations.

G