Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑯ 気圧センサ BME280
Adafruitから入手したMEMS気圧センサ BME280(ボッシュ)を利用します。
気圧と温度、湿度が測定できます。
●AdafruitのStemma QT/Qwiicボード
BMP280の外見は上から見ると長方形、BME280は正方形です。内部のレジスタ類は、BMP280に湿度関係を追加した内容になっています。
Stemma QT/Qwiic(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。
コネクタは、表と裏のどちらも差し込めそうですが、ピンが内部の上部に並んでいるので、差し込める方向は一意です。ロック機構はないですが、すぐに抜けるということはありません。
●気圧センサBMP280のおもなスペック
- 動作電圧 1.71~3.6 V
- 動作温度範囲 -40℃~85℃
- 気圧測定範囲 300~1100hPa、分解能 ±0.12hPa(±1m)
- 温度測定範囲 -40~+85℃、確度 ±0.5℃
- 湿度測定範囲 0~100%、確度 ±3%
- インターフェース 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で動作させます。
●ライブラリの用意
BME280で検索して、見つかったAdafruit BME280ライブラリをインストールします。
インストールを始めたとき、関連のライブラリや依存関係をインストールするかというパネルが出た場合は、全てをインストールを選びます。
●サンプル・スケッチ
メニューのファイル->スケッチ例から、Adafruit bme280のbme280test.inoを選択し、
コンパイル、実行します。
SPI関連を取り除き、I2Cの記述だけ残したスケッチです。
// Designed specifically to work with the Adafruit BME280 Breakout
// ----> http://www.adafruit.com/products/2650
// Written by Limor Fried & Kevin Townsend for Adafruit Industries.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C
void setup() {
Serial.begin(9600);
while(!Serial); // time to get serial running
Serial.println(F("BME280 test"));
unsigned status;
// default settings
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.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);
}
Serial.println();
}
void loop() {
printValues();
delay(5000);
}
void printValues() {
float tempC = bme.readTemperature();
float press = bme.readPressure() / 100.0;
float humi = bme.readHumidity();
Serial.print("Temperature = ");
Serial.print(tempC); Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(press); Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(humi); Serial.println(" %");
Serial.println();
}
●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 BME280 Breakout
// ----> http://www.adafruit.com/products/2650
// Written by Limor Fried & Kevin Townsend for Adafruit Industries.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_7segment matrix = Adafruit_7segment();
Adafruit_BME280 bme; // I2C
void setup() {
Serial.begin(9600);
while(!Serial) delay(100); // time to get serial running
Serial.println("BME280 + 7segment LED");
unsigned status;
// default settings
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.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);
}
Serial.println();
matrix.begin(0x71);
matrix.setBrightness(0x05); // default 0x0E
}
void loop() {
printValues();
delay(5000);
}
void printValues() {
float tempC = bme.readTemperature();
float press = bme.readPressure() / 100.0;
float humi = bme.readHumidity();
Serial.print("Temperature = ");
Serial.print(tempC); Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(press); Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(humi); Serial.println(" %");
Serial.println();
matrix.print(tempC, DEC);
matrix.writeDisplay();
delay(2000);
matrix.print(press, DEC);
matrix.writeDisplay();
delay(2000);
matrix.print(humi, DEC);
matrix.writeDisplay();
}
実行例です。
●グラフィック・ディスプレイに測定結果を表示
次の記事を参考に、グラフィック・ディスプレイに測定した気圧、湿度、温度を表示します。
Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑤ 温湿度センサSi7021の測定結果をグラフィック・ディスプレイに表示
スケッチです。
// Designed specifically to work with the Adafruit BME280 Breakout
// ----> http://www.adafruit.com/products/2650
// Written by Limor Fried & Kevin Townsend for Adafruit Industries.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.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_BME280 bme; // I2C
void setup() {
Serial.begin(9600);
while(!Serial) delay(100); // time to get serial running
Serial.println("BME280 + OLED SSD1306");
unsigned status;
// default settings
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.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);
}
Serial.println();
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(3,3);
display.println("Temperature:");
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(79,3);
display.println("Humidity");
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(4,34);
display.println("Pressure:");
display.display();
delay(200);
}
void loop() {
printValues();
delay(5000);
}
void printValues() {
float tempC = bme.readTemperature();
float press = bme.readPressure() / 100.0;
float humi = bme.readHumidity();
Serial.print("Temperature = ");
Serial.print(tempC); Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(press); Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(humi); Serial.println(" %");
Serial.println();
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(10,17);
display.fillRect(10, 17, 70, 16, SSD1306_BLACK);
display.println(tempC);
display.setCursor(80,17);
display.fillRect(80, 17, 46, 16, SSD1306_BLACK);
display.println(humi,1);
display.setCursor(10, 46);
display.fillRect(10, 46, 100, 16, SSD1306_BLACK);
display.println(press);
display.display();
}
実行例です。