Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑲ 気圧センサ DPS310
Adafruitから入手したMEMS気圧センサ DPS310(Infineon)を利用します。
気圧と温度が測定できます。気圧の分解能が高く、階段の上り下りの動きを高度データで取得できます。
●AdafruitのStemma QT/Qwiicボード
Stemma QT/Qwiic(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。
コネクタは、表と裏のどちらも差し込めそうですが、ピンが内部の上部に並んでいるので、差し込める方向は一意です。ロック機構はないですが、すぐに抜けるということはありません。
●気圧センサDPS310のおもなスペック
- 動作電圧 1.7~3.6 V
- 気圧測定範囲 300~1200hPa、確度 ±0.06hPa(±0.5m)
- 分解能 ±0.002hPa(or ±0.02 m)(high precision mode)
- 温度測定範囲 -40~+85℃、確度;±0.5℃
- インターフェース SPI(最大10MHz)、I2C(最大3.4MHz)
- スレーブ・アドレス 0x77。SDOピン(裏面の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で動作させます。
●ライブラリの用意
DPS310で検索して、見つかったAdafruit DPS310ライブラリをインストールします。
インストールを始めたとき、関連のライブラリや依存関係をインストールするかというパネルが出た場合は、全てをインストールを選びます。
●サンプル・スケッチ
メニューのファイル->スケッチ例から、Adafruit DPS310のdps310_simpletest.inoを選択し、
コンパイル、実行します。
I2Cの記述だけ残したスケッチです。
// This example shows how to read temperature/pressure
#include <Adafruit_DPS310.h>
Adafruit_DPS310 dps;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("DPS310");
if (! dps.begin_I2C()) { // Can pass in I2C address here
Serial.println("Failed to find DPS");
while (1) yield();
}
Serial.println("DPS OK!");
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
}
void loop() {
sensors_event_t temp_event, pressure_event;
while (!dps.temperatureAvailable() || !dps.pressureAvailable()) {
return; // wait until there's something to read
}
dps.getEvents(&temp_event, &pressure_event);
float tempC = temp_event.temperature;
float press = pressure_event.pressure;
Serial.print(F("Temperature = "));
Serial.print(tempC); Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(press); Serial.println(" hPa");
Serial.println();
delay(5000);
}
●4桁の7セグメントLED表示器をつないで測定結果を表示する
連載の第4回目の記事を参照しながら表示器を接続します。
Arduino UNO R4 Minimaでセンサ・インターフェーシング ④ 温湿度センサSi7021の測定結果を7セグメントLEDに表示
スケッチです。
7セグメントLED表示器は、第12回 温湿度センサ SHTC3でスレーブ・アドレスが重なったので、デフォルトの0x70から、ジャンパのA0をショートして0x71に変更してあります。デフォルトのまま使うときは0x70で使ってください。
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Adafruit_DPS310.h>
Adafruit_7segment matrix = Adafruit_7segment();
Adafruit_DPS310 dps;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("DPS310");
if (! dps.begin_I2C()) { // Can pass in I2C address here
Serial.println("Failed to find DPS");
while (1) yield();
}
Serial.println("DPS OK!");
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
matrix.begin(0x71);
matrix.setBrightness(0x05); // default 0x0E
}
void loop() {
sensors_event_t temp_event, pressure_event;
while (!dps.temperatureAvailable() || !dps.pressureAvailable()) {
return; // wait until there's something to read
}
dps.getEvents(&temp_event, &pressure_event);
float tempC = temp_event.temperature;
float press = pressure_event.pressure;
Serial.print("\nTemperature = ");
Serial.print(tempC); Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(press); Serial.println(" hPa");
matrix.print(tempC, DEC);
matrix.writeDisplay();
delay(2000);
matrix.print(press, DEC);
matrix.writeDisplay();
delay(5000);
}
実行例です。
●グラフィック・ディスプレイに測定結果を表示
次の記事を参考に、グラフィック・ディスプレイに測定した温度と気圧を表示します。
Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑤ 温湿度センサSi7021の測定結果をグラフィック・ディスプレイに表示
スケッチです。
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_DPS310.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_DPS310 dps;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("DPS310 + OLED SSD1306");
if (! dps.begin_I2C()) { // Can pass in I2C address here
Serial.println("Failed to find DPS");
while (1) yield();
}
Serial.println("DPS OK!");
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
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() {
sensors_event_t temp_event, pressure_event;
while (!dps.temperatureAvailable() || !dps.pressureAvailable()) {
return; // wait until there's something to read
}
dps.getEvents(&temp_event, &pressure_event);
float tempC = temp_event.temperature;
float press = pressure_event.pressure;
Serial.print("\nTemperature = ");
Serial.print(tempC); Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(press,3); 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, 110, 16, SSD1306_BLACK);
display.println(press,3);
display.display();
delay(5000);
}
実行例です。