Ping

はじめに

超音波距離センサを使い距離を測定します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
	- +V connection of the PING))) attached to +5V
	- GND connection of the PING))) attached to ground
	- SIG connection of the PING))) attached to digital pin 7

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping
*/

// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;
 

const int型の変数pingPinを定義し初期化します。

setup()

27
28
29
30
31
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 

Serial.begin()でシリアルポートを初期化します。

loop()

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}
 

pinMode()を使いpingPinを出力モードにします。digitalWrite()を使い、5マイクロ秒間pingPinをHIGHにし、超音波を発生させます。

pinMode()で、pingPinを入力モードにし、pulseIn()で、pingPinがHIGHになっている時間を測定し、durationに代入します。microsecondsToInches()とmicrosecondsToCentimeters()を呼び出し、時間を距離に変換した値をそれぞれ、inchesとcmに代入します。

Serial.print()Serial.println()を使い、inchesとcmをシリアルコンソールに送信します。

最後にdelay()を使い、しばらく待ちます。

microsecondsToInches

65
66
67
68
69
70
71
72
73
long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
  return microseconds / 74 / 2;
}
 

このセンサは、超音波を送信してから受信するまでの時間を測ります。音が1インチ進むのに73.746マイクロ秒かかるので、時間を73.746で割ると、超音波が進んだ距離が求まります。超音波は物体にあたって跳ね返るため、物体までの距離は計算した距離をさらに2で割る必要があります。

引数として渡された値を74で割り、さらに2で割ります。その結果を返します。

microsecondsToCentimeters

74
75
76
77
78
79
long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

音が1センチ進むのに29マイクロ秒かかります。

引数として渡された値を29で割り、さらに2で割ります。その結果を返します。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system