7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Intel Edisonで超音波距離センサを使ってみる

Last updated at Posted at 2016-07-28

電子工作は初心者ですが、これから色々作っていきたいと思います。

DSC_0039.jpg

手元に超音波距離センサがあったので、これを使って見ようと思います。
ちょっと見た目がカッコいいのと、超音波って言葉に惹かれました。 :grin:

今回は Intel Edison + Node.js で距離を測ることが目標です。

組み立て

まずは、Intel Edison 本体を Intel Edison Board for Arduino に取り付けます。
DSC_0008.jpg

簡単に取り付けられますね。
DSC_0014.jpg

次に Groveベースシールドを取り付けます。
DSC_0040.jpg

まずは Arduino で試す

センサの製品ページ(Grove - Ultrasonic Ranger)に Arduino のスケッチが載ってたので、
まずはこれを試してみようと思ったのですが、
UltrasonicRanger Library ってものを使っているみたいなので、その中のサンプルを試します。
DSC_0041.jpg

サンプルをそのまま動かしたのに、全然距離が測れない!!
serial_output.png

センサのドキュメントも見てみましたが、よくわかりません。 :dizzy_face:
Ultra-Sonic-seq.JPG

ライブラリのソースを読んでみたら、なんとなく理解できました。
簡単に言うと、シグナルPinを10マイクロ秒以上 HIGH にしてから,
LOW にすると40kHzのパルスを8回送信して Echo back されるらしい。
で、出力端子が HIGH になっている期間(マイクロ秒)の半分を音速で割れば距離を算出できる。

上手くいかなかったので Arduino UNO で試す

なぜ距離が取れないのかわからないので、Arduino UNO(互換ボード)で試してみます。
DSC_0043.jpg

何の問題もないですね。
serial_output2.png

今度は HC-SR04 で試す

手元にHC-SR04もあったので、こっちで試してみます。
DSC_0045.jpg
こっちのセンサはトリガーPin(Trig)とエコーPin(Echo)が分かれてます。
ドキュメントを見たところ、トリガーPinに入力してエコーPinから読み取ればいいみたい。
以下のように配線してみました。
Arduino_HC-SC04_ブレッドボード.png

こんな感じになりました。なかなかいいですね!
DSC_0046.jpg

UltrasonicRanger Library のソースを参考にスケッチを作成しました。

ultrasonic.ino
# define echoPin 7 // Echo Pin
# define trigPin 8 // Trigger Pin

long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  // 距離を計算(cm)
  distance = duration / 29 / 2;

  if (distance > 0) {
    Serial.print(distance);
    Serial.println(" cm");
  }

  delay(500);
}

距離もちゃんと取れてます。
serial_output3.png

再度 Intel Edison Board for Arduino で試す

Arduino UNOと同様に配線。
Edison_HC-SC04_ブレッドボード.png

DSC_0047.jpg

今度は距離が取れました!
serial_output4.png

Intel Edison + Node.js で距離を測定してみる

先ほど作った ultrasonic.ino を元に Node.js 用ソースを作成。
pulseIn 関数がなかったので、適当に作ってみました。

ultrasonic.js
var mraa = require('mraa');
var sleep = require('sleep');
var μs = require('microseconds');

// GPIO
// trigger pin: send trigger signal
var trigPin = new mraa.Gpio(8);
trigPin.dir(mraa.DIR_OUT);

// echo pin: receive echo from module
var echoPin = new mraa.Gpio(7);
echoPin.dir(mraa.DIR_IN);

var LOW  = 0;
var HIGH = 1;

function pulseIn(pin, value, timeout) {
    timeout = timeout || 1000000;

    var pulseOn, pulseOff;

    var start = μs.now();
    
    while (pin.read() != value) {
        if (μs.since(start) > timeout) {
            return 0;
        }
        pulseOn = μs.now();
    }
    while (pin.read() == value) {
        if (μs.since(start) > timeout) {
            return 0;
        }
        pulseOff = μs.now();
    }
    // duration[microsec]
    var duration = pulseOff - pulseOn;
    
    return duration;
}

setInterval( function () {
    // Send Trigger Signal to Module
    trigPin.write(LOW);
    sleep.usleep(2);
    trigPin.write(HIGH);
    sleep.usleep(5);
    trigPin.write(LOW);
    
    // Measure duration of echo pulse
    var duration = pulsein(echoPin, HIGH);

    // Calculate Distance from duration
    var distance = duration / 29 / 2;
    
    if (distance > 0) {
        // Show the distance on the console
        console.log(distance + ' cm');
    }
   
}, 100);

xdk_console.png

まとめ

Intel Edison + Groveベースシールド + 超音波距離センサだと
どうして上手くいかなかったのか結局わかりませんでしたが、
何とか目標を達成することができてよかったです。

次は Raspberry Pi でやってみようかなぁ。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?