Last active
February 24, 2016 21:31
-
-
Save andreuinyu/ade421e9bc76b3a1d2ff to your computer and use it in GitHub Desktop.
Arduino code to use with the BMP180 pressure and temperature sensor. SCL -> A5. SDA -> A4. Code worked out from the official datasheet: http://www.mouser.com/ds/2/783/BST-BMP180-DS000-12~1-786481.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#define BMP180_I2C_Adress 0x77 //Usually 0x77 | |
int p0 = 1013.25; //Pressure at sea level; used to calculate altitude | |
int i = 2; //Pressure precision setting: 0, 1, 2 or 3 (3 is broken right now. will fix.) | |
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; | |
float T; | |
float P; | |
void setup(){ | |
Wire.begin(); | |
BMP180Calibration(); | |
} | |
void loop(){ | |
//your code here | |
} | |
int BMP180ReadInt(unsigned char adress){ | |
unsigned char MSB, LSB; | |
Wire.beginTransmission(BMP180_I2C_Adress); | |
Wire.write(adress); | |
Wire.endTransmission(); | |
Wire.requestFrom(BMP180_I2C_Adress, 2); | |
while (Wire.available() < 2){ | |
Serial.print(Wire.available()); | |
delay(100); | |
} | |
MSB = Wire.read(); | |
LSB = Wire.read(); | |
return (int) MSB<<8 | LSB; | |
} | |
void BMP180Calibration(){ | |
AC1 = BMP180ReadInt(0xAA); | |
AC2 = BMP180ReadInt(0xAC); | |
AC3 = BMP180ReadInt(0xAE); | |
AC4 = BMP180ReadInt(0xB0); | |
AC5 = BMP180ReadInt(0xB2); | |
AC6 = BMP180ReadInt(0xB4); | |
b1 = BMP180ReadInt(0xB6); | |
B2 = BMP180ReadInt(0xB8); | |
MB = BMP180ReadInt(0xBA); | |
MC = BMP180ReadInt(0xBC); | |
MD = BMP180ReadInt(0xBE); | |
} | |
float BMP180Temperature(){ | |
long X1, X2, UT; | |
Wire.beginTransmission(BMP180_I2C_Adress); | |
Wire.write(0xF4); | |
Wire.write(0x2E); | |
Wire.endTransmission(); | |
delayMicroseconds(4500); | |
UT = BMP180ReadInt(0xF6); | |
X1 = (((long)UT - (long)AC6)*(long)AC5) >> 15; | |
X2 = ((long)MC << 11)/(X1 + MD); | |
B5 = X1 + X2; | |
T = ((B5 + 8)>>4); | |
T *= 0.1; | |
return T; | |
} | |
float BMP180Pressure(unsigned int precision){ | |
long X1, X2, X3, B3, B6, p; | |
unsigned long B4, B7, UP; | |
unsigned char MSB, LSB, XLSB; | |
Wire.beginTransmission(BMP180_I2C_Adress); | |
Wire.write(0xF4); | |
Wire.write(0x34 + (precision<<6)); | |
Wire.endTransmission(); | |
if (precision == 0){ | |
delayMicroseconds(4500); | |
}else if (precision == 1){ | |
delayMicroseconds(7500); | |
}else if (precision == 2){ | |
delayMicroseconds(13500); | |
}else if (precision == 3){ | |
delayMicroseconds(30000); | |
}else{ | |
return 0; | |
} | |
Wire.beginTransmission(BMP180_I2C_Adress); | |
Wire.write(0xF6); | |
Wire.endTransmission(); | |
Wire.requestFrom(BMP180_I2C_Adress, 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-precision); | |
B6 = B5 - 4000; | |
X1 = (B2 * (B6 * B6)>>12)>>11; | |
X2 = (AC2 * B6)>>11; | |
X3 = X1 + X2; | |
B3 = (((((long)AC1)*4 + X3)<<precision) + 2)>>2; | |
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>>precision)); | |
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; | |
return 0.01*p; | |
} | |
int BMP180Altitude(float p0){ | |
return 44330*(1-pow(BMP180Pressure(i)/p0, 1/5.255)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment