I am trying to get to BMP085 connected to one Arduino Uno. Using the solution I got here, I was able to make two sensors work together (the solution is towards the bottom). The solution is messy and hard to read and my code is going to be looked at by people who don't normally code so I wanted to create a library with the functions. My library works perfect when only one BMO085 sensor is connected but when I add two, I end up with the serial monitor just printing a bunch of zeros, either right away or after a bit. I am only able to replicate this problem in the solution when I disconnect one of the BMP085 from the I2C bus. Could my wires or sensors be bad? Is there something I am missing in my library?
Library MultiBMP085.h
#ifndef MultiBMP085_h
#define MultiBMP085_h
#include "Arduino.h"
class MultiBMP085 {
public:
MultiBMP085(int assignPin);
boolean BMP085_begin();
float getTemperature();
short getPressure();
private:
int _pin;
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
long b5;
const unsigned char OSS = 0;
void Calibrate();
unsigned int ReadUT();
int ReadInt(unsigned char address);
unsigned long ReadUP();
} ;
#endif
Library MultiBMP085.cpp
#include "Arduino.h"
#include "MultiBMP085.h"
#include <Wire.h>
#define BMP085_ADDRESS 0x77 //I2C address of BMP085
MultiBMP085::MultiBMP085(int assignPin) // Used when creating the class variables, assigned what pin the BMP085 XCLR is in
{
pinMode(assignPin, OUTPUT);
_pin = assignPin;
digitalWrite(_pin, LOW);
}
boolean MultiBMP085 :: BMP085_begin(){
digitalWrite(_pin, HIGH);
delay(10);
ac1 = ReadInt(0xAA);
ac2 = ReadInt(0xAC);
ac3 = ReadInt(0xAE);
ac4 = ReadInt(0xB0);
ac5 = ReadInt(0xB2);
ac6 = ReadInt(0xB4);
b1 = ReadInt(0xB6);
b2 = ReadInt(0xB8);
mb = ReadInt(0xBA);
mc = ReadInt(0xBC);
md = ReadInt(0xBE);
digitalWrite(_pin,LOW);
}
float MultiBMP085 :: getTemperature(){
digitalWrite(_pin,HIGH);
delay(5);
unsigned int ut = ReadUT();
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
digitalWrite(_pin,LOW);
delay(5);
float toReturn = ((b5 +8)>>4)/10.0;
return toReturn;
}
short MultiBMP085:: getPressure(){
digitalWrite(_pin,HIGH);
delay(5);
short na = getTemperature(); //Need to make sure b5 is set before getting pressure
unsigned long up = ReadUP();
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS)); //HERE
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
digitalWrite(_pin,LOW);
return p;
}
unsigned int MultiBMP085 :: ReadUT(){
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = ReadInt(0xF6);
return ut;
}
int MultiBMP085 :: ReadInt(unsigned char address){
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2){
Serial.print(Wire.available());
delay(100); }
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
unsigned long MultiBMP085 :: ReadUP(){
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
// Wait for data to become available
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);
return up;
}
Test for Library (prints a bunch of zeros when I dd the commented out parts) :
#include "MultiBMP085.h"
MultiBMP085 bmp1(13);
//MultiBMP085 bmp2(12);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("test");
boolean test = bmp1.BMP085_begin();
//test = bmp2.BMP085_begin();
Serial.println("bmp1 setup");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("BMP1: ");
float temp = bmp1.getTemperature();
Serial.print(temp, 2);
Serial.println(" *C");
/*Serial.print("BMP2: ");
temp = bmp2.getTemperature();
Serial.print(temp, 2);
Serial.println(" *C");
*/delay(1000);
}
Example of Serial Monitor when 1 BMP sensor:
test
bmp1 setup
BMP1: 23.90 *C
BMP1: 23.90 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
BMP1: 23.80 *C
With two sensors:
test
bmp1 setup
BMP1: 23.80 *C
BMP2: 23.90 *C
BMP1: 23.80 *C
BMP2: 23.90 *C
BMP1: 23.80 *C
BMP2: 24.00 *C
BMP1: 23.80 *C
BMP2: 23.90 *C
BMP1: 23.80 *C
BMP2: 23.90 *C
BMP1: 23.80 *C
BMP2: 61.90 *C
BMP1: 23.80 *C
BMP2: 24.00 *C
BMP1: 23.80 *C
BMP2: 24.00 *C
BMP1: 23.80 *C
BMP2: 0000000000000000000000000000000000000000[/code]