HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Feb 21, 2010, 08:59 AM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Discussion
Arduino code for BMP085 pressure sensor?

Greetings,

I'm trying to use the Bosch BMP085 pressure sensor to get precise altitude readings, but so far, I haven't had complete success. Part of that is probably because I'm really just teaching myself programming.

The best info I've found is here:
http://interactive-matter.org/2009/1...sensor-bmp085/...

Towards the bottom of the page, he includes a complete Arduino sketch:
http://interactive-matter.org/wp-con...MP085.pde_.zip

When I run this, I get a value around 70000 for the pressure. Since I live at sea level, it should be around 102000. I noticed that the manufacturers data sheet also shows about 70000 for their example.

Jee Labs shows the correct 102000 value, and mentions some errors in the algorithm, and with higher temps. I tried to look at his code, but I don't really see any difference, though I'm not a software guy by any stretch of the imagination.

http://news.jeelabs.org/2009/02/19/h...bmp085-sensor/

FWIW, I'm using this board, which I had laying around:

http://www.quadroufo.com/product_inf...&osCsid=d617d9...

Any idea why I get 70000 rather than 102000?

Thanks,
Rusty
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Reply With Quote
Sign up now
to remove ads between posts
Old Feb 21, 2010, 02:14 PM
Don L.
Old Lyme, Connecticut
Joined Feb 2006
939 Posts
Quote:
Originally Posted by 13brv3 View Post
Any idea why I get 70000 rather than 102000?
Hi Rusty,

When I was experimenting with coding this for the UAVP, I had an issue with the Bosch algorithm, where the variable wasn't big enough to hold the result. I suspect that is what you are seeing, too.

- Don
dleroi is offline Find More Posts by dleroi
Reply With Quote
Old Feb 21, 2010, 02:21 PM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Quote:
Originally Posted by dleroi View Post
Hi Rusty,

When I was experimenting with coding this for the UAVP, I had an issue with the Bosch algorithm, where the variable wasn't big enough to hold the result. I suspect that is what you are seeing, too.

- Don
Hi Don,

I've seen two problems mentioned. One was an error in the b3 calculation, that throws off the range. I may actually have that figured out, but I'm waiting on an email from one of the guys who posted the code before I claim victory. It does seem to be working correctly now though.

The other problem was when the temp got above about 25C, and that was the one that required a bigger variable I think.

When you were playing with this, did you find that the higher oversampling setting gave a more stable number? To me, it looks as bad, or even worse than the single sample mode. My guess is that the problem is still on my end

Rusty (able to program 0s and 1s at random)
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Reply With Quote
Old Feb 21, 2010, 04:43 PM
Don L.
Old Lyme, Connecticut
Joined Feb 2006
939 Posts
Quote:
Originally Posted by 13brv3 View Post
When you were playing with this, did you find that the higher oversampling setting gave a more stable number? To me, it looks as bad, or even worse than the single sample mode. My guess is that the problem is still on my end
Hi Rusty,

I didn't try different OS settings. I think I just picked "high resolution" (2?) and stuck with it. My goal wasn't actually to read the pressure, rather to figure out why the UAVP code wasn't working.

I recall there being a difference in the b3 calculation between the data sheet and the API that I got from Bosch. I used the API.

In the API it is: b3 = ((ac1 * 4 + x3) << oss + 2) >> 2, whereas the data sheet has: b3 = ((ac1 * 4 + x3) << oss + 2) /4.

The API calc makes sense (to me), in that it seems to accurately adjust for the oversampling setting. i.e., for no oversampling (oss=0), it divides by 4 (<< 0+2) then multiplies by 4 (>> 2), which leaves it where it was; but for 4x oversampling, it divides by 16 (<< 2+2) then multiplies by 4, which results in it being divided by 4.

Is this what you found?

- Don
dleroi is offline Find More Posts by dleroi
Reply With Quote
Old Feb 21, 2010, 05:39 PM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Hi Don,

That's definitely the area in question, and all this bit shifting is giving me a headache. I didn't even know what those symbols were until this morning

The code I was using on the Arduino had the option to set the sampling mode, and used the same line as you listed for the API. It gave 70k readings for all sampling modes 0-3.

I found a correction online, which changed that line to:
b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;

