Hardware | Arduino BMP280 고도/온도/기압 센서

|

1. 시작하기

온도, 기압, 고도센서가 하나의 기판에 달려있는, 참 고마원 BMP280 을 가지고 놀아봅니다.

원래는 Adafruit 에서 나온 정식 발매품이 있는 듯 하나, Aliexpress 를 사랑하는 저로서는 clone 품을 가지고 놀아봅니다.





2. Library

2가지 library 를 설치해야 합니다.

- Adafruit Sensor

- Adafruit BMP280


먼저, Adafruit Sensor library 를 설치합니다.

- https://github.com/adafruit/Adafruit_Sensor




그리고, BMP280 용 library 를 다운로드 받아 설치합니다.

     - https://learn.adafruit.com/adafruit-bmp280-barometric-pressure-plus-temperature-sensor-breakout/wiring-and-test




맨 마지막으로, clone 부품을 구동시키기 위해,

"Adafruit_BMP280.h" 파일을, 아래처럼 I2C 어드레스에 대해 0x77 > 0x76 으로 수정해 줘야 합니다.





3. 소스코드

위의 library 를 추가하였으면, "File > Examples > Adafruit BMP280 Library > bmp280test" 를 선택할 수 있습니다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor
 
  Designed specifically to work with the Adafruit BMEP280 Breakout
 
  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.
 
  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!
 
  Written by Limor Fried & Kevin Townsend for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/
 
#include <wire.h>
#include <spi.h>
#include <adafruit_sensor.h>
#include <adafruit_bmp280.h>
 
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
 
Adafruit_BMP280 bme; // I2C
//Adafruit_BMP280 bme(BMP_CS); // hardware SPI
//Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
 
void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 test"));
   
  if (!bme.begin()) { 
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
}
 
void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bme.readTemperature());
    Serial.println(" *C");
     
    Serial.print(F("Pressure = "));
    Serial.print(bme.readPressure());
    Serial.println(" Pa");
 
    Serial.print(F("Approx altitude = "));
    Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");
     
    Serial.println();
    delay(2000);
}
</adafruit_bmp280.h></adafruit_sensor.h></spi.h></wire.h>



4. Layout

PIN 배열은 다음과 같습니다.

- VCC : 3.3v

- GND : GND

- SCL : A5

- SDA : A4







5. 결과

잘 나오네요.

실재로 사용시에는 기준값을 조금 수정해야 할 것 같습니다만, 일단 성공입니다.




6. 확장

아래 링크에서는 OLED 를 사용한 방법을 소개하고 있습니다.


- http://www.instructables.com/id/Standalone-Arduino-Altimeter/


바로 따라해 봅니다.

우선 아래 용도로 사용될 BMP280 library 를 다운로드 받고, libraries 폴더에 위치시킵니다.


BMP280.zip





[BMP280]

----------------------

- VCC : +3.3V

- GND : GND

- SCL : A5

- SDA : A4

- CSB : +3.3V

- SDO : GND


[0.96" I2C IIC Series 128X64 OLED]

----------------------

- SCL : A5

- SDA : A4

- VCC : +3.3V

- GND : GND


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "U8glib.h"
#include "BMP280.h"
#include "Wire.h"
#define P0 1021.97  //1013.25
BMP280 bmp;
 
// OLED Type
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
 
char sT[20];
char sP[9];
char sA[9];
char sA_MIN[9];
char sA_MAX[9];
double A_MIN = 0;
double A_MAX = 0;
 
void draw(double T, double P, double A) {
  u8g.setFont(u8g_font_unifont);
 
  dtostrf(T, 4, 2, sT);
  dtostrf(P, 4, 2, sP);
  dtostrf(A, 4, 2, sA);
 
  u8g.drawStr( 5, 10, "Temp: ");
  u8g.drawStr( 5, 30, "Bar : ");
  u8g.drawStr( 5, 50, "Alt : ");
  u8g.drawStr( 50, 10, sT);
  u8g.drawStr( 50, 30, sP);
  u8g.drawStr( 50, 50, sA);
}
 
void draw2(double A_MIN, double A_MAX) {
  u8g.setFont(u8g_font_unifont);
 
  dtostrf(A_MIN, 4, 2, sA_MIN);
  dtostrf(A_MAX, 4, 2, sA_MAX);
  u8g.drawStr( 5, 20, "A Min: ");
  u8g.drawStr( 60, 20, sA_MIN);
  u8g.drawStr( 5, 45, "A Max: ");
  u8g.drawStr( 60, 45, sA_MAX);
}
 
void setup() {
  Serial.begin(9600);
  if (!bmp.begin()) {
    Serial.println("BMP init failed!");
    while (1);
  }
  else Serial.println("BMP init success!");
 
  bmp.setOversampling(4);
 
  u8g.setColorIndex(1);
  u8g.setFont(u8g_font_unifont);
}
 
void loop(void) {
  double T, P;
  char result = bmp.startMeasurment();
 
  if (result != 0) {
    delay(result);
    result = bmp.getTemperatureAndPressure(T, P);
 
    if (result != 0) {
      double A = bmp.altitude(P, P0);
 
      if ( A > A_MAX) {
        A_MAX = A;
      }
 
      if ( A < A_MIN || A_MIN == 0) {
        A_MIN = A;
      }
 
      //      Serial.print("T = \t"); Serial.print(T, 2); Serial.print(" degC\t");
      //      Serial.print("P = \t"); Serial.print(P, 2); Serial.print(" mBar\t");
      //      Serial.print("A = \t"); Serial.print(A, 2); Serial.println(" m");
 
      u8g.firstPage();
      do {
        draw(T, P, A);
      } while ( u8g.nextPage() );
      u8g.firstPage();
      delay(1000);
 
      do {
        draw2(A_MIN, A_MAX);
      } while ( u8g.nextPage() );
      u8g.firstPage();
      delay(1000);
       
    }
    else {
      Serial.println("Error.");
    }
  }
  else {
    Serial.println("Error.");
  }
 
  delay(100);
 
}





값이 잘 바뀌고 있습니다.




FIN

이제 뭘하지?

And