Skip to content

Commit 54ec2bf

Browse files
Add BMP180 sensor example
1 parent 8d7fb22 commit 54ec2bf

File tree

3 files changed

+406
-0
lines changed

3 files changed

+406
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Heltec Automation BMP180 Sensors test example
2+
*
3+
* Function:
4+
* Temperature and Pressure measurement
5+
*
6+
* HelTec AutoMation, Chengdu, China
7+
* www.heltec.org
8+
*
9+
* this project also realess in GitHub:
10+
* https://github.com/HelTecAutomation/ASR650x-Arduino
11+
*
12+
*/
13+
#include "Arduino.h"
14+
#include "heltec.h"
15+
#include <Wire.h>
16+
#include <BMP180.h>
17+
#include "string.h"
18+
19+
BMP085 bmp;
20+
21+
uint8_t T[20] = {"Temperature"};
22+
23+
void setup() {
24+
pinMode(Vext,OUTPUT);
25+
digitalWrite(Vext,LOW);
26+
Serial.begin(115200);
27+
28+
}
29+
void drawFontFaceDemo(double T,double P,double A,double R) {
30+
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, true /*Serial Enable*/);
31+
// Heltec.display->flipScreenVertically();
32+
Heltec.display->clear();
33+
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
34+
Heltec.display->setFont(ArialMT_Plain_10);
35+
Heltec.display->drawString(0 , 0 , "Temperature =");
36+
Heltec.display->drawString(76 , 0 , (String)T);
37+
Heltec.display->drawString(106 , 0 , " *C");
38+
Heltec.display->drawString(0 , 16 , "Pressure =");
39+
Heltec.display->drawString(56 , 16 , (String)P);
40+
Heltec.display->drawString(100 , 16 , "Pa");
41+
Heltec.display->drawString(0 , 32 , "Altitude =");
42+
Heltec.display->drawString(56 , 32 , (String)A);
43+
Heltec.display->drawString(86 , 32 , " m");
44+
Heltec.display->drawString(0 , 48 , "Real altitude =");
45+
Heltec.display->drawString(76 , 48 , (String)R);
46+
Heltec.display->drawString(106 , 48 , " m");
47+
Heltec.display->display();
48+
}
49+
void loop() {
50+
if (!bmp.begin()) {
51+
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
52+
while (1) {}
53+
}
54+
double T =bmp.readTemperature();
55+
Serial.print("Temperature = ");
56+
Serial.print(T);
57+
Serial.println(" *C");
58+
59+
double P = bmp.readPressure();
60+
Serial.print("Pressure = ");
61+
Serial.print(bmp.readPressure());
62+
Serial.println(" Pa");
63+
64+
double A = bmp.readAltitude();
65+
// Calculate altitude assuming 'standard' barometric
66+
// pressure of 1013.25 millibar = 101325 Pascal
67+
Serial.print("Altitude = ");
68+
Serial.print(bmp.readAltitude());
69+
Serial.println(" meters");
70+
71+
72+
Serial.print("Pressure at sealevel (calculated) = ");
73+
Serial.print(bmp.readSealevelPressure());
74+
Serial.println(" Pa");
75+
76+
double R = bmp.readAltitude(101500);
77+
// you can get a more precise measurement of altitude
78+
// if you know the current sea level pressure which will
79+
// vary with weather and such. If it is 1015 millibars
80+
// that is equal to 101500 Pascals.
81+
Serial.print("Real altitude = ");
82+
Serial.print(bmp.readAltitude(101500));
83+
Serial.println(" meters");
84+
85+
Serial.println();
86+
delay(500);
87+
drawFontFaceDemo(T, P, A, R) ;
88+
delay(5000);
89+
}