This gives good readings for the single sample mode, which is all that was available in that version of the code. I'm not dead sure this is right, but it seems to work for the single sample mode. Unfortunately, it gives different results for every other mode.

After playing around a bit, I found that the shift needs to be different for each mode, and the easiest way for me to fix it was:

if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) << 1;
if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 1;
if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;

This gives numbers in all modes that are within 100 Pascals of what I convert from the WX station on the same desk. I'm still thinking that last "+2" may not belong there. I tried removing it, and can't see any difference in the numbers, so I'm betting it doesn't belong. With only one shift, you see how the direction of the shift has to change.

Rusty
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Last edited by 13brv3; Feb 21, 2010 at 05:47 PM.
Reply With Quote
Old Feb 21, 2010, 06:00 PM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Here's the ticket

b3 = (((int32_t) ac1 * 4 + x3)<<oversampling_setting) >> 2;

This works, and must have been what they originally intended. Your comment about shifting one way, and then the other made me realize what was needed.

Thanks,
Rusty (my brain hurts)
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Reply With Quote
Old Feb 21, 2010, 06:44 PM
Don L.
Old Lyme, Connecticut
Joined Feb 2006
939 Posts
Quote:
Originally Posted by 13brv3 View Post
Here's the ticket

b3 = (((int32_t) ac1 * 4 + x3)<<oversampling_setting) >> 2;

This works, and must have been what they originally intended. Your comment about shifting one way, and then the other made me realize what was needed.

Thanks,
Rusty (my brain hurts)
Rusty,

Great job.

How does your code derive "up"? I'm guessing it doesn't adjust for the oversampling setting, which would explain why that line of API code won't work as written.


- Don
dleroi is offline Find More Posts by dleroi
Reply With Quote
Old Feb 21, 2010, 09:08 PM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Quote:
Originally Posted by dleroi View Post
Rusty,

How does your code derive "up"? I'm guessing it doesn't adjust for the oversampling setting, which would explain why that line of API code won't work as written.
Hi Don,

I'm not quite sure what you're asking. I think the example code Bosch supplied must be wrong. Take a look at their example in the datasheet, and you'll notice that it also reads about 70000 Pascals, which is about 10k ft high. They might be on a mountain, but I doubt it

Here's the place I found the code, primarily the link near the bottom of the page. This guy is looking into it as well.

http://interactive-matter.org/2009/1...sensor-bmp085/

The big problem now is trying to use the measurement with enough averages to make it stable. It would probably help if I knew what I was doing

Rusty
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Reply With Quote
Old Feb 21, 2010, 10:19 PM
Don L.
Old Lyme, Connecticut
Joined Feb 2006
939 Posts
Quote:
Originally Posted by 13brv3 View Post
Hi Don,

I'm not quite sure what you're asking. I think the example code Bosch supplied must be wrong. Take a look at their example in the datasheet, and you'll notice that it also reads about 70000 Pascals, which is about 10k ft high. They might be on a mountain, but I doubt it

Here's the place I found the code, primarily the link near the bottom of the page. This guy is looking into it as well.

http://interactive-matter.org/2009/1...sensor-bmp085/

The big problem now is trying to use the measurement with enough averages to make it stable. It would probably help if I knew what I was doing

Rusty
Hi Rusty,

I agree that the example in the data sheet is wrong and I believe that your solution delivers the right answer. I also think that the code in the API, which is different than the code in the data sheet, works when the uncorrected pressure (UP) is calculated the same way as in the API.

I was asking for the line of code, from your program, that creates the value of UP to see if it is the same as the API.

When I try to extract the Sketch, I get a message that it is corrupted.

Anyway, it isn't important. I was just curious.

- Don
dleroi is offline Find More Posts by dleroi
Reply With Quote
Old Jul 18, 2010, 03:30 PM
Registered User
mifau's Avatar
Joined Jan 2004
50 Posts
Didn't you guys have any problems with i2c voltage from arduino?
BMP085 accepts only 3,3V i2c levels...
mifau is offline Find More Posts by mifau
Reply With Quote
Old Jul 19, 2010, 02:16 AM
Registered User
United Kingdom
Joined Nov 2008
2,024 Posts
Quote:
Originally Posted by mifau View Post
Didn't you guys have any problems with i2c voltage from arduino?
BMP085 accepts only 3,3V i2c levels...
Would imagine they just ran it @ 3.3v (not using the normal jack).

