MS5837 for measuring the draft of the boat?

So cool that it works!

I don’t think the map function is needed here, and it uses integer math which is probably why your readings are discretized. Just multiply.

int delayMicros = depth*1000.0; // 10 us/cm is 1000 us/m

@meholden - I followed your suggestion to replace the map function by simple math - it worked out perfect.

digital gauge serial port MP (cm H2O)
0.03 36 1632 (?)
40 10 10
80 51 51
120 89 90
200 170 171
304 276 278
416 389 379
605 580 586
802 779 787

This is so cool! Thanks a lot for your valuable advice.
Readings are even accurate over a range of 800 cm H2O - which is probably beyond the crush depth of the hull. I´m curious how you would interprete the misreading at ambient pressure.

it looks like its overflowing because its a float 0.03 is getting converted to integer, I would just add something like

if (reading< 1){reading=0;}

before you convert it to depth.

Hi @geofrancis - unfortunately adding the "if (reading< 1){reading=0;} " term doesnt solve the issue - atmospheric pressure still gives a negative depth value. It´s the same when I connect the bar30 but depth values are off by 9.800 cm H2O at atmospheric pressure. Atmospheric pressure needs to be set as zero as described by @McKey at the beginning of this topic.

Below is the latest version of the script.
MS5837-02BA_PWM.cpp (1.1 KB)

1 Like

Lol when people find my github…

1 Like

I suspect the ambient pressure strangeness is because the pressure reading is negative.

A negative pressure will convert to a negative pulse time, which will be implemented as a large positive pulse time. Probably the FC times out on that pulse (gives up waiting for the end) and gives you the max. E.g.delayMicroseconds(-100) is the same as delayMicroseconds(65436) if I’ve done the math correctly

The fix is to make sure you don’t try to send negative pulse times, it is a good idea to set an upper limit too. You can use if tests or the arduino constrain function:

// Convert depth to a delay in microseconds
int delayMicros = depth * 1000.0; // 10 us/cm is 1000 us/m

// limit pulse time to reasonable values
delayMicros = constrain(delayMicros, 0, 10000);

I’m a little confused by the serial port printout of 36, that should show a negative value and it doesn’t, so there may be a different issue.

@meholden serial output is indeed minus 36

@meholden YES again - including the if function solved the problem with negative depth readings - with the bar 30 depth values are now plus 1 cm H2O at atmospheric pressure - with increasing pressure depth values increase up to 8 m H2O - deviation is constant @ + 5 cm - which is more than neglegible - with negative pressure depth values remain at atmospheric depth of 1 cm H2O.

I appreciate your skilled help as well as the suggestions from @geofrancis to get the script working. The PWM script also saves one serial port for an underwater sonar.

I let you know what happens with the bar02.

1 Like

script also works flawlessly with the bar02…

besides…today I got myself a waveshare rp2040 zero because it obviates the need for logic level shifting and comes with a tiny form factor - which VSC extension would you recommend to get the baro script onto the board?

something just occurred to me, I have tried what your doing before when I was trying to convert a pressure sensor into a rangefinder, the problem I ran into was depth is a negative value originating from the surface, where a rangefinder is a positive value originating from the bottom. I couldn’t find a way around this problem so it couldnt be used for depth hold. I ended up making a custom sub controller with ardurover for just navigation.

The only way I have found to feed barometric data into ardupilot is using MSP then treat the sub as an underwater plane, but there is almost zero documentation on MSP and when I asked over at Inav for MSP documentation they were less than helpful, all i got from their developers was “look at the source code”.

The rp2040 seems like a good choice for a 3.3V microcontroller. I don’t use VSC, I just use the arduino IDE with the rp2040 board add-on. Here’s a tutorial on using the arduino IDE with rp2040 if you want to try that approach.