1
0

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 5とDHT-11で温度・湿度を計測する

Posted at

購入したセンサー

下記のキットに温湿度センサーとしてDHT-11が含まれており、それを利用しました。

1. DHT11センサーをRaspberry Piに接続する

この動画のとおりに接続で問題ありませんでした。上記購入先が提供しているビデオチュートリアルです。
ただし、私はRaspberry Pi 5を使用していたので、この動画のプログラムでは動きませんでした。Raspberry Pi 5用のプログラム作成方法については後述します。

必要なもの

必要なコンポーネントは、Raspberry Pi、ジャンパーケーブル、DHT11センサー、10k抵抗です。

DHT11センサーをRaspberry Piに接続する

ピン配置

  • DHT11 の VCC ピンを Raspberry Pi の 3.3V に接続
  • DHT11 の GND ピンを Raspberry Pi の GND に接続
  • DHT11 のデータピン(DAT)を、Raspberry Pi の GPIO 21(BCM番号)など使用していないGPIOに接続

Raspberry Pi以外は上記の購入キットに含まれているはずです。
動画のとおりに接続したらブレッドボード側はOK。

2. DHT11用のdtoverlayを設定する

私はRaspberry Pi5を使用したのですが、Raspberry Pi 5はPRI.GPIOライブラリが使えず、Raspberry Pi 4以前までのものと、やり方が少し違います。

下記を参考に進めました。

設定ファイルを編集

  1. ターミナルを開き、以下のコマンドを入力して /boot/firmware/config.txt を編集します。

    sudo nano /boot/firmware/config.txt
    
  2. [all] セクションのすぐ下に、以下の1行を追加します。(GPIO 21 を使っている例)

    [all]
    dtoverlay=dht11,gpiopin=21
    
  3. Ctrl + XY キーで保存して、エディタを終了します。

  4. Raspberry Piを再起動

    sudo reboot
    

3. DHT11センサーが認識されているか確認

再起動後、次の手順でシステムがDHT11を認識しているか確認します。

  1. ターミナルで以下を入力して、IIOデバイスディレクトリに移動

    cd /sys/bus/iio/devices
    ls
    
  2. iio:device0 のフォルダが表示される場合、DHT11が認識されています。さらに中身を確認します。

    cd iio:device0
    ls
    
    • in_temp_input(温度データ)
    • in_humidityrelative_input(湿度データ)

    といったファイルがあることがわかります。

  3. 実際に数値が取得できるか試します。

    cat in_temp_input            # 温度データ(例: 20000)
    cat in_humidityrelative_input  # 湿度データ(例: 36000)
    

    これらの値が出力されれば、センサーが正常に動作している証拠です。

4. Pythonスクリプトで温度・湿度を取得

最後に、PythonからDHT11センサーの値を読み取るサンプルスクリプトを紹介します。

適当なPythonファイル(例: read_dht11.py)を作成し、以下のコードを貼り付けて保存します。

import time

device0 = "/sys/bus/iio/devices/iio:device0"

# ファイルの先頭行を読み取って整数値を返す関数
def readFirstLine(filename):
    try:
        f = open(filename,"rt")
        value = int(f.readline())
        f.close()
        return True, value
    except ValueError:
        f.close()
        return False, -1
    except OSError:
        return False, 0

try:
    while True:
        # 温度を取得
        Flag, Temperature = readFirstLine(device0 + "/in_temp_input")
        print("Temperature:", end="")
        if Flag:
            print(Temperature // 1000, "", end="\t")
        else:
            print("N.A.", end="\t")

        # 湿度を取得
        Flag, Humidity = readFirstLine(device0 + "/in_humidityrelative_input")
        print("Humidity:", end="")
        if Flag:
            print(Humidity // 1000, "%")
        else:
            print("N.A.")

        time.sleep(2.0)
except KeyboardInterrupt:
    pass

スクリプトを実行します。

python3 readDHT11.py

正常に動作していれば、数秒おきに以下のように温度・湿度が表示されます。

Temperature:26 ℃   Humidity:32 %
Temperature:27 ℃   Humidity:31 %
...
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?