Somewhat after the event, I note that the defintion of ac1 (an int) in the calculation probably causes the subsequent calculation of b3 to overflow hence the insertion of the cast (int32_t) forcing the correct (or closer) calc.

Also, b3 = ((ac1 * 4 + x3) << oss + 2) >> 2 is the same as b3 = ((ac1 * 4 + x3) << oss + 2) /4
each time you right-shift a number (the >> bit), you divide it by 2 (think binary )

the +2 bit in the b3 calculation is probably a 'half-adjust' since this is then divided by 4.

Just a few educated guesses....

HTH
Mike Barton is offline Find More Posts by Mike Barton
Reply With Quote
Old Aug 26, 2010, 03:05 AM
Registered User
Joined Aug 2010
1 Posts
Hello,

I am Dominik Geisler, application engineer for Bosch Sensortec (region Europe). We saw you have an issue with our sensor here, so I would like to help.

The issue you saw as indeed caused by a missing explicit cast into int32. However, in the official API, this cast has been made in every released version (the line reads ((long)p_bmp085->cal_param.ac1)...). I image someone removed it as for 32-bit machines, it is not needed on those architectures. This code then won't work on non-32-bit architectures. The code discussed here is not the official released code from Bosch Sensortec.

We are planning to release the official API code on our web site in about one week. In the meantime, you can use the "contact" function on our web site to obtain the latest official API version.

@Mike Barton: the +2 is indeed for correct rounding. The difference will however not be visible most of the time.

Best Regards,

Dominik Geisler

Bosch Sensortec GmbH
Application Engineer (BST/ENG3)
Gerhard-Kindler-Straße 8
72770 Reutlingen
GERMANY
www.bosch-sensortec.com
Dominik_Geisler is offline Find More Posts by Dominik_Geisler
Reply With Quote
Old Aug 26, 2010, 04:00 AM
Registered User
Hoofddorp, Netherlands
Joined Sep 2004
3,917 Posts
Thanks for that info Donminic. I-ve also posted in the AeroQuad forum (http://aeroquad.com/showthread.php?7...=9129#post9129) which should get the info to both the AeroQuad and ArduCopter (DIY Drones) communicties using this sensor in their various autopilots / flight controlers.
Arthur P. is offline Find More Posts by Arthur P.
Reply With Quote
Old Aug 26, 2010, 07:12 AM
13brv3's Avatar
Navarre, FL
Joined Mar 2002
3,839 Posts
Thanks for the post Dominik. The BMP085 seems to be a leading choice in sensors for us, so it will be helpful to have good, official data

Cheers,
Rusty
13brv3 is offline Find More Posts by 13brv3
Site Sponsor
Reply With Quote
Old Mar 19, 2011, 09:42 AM
Registered User
Joined Mar 2011
20 Posts
Hallo, I have this board:
http://cdn.shopify.com/s/files/1/006...b_grande.jpg?7

and this sensor:
http://www.watterott.com/de/Breakout...en-Drucksensor

do I have to use some resistors like here:
http://interactive-matter.eu/2009/12...sensor-bmp085/
Between this 3 connectors VCC SCL SDA?

http://cdn.shopify.com/s/files/1/006...b_grande.jpg?7

And do I only have to connect GND to GND
VCC to VCC
SCL to SCL
SDA to SDA

And the other to pins don´t have to be connected to any other pin?

thx
druckgott
druckgott is offline Find More Posts by druckgott
Reply With Quote
Reply


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Question Pressure sensor water seal mhfalken Submarines 12 Oct 09, 2009 01:39 PM
New Product tiny Pressure Sensor for altimeter applications muc UAV - Unmanned Aerial Vehicles 1 Oct 25, 2006 12:03 AM
Idea Pressure sensor for EDF ducting... meteor Electric Ducted Fan Jet Talk 0 Apr 18, 2006 02:55 PM
Looking for inexpensive differential pressure sensor to use for airspeed? deh6 DIY Electronics 20 Sep 06, 2005 10:38 AM