src/BMP180.cpp

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#include "BMP180.h"
2+
3+
BMP085::BMP085() {
4+
}
5+
6+
7+
boolean BMP085::begin(uint8_t mode) {
8+
if (mode > BMP085_ULTRAHIGHRES)
9+
mode = BMP085_ULTRAHIGHRES;
10+
oversampling = mode;
11+
//begin(int sdaPin, int sclPin, uint32_t frequency)
12+
Wire.begin(13, 12, 100000);
13+
14+
if (read8(0xD0) != 0x55) return false;
15+
16+
/* read calibration data */
17+
ac1 = read16(BMP085_CAL_AC1);
18+
ac2 = read16(BMP085_CAL_AC2);
19+
ac3 = read16(BMP085_CAL_AC3);
20+
ac4 = read16(BMP085_CAL_AC4);
21+
ac5 = read16(BMP085_CAL_AC5);
22+
ac6 = read16(BMP085_CAL_AC6);
23+
24+
b1 = read16(BMP085_CAL_B1);
25+
b2 = read16(BMP085_CAL_B2);
26+
27+
mb = read16(BMP085_CAL_MB);
28+
mc = read16(BMP085_CAL_MC);
29+
md = read16(BMP085_CAL_MD);
30+
#if (BMP085_DEBUG == 1)
31+
Serial.print("ac1 = "); Serial.println(ac1, DEC);
32+
Serial.print("ac2 = "); Serial.println(ac2, DEC);
33+
Serial.print("ac3 = "); Serial.println(ac3, DEC);
34+
Serial.print("ac4 = "); Serial.println(ac4, DEC);
35+
Serial.print("ac5 = "); Serial.println(ac5, DEC);
36+
Serial.print("ac6 = "); Serial.println(ac6, DEC);
37+
38+
Serial.print("b1 = "); Serial.println(b1, DEC);
39+
Serial.print("b2 = "); Serial.println(b2, DEC);
40+
41+
Serial.print("mb = "); Serial.println(mb, DEC);
42+
Serial.print("mc = "); Serial.println(mc, DEC);
43+
Serial.print("md = "); Serial.println(md, DEC);
44+
#endif
45+
46+
return true;
47+
}
48+
49+
int32_t BMP085::computeB5(int32_t UT) {
50+
int32_t X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15;
51+
int32_t X2 = ((int32_t)mc << 11) / (X1+(int32_t)md);
52+
return X1 + X2;
53+
}
54+
55+
uint16_t BMP085::readRawTemperature(void) {
56+
write8(BMP085_CONTROL, BMP085_READTEMPCMD);
57+
delay(5);
58+
#if BMP085_DEBUG == 1
59+
Serial.print("Raw temp: "); Serial.println(read16(BMP085_TEMPDATA));
60+
#endif
61+
return read16(BMP085_TEMPDATA);
62+
}
63+
64+
uint32_t BMP085::readRawPressure(void) {
65+
uint32_t raw;
66+
67+
write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
68+
69+
if (oversampling == BMP085_ULTRALOWPOWER)
70+
delay(5);
71+
else if (oversampling == BMP085_STANDARD)
72+
delay(8);
73+
else if (oversampling == BMP085_HIGHRES)
74+
delay(14);
75+
else
76+
delay(26);
77+
78+
raw = read16(BMP085_PRESSUREDATA);
79+
80+
raw <<= 8;
81+
raw |= read8(BMP085_PRESSUREDATA+2);
82+
raw >>= (8 - oversampling);
83+
84+
/* this pull broke stuff, look at it later?
85+
if (oversampling==0) {
86+
raw <<= 8;
87+
raw |= read8(BMP085_PRESSUREDATA+2);
88+
raw >>= (8 - oversampling);
89+
}
90+
*/
91+
92+
#if BMP085_DEBUG == 1
93+
Serial.print("Raw pressure: "); Serial.println(raw);
94+
#endif
95+
return raw;
96+
}
97+
98+
99+
int32_t BMP085::readPressure(void) {
100+
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
101+
uint32_t B4, B7;
102+
103+
UT = readRawTemperature();
104+
UP = readRawPressure();
105+
106+
#if BMP085_DEBUG == 1
107+
// use datasheet numbers!
108+
UT = 27898;
109+
UP = 23843;
110+
ac6 = 23153;
111+
ac5 = 32757;
112+
mc = -8711;
113+
md = 2868;
114+
b1 = 6190;
115+
b2 = 4;
116+
ac3 = -14383;
117+
ac2 = -72;
118+
ac1 = 408;
119+
ac4 = 32741;
120+
oversampling = 0;
121+
#endif
122+
123+
B5 = computeB5(UT);
124+
125+
#if BMP085_DEBUG == 1
126+
Serial.print("X1 = "); Serial.println(X1);
127+
Serial.print("X2 = "); Serial.println(X2);
128+
Serial.print("B5 = "); Serial.println(B5);
129+
#endif
130+
131+
// do pressure calcs
132+
B6 = B5 - 4000;
133+
X1 = ((int32_t)b2 * ( (B6 * B6)>>12 )) >> 11;
134+
X2 = ((int32_t)ac2 * B6) >> 11;
135+
X3 = X1 + X2;
136+
B3 = ((((int32_t)ac1*4 + X3) << oversampling) + 2) / 4;
137+
138+
#if BMP085_DEBUG == 1
139+
Serial.print("B6 = "); Serial.println(B6);
140+
Serial.print("X1 = "); Serial.println(X1);
141+
Serial.print("X2 = "); Serial.println(X2);
142+
Serial.print("B3 = "); Serial.println(B3);
143+
#endif
144+
145+
X1 = ((int32_t)ac3 * B6) >> 13;
146+
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
147+
X3 = ((X1 + X2) + 2) >> 2;
148+
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
149+
B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );
150+
151+
#if BMP085_DEBUG == 1
152+
Serial.print("X1 = "); Serial.println(X1);
153+
Serial.print("X2 = "); Serial.println(X2);
154+
Serial.print("B4 = "); Serial.println(B4);
155+
Serial.print("B7 = "); Serial.println(B7);
156+
#endif
157+
158+
if (B7 < 0x80000000) {
159+
p = (B7 * 2) / B4;
160+
} else {
161+
p = (B7 / B4) * 2;
162+
}
163+
X1 = (p >> 8) * (p >> 8);
164+
X1 = (X1 * 3038) >> 16;
165+
X2 = (-7357 * p) >> 16;
166+
167+
#if BMP085_DEBUG == 1
168+
Serial.print("p = "); Serial.println(p);
169+
Serial.print("X1 = "); Serial.println(X1);
170+
Serial.print("X2 = "); Serial.println(X2);
171+
#endif
172+
173+
p = p + ((X1 + X2 + (int32_t)3791)>>4);
174+
#if BMP085_DEBUG == 1
175+
Serial.print("p = "); Serial.println(p);
176+
#endif
177+
return p;
178+
}
179+
180+
int32_t BMP085::readSealevelPressure(float altitude_meters) {
181+
float pressure = readPressure();
182+
return (int32_t)(pressure / pow(1.0-altitude_meters/44330, 5.255));
183+
}
184+
185+
float BMP085::readTemperature(void) {
186+
int32_t UT, B5; // following ds convention
187+
float temp;
188+
189+
UT = readRawTemperature();
190+
191+
#if BMP085_DEBUG == 1
192+
// use datasheet numbers!
193+
UT = 27898;
194+
ac6 = 23153;
195+
ac5 = 32757;
196+
mc = -8711;
197+
md = 2868;
198+
#endif
199+
200+
B5 = computeB5(UT);
201+
temp = (B5+8) >> 4;
202+
temp /= 10;
203+
204+
return temp;
205+
}
206+
207+
float BMP085::readAltitude(float sealevelPressure) {
208+
float altitude;
209+
210+
float pressure = readPressure();
211+
212+
altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
213+
214+
return altitude;
215+
}
216+
217+
218+
/*********************************************************************/
219+
220+
uint8_t BMP085::read8(uint8_t a) {
221+
uint8_t ret;
222+
223+
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
224+
225+
Wire.write(a); // sends register address to read from
226+
227+
Wire.endTransmission(); // end transmission
228+
229+
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
230+
Wire.requestFrom(BMP085_I2CADDR, 1);// send data n-bytes read
231+
ret = Wire.read(); // receive DATA
232+
Wire.endTransmission(); // end transmission
233+
234+
return ret;
235+
}
236+
237+
uint16_t BMP085::read16(uint8_t a) {
238+
uint16_t ret;
239+
240+
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
241+
Wire.write(a); // sends register address to read from
242+
Wire.endTransmission(); // end transmission
243+
244+
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
245+
Wire.requestFrom(BMP085_I2CADDR, 2);// send data n-bytes read
246+
ret = Wire.read(); // receive DATA
247+
ret <<= 8;
248+
ret |= Wire.read(); // receive DATA
249+
Wire.endTransmission(); // end transmission
250+
251+
return ret;
252+
}
253+
254+
void BMP085::write8(uint8_t a, uint8_t d) {
255+
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
256+
Wire.write(a); // sends register address to read from
257+
Wire.write(d); // write data
258+
Wire.endTransmission(); // end transmission
259+
}

0 commit comments

Comments
 (0)