[solved] BMP085 : Gentoo : serial.print failed

hello,

i’m getting trouble using a temperature/pressure sensor (bmp085) wich seems to be a programming problem.

Examples and libs i took from seeeduino (which is the bmp085 breakout i own) http://www.seeedstudio.com/wiki/Grove_-_Barometer_Sensor, or from adafruit http://learn.adafruit.com/bmp085/using-the-bmp085, or from sparkfun https://www.sparkfun.com/tutorial/Barometric/BMP085_Example_Code.pde, did not work for me.

so i tried to debug by a step-by-step method.

here is my code :

#include <Wire.h>

#define BMP 0x77
const unsigned char OSS = 0;

long ut;
unsigned long up;
unsigned char msb,lsb,xlsb;

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
// get ucompassed temperature : write 0x2e in 0xf4
  Wire.beginTransmission(BMP);
  Wire.write(byte(0xF4));
  Wire.write(byte(0x2E));
  Wire.endTransmission();
  delay(5);

  // read register 0xf6 (msb), 0xF7 (lsb) 
  Wire.beginTransmission(BMP);
  Wire.write(byte(0xF6));
  Wire.endTransmission();
  Wire.requestFrom(BMP,2);
  while(Wire.available()<2);
  msb = Wire.read();
  lsb = Wire.read();
  
  ut = (int) msb<<8 | lsb;
// end get uncompassed temperature
 
// get ucompassed pressure : write 0x34 + oversampling in 0xf4
  Wire.beginTransmission(BMP);
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
  delay(2 + (3<<OSS));

  // read register 0xf6 (msb), 0xf7 (lsb), and 0xf8 (xlsb)
  Wire.beginTransmission(BMP);
  Wire.write(byte(0xF6));
  Wire.endTransmission();
  Wire.requestFrom(BMP,3);
  while(Wire.available()<3);
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
// end get uncompassed pressure

  Serial.print("raw temp : ");
  Serial.println(ut);  
  Serial.print("raw press : ");
  Serial.println(up);  

  delay(1000);
}

unfortunatly, this code prints nothing on my serial monitor and the onboard led (pin13) blink each 1sec like if it would like to say me : “there is something wrong”.

if i comment out the part “get uncompassed temperature” i’am able to get and print the pressure (up);
if i comment out the part “get uncompassed pressure”, i’am able to get and print the temp (ut).

why i cannot get the both ?
i certainly did something wrong but what ?

I've got this sensor ticking away on my project. Give me a while and I'll go get the code I used. Don't give up hope! Its a lovely little sensor!

This is one problem:

while(Wire.available()<2);

It is a common mistake, and I see it in example code a lot. But it is wrong.
The Wire.available() returns the number of byte that is in a buffer (in the Wire library).
The I2C session has already ended. The Wire.requestfrom() handles the session, and places the incoming bytes into a buffer.

You should use Wire.availabe() only to check if there is still something is a buffer that should be taken from the buffer with Wire.read().

Why don’t you use the Adafruit library ?

I just came to look at my code and Erdin is correct in his assessment. My code shows:

while(!Wire.available())

I was able to copy and paste the SparkFun code and it worked out of the gate. I only had to start making changes when I started adding more sensors to the mix; I had to make sure the pins didn't overlap, etc.

hello,

thanks for your replies.
i tried the adafruit library (and the seeeduino one, and the sparkfun code) but it did not work for me,
it’s like this i end up to try little pieces of code to try to see from where it comes.

i tried changing the Wire.available part but without success.

...
  Wire.requestFrom(BMP,2);
  while(!Wire.available());
  msb = Wire.read();
  lsb = Wire.read();
  ut = (int) msb<<8 | lsb;
...

following the Wire.read example from the arduino reference, i also tried printing the register :

...
// get temp
  Wire.requestFrom(BMP,2);
  while(Wire.available()) {
     char c = Wire.read()
     Serial.print(c);
  }
...
// get pressure
  Wire.requestFrom(BMP,3);
  while(Wire.available()) {
     char c = Wire.read()
     Serial.print(c);
  }
...

and the same behaviour happened. they both work alone but not work together.

then i tried to remove the Serial.print(c) inside my Wire.available loop and replace it by Serial.print(".")
and the code work with output like this :

..
...
..
...

wich is fine : 2 bytes for raw temp and 3 for raw pressure.

next step get the value seems to work :

[...]
  while(Wire.available()) {
    msb = Wire.read();
    lsb = Wire.read();
    Serial.print("ut"); 
  }
[...]
  while(Wire.available()) {
    msb = Wire.read();
    lsb = Wire.read();
    xlsb = Wire.read();
    Serial.print("up"); 
  }
[...]

output :

ut
up
ut
up
...

good,
but the code stop to work as soon as i try to print any variable : ut or up or even a one i created for test (int too = 42;).

if i use Serial.write instead of Serial.print to print variable, i get a character and the code run.
not very usefull but at least it’s something

i’m going mad …

hello,

it works !

the problem comes from my compilation, os, toolchain or whatever like this.
i run gentoo amd64.
i tried the code (http://www.seeedstudio.com/wiki/Grove_-_Barometer_Sensor) on a windows and it works.

we’ll try to find the problem …

solved !

the solution is on the forum ...
http://forum.arduino.cc/index.php/topic,93672.msg706517.html#msg706517

i "just" applied the patch for crossdev (by hand because the crossdev seems not to be exactly the same) :
https://378387.bugs.gentoo.org/attachment.cgi?id=285901

then rebuild the tool chain as explain there : Arduino Playground - Gentoo
USE="-openmp" crossdev -t avr -s4 -S --without-headers

works for my arduino duemilanove.

avr-gcc (Gentoo 4.6.3 p1.13, pie-0.5.2) 4.6.3
cross-avr/binutils 2.22-r1
arduino 1.0.4