{"id":78,"date":"2010-05-22T20:00:03","date_gmt":"2010-05-22T20:00:03","guid":{"rendered":"http:\/\/mitat.tuu.fi\/?p=78"},"modified":"2010-08-10T19:12:12","modified_gmt":"2010-08-10T19:12:12","slug":"bmp085-with-arduino","status":"publish","type":"post","link":"http:\/\/mitat.tuu.fi\/?p=78","title":{"rendered":"BMP085 with Arduino (tested &#038; working)"},"content":{"rendered":"<p>I tested &#038; corrected and this code really works.<\/p>\n<p><code><br \/>\n\/\/ BMP08 with Arduino<\/p>\n<p>\/\/ DANGER: The BMP08 accepts 1.8 to 3.6 Volts \u2013 so no chance to connect it directly to 5 Volts.<\/p>\n<p>\/\/ Connect VCC to VCC and GND to GND, SCL goes to analogue pin 5, SDA to analogue pin4.<br \/>\n\/\/ Notice! Sparkfun breakoutboard contains already 4.7K pull ups,<br \/>\n\/\/ If not using pre-built pull-ups:<br \/>\n\/\/ --> Add some pull up resistors (1K to 20K, most often something like 4.7K) between SDA, SCL and VCC finishes the setup.<\/p>\n<p>\/\/ References: http:\/\/interactive-matter.org\/2009\/12\/arduino-barometric-pressure-sensor-bmp085\/ and http:\/\/news.jeelabs.org\/2009\/02\/19\/hooking-up-a-bmp085-sensor\/<br \/>\n\/\/ Specification: http:\/\/www.bosch-sensortec.com\/content\/language1\/downloads\/BST-BMP085-DS000-05.pdf<br \/>\n\/\/ SparkFun breakout board: http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=9694<\/p>\n<p>#include \"Wire.h\"<\/p>\n<p>#define I2C_ADDRESS 0x77 <\/p>\n<p>const unsigned char oversampling_setting = 3; \/\/oversamplig for measurement<br \/>\nconst unsigned char pressure_waittime[4] = { 5, 8, 14, 26 };<\/p>\n<p>\/\/just taken from the BMP085 datasheet<br \/>\nint ac1;<br \/>\nint ac2;<br \/>\nint ac3;<br \/>\nunsigned int ac4;<br \/>\nunsigned int ac5;<br \/>\nunsigned int ac6;<br \/>\nint b1;<br \/>\nint b2;<br \/>\nint mb;<br \/>\nint mc;<br \/>\nint md;<\/p>\n<p>void setup()<br \/>\n{<br \/>\n  Serial.begin(9600);  \/\/ start serial for output<br \/>\n  Serial.println(\"Setting up BMP085\");<br \/>\n  Wire.begin();<br \/>\n  bmp085_get_cal_data();<br \/>\n}<br \/>\nvoid bmp085_read_temperature_and_pressure(int& temperature, long& pressure);<br \/>\nvoid loop()<br \/>\n{<br \/>\n  int  temperature = 0;<br \/>\n  long pressure = 0;<\/p>\n<p>  bmp085_read_temperature_and_pressure(&temperature,&pressure);<br \/>\n  Serial.print(temperature,DEC);<br \/>\n  Serial.print(\" \");<br \/>\n  Serial.print(pressure,DEC);<br \/>\n  Serial.println();<br \/>\n  delay(100);<br \/>\n}<\/p>\n<p>void bmp085_read_temperature_and_pressure(int* temperature, long* pressure) {<br \/>\n  int  ut= bmp085_read_ut();<br \/>\n  long up = bmp085_read_up();<br \/>\n   long x1, x2, x3, b3, b5, b6, p;<br \/>\n   unsigned long b4, b7;<\/p>\n<p>   \/\/calculate the temperature<br \/>\n   x1 = ((long)ut - ac6) * ac5 >> 15;<br \/>\n   x2 = ((long) mc << 11) \/ (x1 + md);\n   b5 = x1 + x2;\n   *temperature = (b5 + 8) >> 4;<\/p>\n<p>   \/\/calculate the pressure<br \/>\n   b6 = b5 - 4000;<br \/>\n   x1 = (b2 * (b6 * b6 >> 12)) >> 11;<br \/>\n   x2 = ac2 * b6 >> 11;<br \/>\n   x3 = x1 + x2;<\/p>\n<p>   \/\/b3 = (((int32_t) ac1 * 4 + x3)<<oversampling_setting + 2) >> 2;<\/p>\n<p>   if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) << 1;\n   if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2); \n   if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 1;<br \/>\n   if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;<\/p>\n<p>   x1 = ac3 * b6 >> 13;<br \/>\n   x2 = (b1 * (b6 * b6 >> 12)) >> 16;<br \/>\n   x3 = ((x1 + x2) + 2) >> 2;<br \/>\n   b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;<br \/>\n   b7 = ((uint32_t) up - b3) * (50000 >> oversampling_setting);<br \/>\n   p = b7 < 0x80000000 ? (b7 * 2) \/ b4 : (b7 \/ b4) * 2;\n   \n   x1 = (p >> 8) * (p >> 8);<br \/>\n   x1 = (x1 * 3038) >> 16;<br \/>\n   x2 = (-7357 * p) >> 16;<br \/>\n   *pressure = p + ((x1 + x2 + 3791) >> 4);<\/p>\n<p>}<\/p>\n<p>unsigned int bmp085_read_ut() {<br \/>\n  write_register(0xf4,0x2e);<br \/>\n  delay(5); \/\/longer than 4.5 ms<br \/>\n  return read_int_register(0xf6);<br \/>\n}<\/p>\n<p>void  bmp085_get_cal_data() {<br \/>\n  Serial.println(\"Reading Calibration Data\");<br \/>\n  ac1 = read_int_register(0xAA);<br \/>\n  Serial.print(\"AC1: \");<br \/>\n  Serial.println(ac1,DEC);<br \/>\n  ac2 = read_int_register(0xAC);<br \/>\n  Serial.print(\"AC2: \");<br \/>\n  Serial.println(ac2,DEC);<br \/>\n  ac3 = read_int_register(0xAE);<br \/>\n  Serial.print(\"AC3: \");<br \/>\n  Serial.println(ac3,DEC);<br \/>\n  ac4 = read_int_register(0xB0);<br \/>\n  Serial.print(\"AC4: \");<br \/>\n  Serial.println(ac4,DEC);<br \/>\n  ac5 = read_int_register(0xB2);<br \/>\n  Serial.print(\"AC5: \");<br \/>\n  Serial.println(ac5,DEC);<br \/>\n  ac6 = read_int_register(0xB4);<br \/>\n  Serial.print(\"AC6: \");<br \/>\n  Serial.println(ac6,DEC);<br \/>\n  b1 = read_int_register(0xB6);<br \/>\n  Serial.print(\"B1: \");<br \/>\n  Serial.println(b1,DEC);<br \/>\n  b2 = read_int_register(0xB8);<br \/>\n  Serial.print(\"B2: \");<br \/>\n  Serial.println(b1,DEC);<br \/>\n  mb = read_int_register(0xBA);<br \/>\n  Serial.print(\"MB: \");<br \/>\n  Serial.println(mb,DEC);<br \/>\n  mc = read_int_register(0xBC);<br \/>\n  Serial.print(\"MC: \");<br \/>\n  Serial.println(mc,DEC);<br \/>\n  md = read_int_register(0xBE);<br \/>\n  Serial.print(\"MD: \");<br \/>\n  Serial.println(md,DEC);<br \/>\n}<\/p>\n<p>long bmp085_read_up() {<br \/>\n  write_register(0xf4,0x34+(oversampling_setting<<6));\n  delay(pressure_waittime[oversampling_setting]);\n  \n  unsigned char msb, lsb, xlsb;\n  Wire.beginTransmission(I2C_ADDRESS);\n  Wire.send(0xf6);  \/\/ register to read\n  Wire.endTransmission();\n\n  Wire.requestFrom(I2C_ADDRESS, 3); \/\/ read a byte\n  while(!Wire.available()) {\n    \/\/ waiting\n  }\n  msb = Wire.receive();\n  while(!Wire.available()) {\n    \/\/ waiting\n  }\n  lsb |= Wire.receive();\n  while(!Wire.available()) {\n    \/\/ waiting\n  }\n  xlsb |= Wire.receive();\n  return (((long)msb<<16) | ((long)lsb<<8) | ((long)xlsb)) >>(8-oversampling_setting);<br \/>\n}<\/p>\n<p>void write_register(unsigned char r, unsigned char v)<br \/>\n{<br \/>\n  Wire.beginTransmission(I2C_ADDRESS);<br \/>\n  Wire.send(r);<br \/>\n  Wire.send(v);<br \/>\n  Wire.endTransmission();<br \/>\n}<\/p>\n<p>char read_register(unsigned char r)<br \/>\n{<br \/>\n  unsigned char v;<br \/>\n  Wire.beginTransmission(I2C_ADDRESS);<br \/>\n  Wire.send(r);  \/\/ register to read<br \/>\n  Wire.endTransmission();<\/p>\n<p>  Wire.requestFrom(I2C_ADDRESS, 1); \/\/ read a byte<br \/>\n  while(!Wire.available()) {<br \/>\n    \/\/ waiting<br \/>\n  }<br \/>\n  v = Wire.receive();<br \/>\n  return v;<br \/>\n}<\/p>\n<p>int read_int_register(unsigned char r)<br \/>\n{<br \/>\n  unsigned char msb, lsb;<br \/>\n  Wire.beginTransmission(I2C_ADDRESS);<br \/>\n  Wire.send(r);  \/\/ register to read<br \/>\n  Wire.endTransmission();<\/p>\n<p>  Wire.requestFrom(I2C_ADDRESS, 2); \/\/ read a byte<br \/>\n  while(!Wire.available()) {<br \/>\n    \/\/ waiting<br \/>\n  }<br \/>\n  msb = Wire.receive();<br \/>\n  while(!Wire.available()) {<br \/>\n    \/\/ waiting<br \/>\n  }<br \/>\n  lsb = Wire.receive();<br \/>\n  return (((int)msb<<8) | ((int)lsb));\n}\n<\/code><\/p>\n<p>Links: <a href=\"http:\/\/sensorapp.net\/?p=278\">http:\/\/sensorapp.net\/?p=278<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I tested &#038; corrected and this code really works. \/\/ BMP08 with Arduino \/\/ DANGER: The BMP08 accepts 1.8 to 3.6 Volts \u2013 so no chance to connect it directly to 5 Volts. \/\/ Connect VCC to VCC and GND to GND, SCL goes to analogue pin 5, SDA to analogue pin4. \/\/ Notice! Sparkfun [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,7],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-code","category-electronics"],"_links":{"self":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":7,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":107,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/107"}],"wp:attachment":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}