Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑮ 気圧センサ BMP280

 Adafruitから入手したMEMS気圧センサ BMP280(ボッシュ)を利用します。

 気圧温度が測定できます。加えて湿度も測定できるのがBME280です。

 改良型がBMP3xx、BME3xxです。

 気圧温度湿度に加えて大気の汚れを測定できるのがBME68xです。

AdafruitのStemma QT/Qwiicボード

 BMP280ボード解説のページ

 Stemma QT/Qwiic(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。

 コネクタは、表と裏のどちらも差し込めそうですが、ピンが内部の上部に並んでいるので、差し込める方向は一意です。ロック機構はないですが、すぐに抜けるということはありません。

気圧センサBMP280のおもなスペック

 BMP280のデータシート

  • 動作電圧 1.71~3.6 V
  • 測定範囲 300~1100hPa
  • 確度 ±0.12hPa(±1m)
  • インターフェース I2C(最大3.4MHz)、SPI(最大10MHz)
  • スレーブ・アドレス 0x77。裏面のADDRをショートして0x76に設定できる

使用環境

  • Arduino UNO R4 Minima
  • Arduino IDE 2.2.1
  • Windows10 22H2

接続

 Arduino UNO R4 MinimaのI2C信号とセンサ・ボードをJSTコネクタでつなぎます(Stemma QT/Qwiicボードの写真の比率は異なる)。

スレーブ・アドレスを確認

 従来からよく使われているi2cScanner.inoを動かしてスレーブ・アドレスを確認します。電源は5Vです。

  0x77を見つけてきました。

 以降のスケッチは3.3Vで動作させます。

 


ライブラリの用意

 BMP280で検索して、見つかったAdafruit BMP280ライブラリをインストールします。

 インストールを始めたとき、関連のライブラリや依存関係をインストールするかというパネルが出た場合は、全てをインストールを選びます。


サンプル・スケッチ

 メニューのファイル->スケッチ例から、Adafruit bmp280のbmp280test.inoを選択し、

コンパイル、実行します。

 

 I2Cの記述だけ残し、hPaに単位を替えたスケッチです。


//  Designed specifically to work with the Adafruit BMP280 Breakout
//  ----> http://www.adafruit.com/products/2651
//  Written by Limor Fried & Kevin Townsend for Adafruit Industries.

#include <Wire.h>
#include <Adafruit_BMP280.h> Adafruit_BMP280 bmp; // I2C void setup() { Serial.begin(9600); while ( !Serial ) delay(100); // wait for native usb Serial.println("BMP280 test"); unsigned status; status = bmp.begin(); if (!status) { Serial.println(F("Could not find a valid BMP280 sensor, check wiring or " "try a different address!")); Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16); Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); Serial.print(" ID of 0x60 represents a BME 280.\n"); Serial.print(" ID of 0x61 represents a BME 680.\n"); while (1) delay(10); } /* Default settings from datasheet. */ bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ Adafruit_BMP280::FILTER_X16, /* Filtering. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ } void loop() { float tempC = bmp.readTemperature(); float press = bmp.readPressure()/100.0; Serial.print(F("Temperature = ")); Serial.print(tempC); Serial.println(" *C"); Serial.print(F("Pressure = ")); Serial.print(press); Serial.println(" hPa"); Serial.println(); delay(2000); }

4桁の7セグメントLED表示器をつないで測定結果を表示する

 連載の第4回目の記事を参照しながら表示器を接続します。

Arduino UNO R4 Minimaでセンサ・インターフェーシング ④ 温湿度センサSi7021の測定結果を7セグメントLEDに表示


 スケッチです。

 7セグメントLED表示器は、第12回 温湿度センサ SHTC3でスレーブ・アドレスが重なったので、デフォルトの0x70から、ジャンパのA0をショートして0x71に変更してあります。デフォルトのまま使うときは0x70で使ってください。


//  Designed specifically to work with the Adafruit BMP280 Breakout
//  ----> http://www.adafruit.com/products/2651
//  Written by Limor Fried & Kevin Townsend for Adafruit Industries.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Adafruit_BMP280.h> Adafruit_7segment matrix = Adafruit_7segment(); Adafruit_BMP280 bmp; // I2C void setup() { Serial.begin(9600); while ( !Serial ) delay(100); // wait for native usb Serial.println("BMP280 + 7segment LED"); unsigned status; status = bmp.begin(); if (!status) { Serial.println(F("Could not find a valid BMP280 sensor, check wiring or " "try a different address!")); Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16); Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); Serial.print(" ID of 0x60 represents a BME 280.\n"); Serial.print(" ID of 0x61 represents a BME 680.\n"); while (1) delay(10); } /* Default settings from datasheet. */ bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ Adafruit_BMP280::FILTER_X16, /* Filtering. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ matrix.begin(0x71); matrix.setBrightness(0x05); // default 0x0E } void loop() { float tempC = bmp.readTemperature(); float press = bmp.readPressure() / 100.0; Serial.print(F("\nTemperature = ")); Serial.print(tempC); Serial.println(" *C"); Serial.print(F("Pressure = ")); Serial.print(press); Serial.println(" hPa"); matrix.print(tempC, DEC); matrix.writeDisplay(); delay(2000); matrix.print(press, DEC); matrix.writeDisplay(); delay(3000); }


 実行例です。

グラフィック・ディスプレイに測定結果を表示

 次の記事を参考に、グラフィック・ディスプレイに測定した温度と気圧を表示します。

  Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑤ 温湿度センサSi7021の測定結果をグラフィック・ディスプレイに表示

 スケッチです。


//  Designed specifically to work with the Adafruit BMP280 Breakout
//  ----> http://www.adafruit.com/products/2651
//  Written by Limor Fried & Kevin Townsend for Adafruit Industries.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP280.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Adafruit_BMP280 bmp; // I2C void setup() { Serial.begin(9600); while ( !Serial ) delay(100); // wait for native usb Serial.println("BMP280 + OLED SSD1306"); unsigned status; status = bmp.begin(); if (!status) { Serial.println(F("Could not find a valid BMP280 sensor, check wiring or " "try a different address!")); Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16); Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); Serial.print(" ID of 0x60 represents a BME 280.\n"); Serial.print(" ID of 0x61 represents a BME 680.\n"); while (1) delay(10); } /* Default settings from datasheet. */ bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ Adafruit_BMP280::FILTER_X16, /* Filtering. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); display.clearDisplay(); display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(4,3); display.println("Temperature:"); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(4,34); display.println("Pressure:"); display.display(); delay(200); } void loop() { float tempC = bmp.readTemperature(); float press = bmp.readPressure() / 100.0; Serial.print(F("\nTemperature = ")); Serial.print(tempC); Serial.println(" *C"); Serial.print(F("Pressure = ")); Serial.print(press); Serial.println(" hPa"); display.setTextSize(2); // Draw 2X-scale text display.setCursor(10,17); display.fillRect(10, 17, 70, 16, SSD1306_BLACK); display.println(tempC); display.setCursor(10, 46); display.fillRect(10, 46, 100, 16, SSD1306_BLACK); display.println(press); display.display(); delay(5000); }

 実行例です。

前へ

Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑭ 温湿度センサ HTS221

次へ

Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑯ 気圧センサ BME280