1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Arduinoの部品導通記録①~超音波距離センサー

1
Last updated at Posted at 2026-02-26

Arduinoで、超音波距離センサーの導通を行った

環境

HW/SW バージョン他
筐体 Arduino Uno R4 Minima
IDE Arduino IDE 2.3.7
部品 超音波距離センサー(HC-SR04) 部品詳細
その他部品 ブレッドボード、ジャンパーワイアー

回路

テキスト回路図(超音波距離センサー(HC-SR04)→Arduino)

HC-SR04                Arduino UNO
---------------        -----------------
VCC   ----------------> 5V
Trig  ----------------> D9
Echo  ----------------> D10
GND   ----------------> GND

実際の回路写真

プログラム仕様

・実行すると、超音波距離センサー(HC-SR04)から読み取った障害物までの距離を0.2秒ごとにシリアルモニター表示

プログラムソース

HC-SR04.ino
#define TRIG_PIN 9
#define ECHO_PIN 10

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // トリガー信号を送る(10μs)
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Echo の HIGH の長さを測定
  long duration = pulseIn(ECHO_PIN, HIGH);

  // 距離に変換(cm)
  long distance = duration / 58;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(200);
}

実行

image.png

※遮蔽物を近づけたり、遠ざけたりする

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?