注釈

こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

参加する理由は?

  • エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。

  • 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。

  • 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。

  • 特別割引:最新製品の独占割引をお楽しみください。

  • 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。

👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!

超音波

概要

車をバックする際、衝突を避けるために車と周囲の障害物との距離を確認します。その距離を検出する装置が超音波センサーです。この実験では、超音波がどのように距離を検出するかを学びます。

必要なコンポーネント

このプロジェクトでは、以下のコンポーネントが必要です。

全てのキットを一式購入するのが便利です。こちらがリンクです:

名称

このキットのアイテム数

リンク

Elite Explorer Kit

300+

Elite Explorer Kit

下記のリンクから個別に購入することもできます。

コンポーネント紹介

購入リンク

Arduino Uno R4 WiFi

-

ブレッドボード

購入

ジャンパーワイヤー

購入

超音波モジュール

購入

I2C LCD1602

購入

配線図

../_images/06-ultrasonic_module_bb.png

回路図

../_images/06_ultrasonic_schematic.png

コード

注釈

  • elite-explorer-kit-main\basic_project\06-ultrasonic のパスの下にある 06-ultrasonic.ino ファイルを直接開く。

  • または、このコードをArduino IDEにコピーします。

06-ultrasonic.ino
 1/*
 2  This code read the distance from an ultrasonic sensor and display it on 
 3  an LCD screen. It uses an Arduino Uno R4 board and a 16x2 Liquid Crystal 
 4  Display (LCD) connected via I2C. The ultrasonic sensor is connected to 
 5  pins 3 and 4 for echo and trigger, respectively.
 6
 7  Board: Arduino Uno R4 
 8  Component: Ultrasonic Module
 9*/
10
11#include <LiquidCrystal_I2C.h>
12
13LiquidCrystal_I2C lcd(0x27, 16, 2);  // initialize the Liquid Crystal Display object with the I2C address 0x27, 16 columns and 2 rows
14
15// Define the pin numbers for the ultrasonic sensor
16const int echoPin = 3;
17const int trigPin = 4;
18
19void setup() {
20  pinMode(echoPin, INPUT);   // Set echo pin as input
21  pinMode(trigPin, OUTPUT);  // Set trig pin as output
22
23  lcd.init();       // initialize the LCD
24  lcd.clear();      // clear the LCD display
25  lcd.backlight();  // Make sure backlight is on
26}
27
28void loop() {
29  float distance = readDistance();  // Call the function to read the sensor data and get the distance
30
31  lcd.setCursor(0, 0);           //Place the cursor at Line 1, Column 1. From here the characters are to be displayed
32  lcd.print("Distance:");        ////Print Distance: on the LCD
33  lcd.setCursor(0, 1);           //Set the cursor at Line 1, Column 0
34  lcd.print("               ");  //Here is to leave some spaces after the characters so as to clear the previous characters that may still remain.
35  lcd.setCursor(7, 1);           //Set the cursor at Line 1, Column 7.
36  lcd.print(distance);           // print on the LCD the value of the distance converted from the time between ping sending and receiving.
37  lcd.setCursor(14, 1);          //Set the cursor at Line 1, Column 14.
38  lcd.print("cm");               //print the unit "cm"
39
40  delay(800);  // Delay for 800 milliseconds before repeating the loop
41}
42
43// Function to read the sensor data and calculate the distance
44float readDistance() {
45  digitalWrite(trigPin, LOW);   // Set trig pin to low to ensure a clean pulse
46  delayMicroseconds(2);         // Delay for 2 microseconds
47  digitalWrite(trigPin, HIGH);  // Send a 10 microsecond pulse by setting trig pin to high
48  delayMicroseconds(10);
49  digitalWrite(trigPin, LOW);  // Set trig pin back to low
50
51  // Measure the pulse width of the echo pin and calculate the distance value
52  float distance = pulseIn(echoPin, HIGH) / 58.00;  // Formula: (340m/s * 1us) / 2
53  return distance;
54}

コード解析

1. 超音波センサーとLCD1602の初期化

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);  // initialize the Liquid Crystal Display object with the I2C address 0x27, 16 columns and 2 rows

// Define the pin numbers for the ultrasonic sensor
const int echoPin = 3;
const int trigPin = 4;

void setup() {
  pinMode(echoPin, INPUT);               // Set echo pin as input
  pinMode(trigPin, OUTPUT);              // Set trig pin as output

  lcd.init();       // initialize the LCD
  lcd.clear();      // clear the LCD display
  lcd.backlight();  // Make sure backlight is on

}

2. LCD1602で距離を表示

void loop() {
  float distance = readDistance();  // Call the function to read the sensor data and get the distance

  lcd.setCursor(0, 0);         //Place the cursor at Line 1, Column 1. From here the characters are to be displayed
  lcd.print("Distance:");      ////Print Distance: on the LCD
  lcd.setCursor(0, 1);         //Set the cursor at Line 1, Column 0
  lcd.print("               ");  //Here is to leave some spaces after the characters so as to clear the previous characters that may still remain.
  lcd.setCursor(7, 1);         //Set the cursor at Line 1, Column 7.
  lcd.print(distance);         // print on the LCD the value of the distance converted from the time between ping sending and receiving.
  lcd.setCursor(14, 1);        //Set the cursor at Line 1, Column 14.
  lcd.print("cm");             //print the unit "cm"

  delay(800);                       // Delay for 800 milliseconds before repeating the loop
}

3. 時間を距離に変換

float readDistance(){// ...}

ここでの「PING」は、超音波センサーが超音波パルス(または「PING」)を送出し、そのエコーを待つプロセスを指します。

PINGは、2マイクロ秒以上のHIGHパルスによってトリガーされます。(クリーンなHIGHパルスを保証するために、事前に短いLOWパルスを与えます。)

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

エコーピンはPINGからの信号を読み取るために使用され、その持続時間はピンの送信からオブジェクトのエコーの受信までの時間(マイクロ秒)です。以下の関数を使用して持続時間を取得します。

pulseIn(echoPin, HIGH);

音速は340m/sまたは29マイクロ秒/センチメートルです。

これにより、ピンによる往復の移動距離が得られるため、障害物までの距離を取得するために2で割ります。

float distance = pulseIn(echoPin, HIGH) / 29.00 / 2;     // Formula: (340m/s * 1us) / 2