.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _2.1.7_py_pi5:
2.1.7 ポテンショメータ
====================================
.. note::
.. image:: ../img/mcp3008_and_adc0834.jpg
:width: 25%
:align: left
キットのバージョンによって、 **ADC0834** または **MCP3008** が含まれています。
該当するセクションを選択してください。
はじめに
------------
ADC(アナログデジタルコンバータ)機能は、アナログ信号をデジタル信号に変換するために使用できます。この実験では、ADC0834を使用してADCに関連する機能を取得します。ここでは、ポテンショメータを使用してこのプロセスを実装します。ポテンショメータは物理的な量である電圧を変化させ、ADC機能によって変換されます。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/2.1.7_potentiometer_list.png
一式を購入するのが便利です、こちらがリンクです:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - 名前
- このキットのアイテム
- リンク
* - Raphael Kit
- 337
- |link_Raphael_kit|
以下のリンクから別々に購入することもできます。
.. list-table::
:widths: 30 20
:header-rows: 1
* - コンポーネントの紹介
- 購入リンク
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_led`
- |link_led_buy|
* - :ref:`cpn_potentiometer`
- |link_potentiometer_buy|
* - :ref:`cpn_adc0834`
- \-
回路図
-----------------
.. image:: ../python_pi5/img/2.1.7_potentiometer_second_1.png
.. image:: ../python_pi5/img/2.1.7_potentiometer_second_2.png
実験手順
-----------------------
**ステップ1**: 回路を組み立てます。
.. image:: ../python_pi5/img/2.1.7_Potentiometer_circuit.png
.. note::
画像に示されている対応する位置を参照して、チップを配置してください。チップの溝は左側にある必要があります。
**ステップ2**: コードファイルを開きます。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ3**: 実行します。
.. raw:: html
.. code-block::
sudo python3 2.1.4_Potentiometer_zero.py
コードが実行されたら、ポテンショメータのつまみを回すと、LEDの輝度がそれに応じて変化します。
.. warning::
エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。
**コード**
.. note::
以下のコードを **変更/リセット/コピー/実行/停止** できます。ただし、それに先立ち、 ``raphael-kit/python-pi5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、効果を直接確認するために実行できます。
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import PWMLED
import ADC0834
import time
# Initialize a PWM LED on GPIO pin 22
led = PWMLED(22)
# Set up the ADC0834 module
ADC0834.setup()
def MAP(x, in_min, in_max, out_min, out_max):
"""
Map a value from one range to another.
:param x: The value to be mapped.
:param in_min: The lower bound of the value's current range.
:param in_max: The upper bound of the value's current range.
:param out_min: The lower bound of the value's target range.
:param out_max: The upper bound of the value's target range.
:return: The mapped value.
"""
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
try:
while True:
# Get the current reading from the ADC0834 module
res = ADC0834.getResult()
print('res = %d' % res)
# Map the ADC value to a range suitable for setting LED brightness
R_val = MAP(res, 0, 255, 0, 100)
# Set the LED brightness
led.value = float(R_val / 100)
# Wait for 0.2 seconds before reading again
time.sleep(0.2)
# Graceful exit when 'Ctrl+C' is pressed
except KeyboardInterrupt:
led.value = 0 # Turn off the LED
**コードの説明**
#. PWM LEDの制御には「gpiozero」、アナログからデジタルへの変換には「ADC0834」、遅延の実装には「time」が使用されています。
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import PWMLED
import ADC0834
import time
#. GPIOピン22に接続されたPWMLEDオブジェクトを初期化し、ADC0834コンバータをセットアップします。
.. code-block:: python
# Initialize a PWM LED on GPIO pin 22
led = PWMLED(22)
# Set up the ADC0834 module
ADC0834.setup()
#. 1つの範囲の値を別の範囲に変換するための「MAP」という名前の関数を定義し、ADCの値を適切なLEDの明るさにマッピングするのに役立ちます。
.. code-block:: python
def MAP(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#. ループ内でADC値を連続して読み取り、ADCの読み取り値(0-255)をLEDの明るさ(0-100)にマッピングします。このマップされた値に基づいてLEDの明るさを調整します。視認性と安定性を向上させるために0.2秒の遅延を実装します。
.. code-block:: python
try:
while True:
# Get the current reading from the ADC0834 module
res = ADC0834.getResult()
print('res = %d' % res)
# Map the ADC value to a range suitable for setting LED brightness
R_val = MAP(res, 0, 255, 0, 100)
# Set the LED brightness
led.value = float(R_val / 100)
# Wait for 0.2 seconds before reading again
time.sleep(0.2)
# Graceful exit when 'Ctrl+C' is pressed
except KeyboardInterrupt:
led.value = 0 # Turn off the LED