戻る

Raspberry Pi Picoでアナログセンサーを読み取る — Velxio

はじめに

Velxioブログのハンズオンチュートリアルへようこそ!今回は、Raspberry Pi PicoのADC(アナログ-デジタル変換器) について詳しく見ていきます。Picoは非常に強力な小型ボードで、その最も優れた機能のひとつがアナログ電圧の読み取りです。センサーやポテンショメータなどに最適です。この例では、3つの外部アナログ信号をポテンショメータから読み取り、さらに内蔵温度センサーの値も取得します。これらすべてが、VelxioのSPICE精度のシミュレーションでブラウザ上で動作します。

Velxioシミュレーターキャンバス上のpico-adc-read回路図

回路の解説

回路を見てみましょう。2つのポテンショメータとLEDに接続されたRaspberry Pi Pico(VelxioではNano RP2040バリアント)があります。

コンポーネント

配線の詳細

注意:PicoのADCピンは3.3V対応なので、3.3V電源を使用します。ポテンショメータは分圧器として機能し、可動接点に0〜3.3Vの可変電圧を出力します。

コードの解説

それではコードを見てみましょう。Raspberry Pi Pico用のArduinoスタイルのC++で記述されています。

// Raspberry Pi Pico — ADC Read Test
// Reads analog values from A0-A2 (GPIO26-28) and the internal temp sensor

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("=== Pico ADC Read ===");
  Serial.println("A0=GP26  A1=GP27  A2=GP28  Temp=internal");
  Serial.println("12-bit resolution (0-4095), 3.3V ref");
  Serial.println();

  analogReadResolution(12);
}

void loop() {
  int a0 = analogRead(A0);
  int a1 = analogRead(A1);
  int a2 = analogRead(A2);

  // Internal temperature sensor on channel 4
  // T = 27 - (V - 0.706) / 0.001721
  int tempRaw = analogRead(A3); // Channel 4 mapped to A3 by Pico core
  float voltage = tempRaw * 3.3f / 4095.0f;
  float tempC = 27.0f - (voltage - 0.706f) / 0.001721f;

  Serial.print("A0: "); Serial.print(a0);
  Serial.print("  A1: "); Serial.print(a1);
  Serial.print("  A2: "); Serial.print(a2);
  Serial.print("  Temp: "); Serial.print(tempC, 1); Serial.println(" C");

  delay(1000);
}

重要なポイント

重要な概念

よくある落とし穴とデバッグのヒント

推奨拡張機能

自分で試してみよう!

実験の準備はできましたか?Velxioでライブサンプルを開き、回路やコードを調整してみてください。以下のリンクをクリックして、ブラウザでシミュレーションを起動してください。

VelxioでPico ADC読み取りサンプルを起動

楽しい工作を!


共有:
David Montero

執筆者

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

関連記事


前の記事
Pico マルチプロトコルデモ: RP2040 でのシリアル、I2C、SPI、ADC
次の記事
Pico SPIループバック:RP2040のシリアル通信をテストする