HC-SR04 超音波距離センサーと SSD1306 OLED ディスプレイ用の Arduino コードとビデオ
このチュートリアルでは、HC-SR04超音波距離センサーとSSD1306 OLEDディスプレイを組み合わせて距離測定を視覚化する方法を説明します。このプロジェクトではコンピュータを使わずに距離を測定できるため、ロボット工学を含むさまざまな用途に最適です。プロジェクトを終えるころには、距離測定結果を直接OLED画面に表示する実用的なセットアップが完成します。


進めるにあたって、両方のコンポーネントに必要な配線接続を説明し、これらのコンポーネントを統合するArduinoコードのウォークスルーを行います。これにより、センサーがどのように距離を測定し、そのデータをOLEDディスプレイに出力するかを理解できるようになります。さらに詳しくは、本チュートリアルに関連するビデオ(ビデオ内 00:00)を参照してください。
ハードウェアの解説
このプロジェクトの主な構成要素は、HC-SR04 超音波距離センサーと SSD1306 OLED ディスプレイです。HC-SR04 は超音波を用いて動作し、トリガーピンから音波パルスを発信し、エコーピンで反射波を受信します。音が戻るまでの時間を計算することで、物体までの距離を測定できます。このセンサーは短距離測定に非常に有効で、通常は最大400〜500 cm 程度まで測定できます。
SSD1306 OLEDディスプレイは、テキストやグラフィックスを表示できるI2Cインターフェースのディスプレイです。SDA(データ線)とSCL(クロック線)の2本のピンで通信します。今回のセットアップでは、これらのピンを特定のArduinoピンに接続しており、容易に統合して制御できます。
データシートの詳細
| 製造元 | 様々な |
|---|---|
| 部品番号 | HC-SR04 |
| ロジック/入出力電圧 | 5ボルト |
| 供給電圧 | 5 V |
| 出力電流(チャンネルごと) | 15 mA |
| ピーク電流(チャンネルあたり) | 20 mA |
| PWM周波数ガイドライン | 該当なし |
| 入力ロジックの閾値 | 0.3×Vccから0.7×Vccまで |
| 電圧降下 / RDS(オン)/ 彩度 | 該当なし |
| 熱限界 | 0〜70°C |
| パッケージ | 4ピンモジュール |
| 備考/バリエーション | 標準モジュール |
- 両方のコンポーネントに対して適切な電源(5 V)を確保してください。
- I2C通信では、必要に応じてプルアップ抵抗を使用してください。
- トリガー線とエコー線は干渉を避けるため短くしてください。
- 正確な測定のために超音波センサーの向きを確認してください。
- 機械的振動を避けるため、ディスプレイは安定した面に設置してください。
配線手順

HC-SR04 超音波センサーを接続するには、まず VCC ピン(通常は赤)を Arduino の 5V ピンに配線します。次に、GND ピン(一般的に黒または黄色)を Arduino の GND ピンのいずれかに接続します。トリガーピン(多くは青)は Arduino のデジタルピン 12 に配線し、エコーピン(通常は緑)はデジタルピン 11 に接続します。
SSD1306 OLEDディスプレイの場合、VCCピンはArduinoの5Vピンに、GNDピンはGNDに接続します。SCLピンはArduinoのA5ピンに、SDAピンはA4ピンに接続してください。専用のI2Cピンを備えた別のArduinoモデルを使用している場合は、ディスプレイをそれらのピンに直接接続してください。
コード例と解説
setup 関数では、ディスプレイとシリアル通信を初期化します。以下の抜粋は、ディスプレイの設定方法を示しています:
void setup() {
Serial.begin(9600);// set serial monitor with 9600 baud
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D
}ここで、ディスプレイはI2Cアドレスで初期化され、Arduinoと通信できるようになります。シリアルモニタも距離の値を表示するように設定されています。
メインループでは、ディスプレイをクリアし、距離を測定して表示します。以下のコードスニペットは、距離測定の処理方法を示しています:
void loop() {
display.clearDisplay();
String distance = String(sonar.ping_cm());// get distance and convert it to string for display
robojaxText(distance +"cm", 3, 20, 3, false);
display.display();
delay(50); // Wait 50ms between pings
}このループは距離を継続的に測定し、現在の測定値でディスプレイを更新します。robojaxText関数はOLED画面にテキストを表示するために使用されます。
デモ/何を期待するか
セットアップが完了しコードをアップロードすると、OLED画面に距離がセンチメートルで表示されます。物体をセンサーに近づけたり遠ざけたりすると、表示される値がそれに応じて変化するはずです。センサーの向きが正しく、遮られていないことを確認してください(動画の02:30参照)。
動画のタイムスタンプ
- 00:00- プロジェクトの紹介
- 01:15- 配線手順
- 02:30- コードの説明
- 04:00- センサーの実演
/*
* This is the Arduino code for the HC-SR04 Ultrasonic Distance Sensor with SSD1306 Display
* to measure the distance using Arduino for a robotic car and other applications
* Watch the video https://youtu.be/Pgx5fNF4Q6M
*
* Written by Ahmad Shamshiri for Robojax Video
* Date: December 21, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
*/
/* Original Code
from https://github.com/adafruit/Adafruit_SSD1306
// https://playground.arduino.cc/Code/NewPing
* Modified for Robojax video on December 21, 2017
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
*/
//// start of SSD1306 display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 64
#define LOGO16_GLCD_WIDTH 128
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//// end of SSD1306 display
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600);// set serial monitor with 9600 baud
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
}// setup end
void loop() {
display.clearDisplay();
robojaxText("Distance", 3, 0, 2, false);
String distance = String(sonar.ping_cm());// get distance and convert it to string for display
robojaxText(distance +"cm", 3, 20, 3, false);
display.display();
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}// loop end
/*
* robojaxText(String text, int x, int y,int size, boolean d)
* text is the text string to be printed
* x is the integer x position of text
* y is the integer y position of text
* size is the text size, 1, 2, 3 etc
* d is either true or false. Use true to display.
*/
void robojaxText(String text, int x, int y,int size, boolean d) {
display.setTextSize(size);
display.setTextColor(WHITE);
display.setCursor(x,y);
display.println(text);
if(d){
display.display();
}
}
リソースと参考文献
-
外部Arduino公式ウェブサイトのHC-SR04超音波ライブラリplayground.arduino.cc
ファイル📁
ファイルは利用できません。