.. include:: /index.rst :start-after: start_hello_message :end-before: end_hello_message .. _py_thermistor: 2.10 サーミスタ ==================================== **はじめに** サーミスタ(Thermistor)は温度に応じて抵抗値が変化する温度感知抵抗であり、温度測定や温度制御の用途で広く利用されています。光を検出するフォトレジスタとは異なり、サーミスタは温度の変化に応じて抵抗値が変化します。そのため、ヒートアラーム、サーモスタット、温度監視システムなどのプロジェクトに適しています。本プロジェクトでは、サーミスタを使用して周囲温度を測定し、摂氏(Celsius)と華氏(Fahrenheit)の両方で表示する方法を学びます。 ---------------------------------------------- **必要なもの** このプロジェクトに必要なコンポーネントは以下のとおりです。 .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_resistor` - |link_resistor_buy| * - :ref:`cpn_thermistor` - |link_thermistor_buy| * - :ref:`cpn_fusion_hat` - \- * - Raspberry Pi - \- ---------------------------------------------- **回路図** 以下はサーミスタをFusion HAT+に接続する方法を示した回路図です。 .. image:: img/fzz/2.2.2_sch.png ---------------------------------------------- **配線図** 以下の配線図を参考に回路を組み立ててください。 .. image:: img/fzz/2.2.2_bb.png :width: 80% :align: center 次の点を確認してください。 * サーミスタがFusion HAT+に正しく接続されていること。 * 電源とGNDの接続が確実であること。 * 配線が図と一致しており、正常に動作するように接続されていること。 ---------------------------------------------- **サンプルの実行** このチュートリアルで使用するすべてのサンプルコードは ``ai-lab-kit`` ディレクトリに含まれています。 以下の手順に従ってサンプルを実行してください。 .. raw:: html .. code-block:: shell cd ~/ai-lab-kit/python/ sudo python3 2.10_Thermistor.py このPythonスクリプトは、Fusion HAT+ を介してサーミスタからアナログ信号を読み取り、対応する温度を計算します。実行すると次のように動作します。 1. スクリプトは電圧値を継続的に読み取ります。 2. サーミスタの抵抗値( ``Rt`` )を計算し、ケルビン(Kelvin)、摂氏(Celsius)、華氏(Fahrenheit)の温度を求めます。 3. 計算された温度は、 ``Celsius: C Fahrenheit: F`` の形式で小数点以下2桁まで表示されます。 4. この処理は0.2秒ごとに繰り返され、 ``Ctrl+C`` が押されるまで実行されます。 ---------------------------------------------- **コード** 以下はこのプロジェクトで使用するPythonコードです。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 # -*- coding: utf-8 -*- from fusion_hat.adc import ADC import time import math thermistor = ADC('A3') # Run the process in a try-except block try: while True: # Read the voltage from the sensor Vr = thermistor.read_voltage() # Calculate the resistance of the thermistor if 3.3 - Vr < 0.1: print("Please check the sensor") continue else: Rt = 10000 * Vr / (3.3 - Vr) # Calculate the temperature in Kelvin temp = 1 / (((math.log(Rt / 10000)) / 3950) + (1 / (273.15 + 25))) # Convert Kelvin to Celsius Cel = temp - 273.15 # Convert Celsius to Fahrenheit Fah = Cel * 1.8 + 32 # Print the temperature in both Celsius and Fahrenheit print('Celsius: %.2f C Fahrenheit: %.2f F' % (Cel, Fah)) # Wait for 0.2 seconds before the next read time.sleep(0.2) # Handle KeyboardInterrupt for graceful termination except KeyboardInterrupt: pass ---------------------------------------------- **コードの解説** 1. **インポート:** .. code-block:: python from fusion_hat.adc import ADC import time import math このスクリプトでは必要なモジュールをインポートします。 ``fusion_hat`` ライブラリの ``ADC`` は電圧読み取りに使用し、 ``time`` は待機処理、 ``math`` は数値計算に使用します。 2. **初期化:** .. code-block:: python thermistor = ADC('A3') A3ピンに接続されたADCオブジェクトを初期化します。 3. **データ処理:** .. code-block:: python while True: # Read the voltage from the sensor Vr = thermistor.read_voltage() # Calculate the resistance of the thermistor if 3.3 - Vr < 0.1: print("Please check the sensor") continue else: Rt = 10000 * Vr / (3.3 - Vr) temp = 1 / (((math.log(Rt / 10000)) / 3950) + (1 / (273.15 + 25))) # Calculate the temperature in Kelvin Cel = temp - 273.15 # Convert Kelvin to Celsius Fah = Cel * 1.8 + 32 # Convert Celsius to Fahrenheit - サーミスタから電圧を読み取ります。 - サーミスタの抵抗値を計算します。 - Steinhart-Hart方程式を用いてケルビン温度を求めます。 - ケルビンを摂氏および華氏に変換します。 4. **出力:** .. code-block:: python print('Celsius: %.2f C Fahrenheit: %.2f F' % (Cel, Fah)) time.sleep(0.2) 計算された温度は摂氏と華氏の両方で画面に表示されます。 また、読み取り値の安定化とCPU負荷軽減のため、短い待機時間を設けています。 ---------------------------------------------- **トラブルシューティング** 1. **温度値が不正確または不安定** - **原因**: サーミスタのパラメータ設定が誤っている、または入力信号にノイズがある。 - **解決方法**: - サーミスタの25°C時の抵抗値(このスクリプトでは ``10000``)と ``3950`` のB値が正しいことを確認してください。 - ノイズを低減するためにコンデンサやソフトウェアフィルタを追加してください。 2. **ゼロ除算エラー** - **原因**: 電圧 ``Vr`` が0または3.3Vに非常に近く、抵抗計算時にゼロ除算が発生する。 - **解決方法**: ADC入力電圧が0〜3.3Vの範囲内であることを確認し、短絡や断線がないか配線を確認してください。 ---------------------------------------------- **拡張アイデア** 1. **LCDやOLEDへの温度表示** LCDまたはOLEDディスプレイを使用して温度をリアルタイムで表示できます。 2. **データログ記録** 温度データをファイルに保存して分析できます。 .. code-block:: python with open("temperature_log.txt", "a") as log_file: log_file.write(f"Celsius: {Cel:.2f}, Fahrenheit: {Fah:.2f}\n") 3. **しきい値アラート** 温度が一定値を超えた場合に警告を表示します。 .. code-block:: python if Cel > 30: print("Warning: High temperature!") 4. **LEDまたはブザーによる通知** 温度に応じて視覚または音によるフィードバックを追加できます。 .. code-block:: python from fusion_hat import Pin led = Pin(27.Pin.OUT) if Cel > 30: led.on() else: led.off() ---------------------------------------------- **まとめ** この実験では、Fusion HAT+ とサーミスタを使用して周囲温度を測定する方法を学びました。アナログ-デジタル変換とSteinhart-Hart方程式の原理を理解することで、さまざまな用途に対応した高度な温度監視および制御システムを構築することができます。