1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Raspberry Pi Pico 2 WH で温度センサーを使ってみます。
使用する温度センサーは、BME680 です。温度だけでなく湿度、気圧なども計測できます。
私は大阪の千石電商で購入しました。

セットアップ

MicroPython を使って開発するので、そのセットアップをします。

上記の記事で記載した通り、Raspberry Pi Pico 2 WH で、MicroPython を使えるようにします。
それに加えて、今回は Thonny を使えるようにします。Thonny は Python の統合開発環境です。

まず、上記のサイトからホストマシン用のパッケージをダウンロードします。
その後、Thonny を使ってどのような開発をするのかを設定する必要があるので、設定します。

Thonny を起動して、Run -> Configure interpreter を選択して

  • Which kind of interpreter shoud Thonny use for running your code?
    • MicroPython (Raspberry Pi Pico)
  • Port or WebREPL
    • Board in FS mode @ /dev/cu.usbmodem*

を選択します。
usbmodem* の箇所は、Raspberry Pi Pico 2 WH をホストマシンに USB 接続すると作成されるデバイスファイルです。

うまく行くとこんな感じになります。

a.png

BME680 を制御する

Raspberry Pi Pico 2 WH と BME680 を物理的に接続する

Raspberry Pi Pico 2 WH は、Raspberry Pi Pico 2 W にピンヘッダーがついたものなので、Raspberry Pi Pico 2 W のピンの配置を見ます。

BME680 の説明書に書かれている通り、I2C通信ができるのでそれ用に繋ぎます。
I2C通信に加えて、SPI通信もできるようですが、今回そちらは使わないので、BME680 側では

  • VCC
  • GND
  • SDA
  • SCL

のピンを使います。

VCC は電源入力用のピンになるので、Raspberry Pi Pico W 側で 3V3(OUT)になっているところに繋ぎます。
GND はそのまま Raspberry Pi Pico W 側の GND に繋ぎます。

SDA, SCL は、Raspberry Pi Pico W 側では

  • I2C0 SDA, I2C0 SCL
  • I2C1 SDA, I2C1 SCL

のピンがあるのでどちらかの組み合わせで繋ぐようにします。

調べてみた感じ、I2C0 の方は予約されているっぽかったので I2C1 SDA, I2C1 SCL を使うことにします。

https://www.raspberrypi.com/documentation/computers/compute-module.html#dt-blob-bin

On most Raspberry Pi models, I2C0 is reserved for exclusive GPU use. dt-blob.bin defines the GPIO pins used for I2C0.

Raspberry Pi Pico WH BME680
GND GND
I2C1 SDA SDA
I2C1 SCL SCL
3V3(OUT) VCC

BME680 用のドライバを使えるようにする

BME680 を制御するには、それ用のドライバが必要になるのでそちらを準備します。

これを Thonny にコピーして Raspberry Pi Pico WH 側に保存します。
ここでは、bme680.py という名前で保存したとして、from bme680 import * でモジュールを import できるものとします。

温度を計測する

BME680 用のドライバファイルが管理されているリポジトリの README の通りにやるとうまくいきます。

I2Cクラスの初期化時に必要な Pin の指定が違うので変更します。

from machine import I2C, Pin
from bme680 import *
import time
- bme = BME680_I2C(I2C(-1, Pin(13), Pin(12)))
+ i2c = I2C(1, sda=Pin(2), scl=Pin(3))
+ bme = BME680_I2C(i2c)

for _ in range(3):
    print(bme.temperature, bme.humidity, bme.pressure, bme.gas)
    time.sleep(1)

id identifies a particular I2C peripheral. Allowed values for depend on the particular port/board

I2C の第一引数は、I2C0 を使っているか I2C1 を指定するので、今回の場合は1になります。

Access the pin peripheral (GPIO pin) associated with the given id

の通り、Pin の引数には GPIO の番号を指定します。
I2C1 SDA, I2C1 SCL はそれぞれ GP2, GP3 なので 2, 3 を指定します。

このままスクリプトを実行すると、温度・湿度・気圧・ガスの値を計測することができます。

スクリーンショット 2025-12-21 4.15.42.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?