SpresenseでLチカから始める (21) 距離TFmini

 TFMiniは、赤外線を使ったToF(Time of Flight)タイプの距離センサです。赤外線を発して対象物にぶつかって戻ってくる時間を測定することで、距離を測ります。外形は43×15×16mmと小型、測定範囲は30cmから12mと広めで解像度は5mmです。Arduino UNOの接続事例はこちらです。

 Sparkfunから購入しましたが、スイッチサイエンスでも扱っています。利用方法は、こちらのページを参照しました。

TFMiniのおもなスペック

  • 電源電圧;4.5~6V
  • 消費電力;120mW以下。LEDのピーク電流は最大800mA
  • UARTの信号レベル;3.3V
  • 通信速度;115200ボー、8ビット・データ、パリティなし、1ストップ・ビット
  • 解像度;5mm
  • 確度;6m以下では1%、6~12mでは2%

接続

 本体には、4ピンのコネクタで接続するケーブルが付属しています。Tx/Rxの信号レベルは3.3Vなので、Spresenseは3.3Vで動作させました。Spresenseの0番と1番ピンはArduino UNOのようにUSB-シリアル通信側にはつながれておらず、独立しています。名前はSerial2です。

1ピン(緑) 2ピン(白) 3ピン(赤) 4ピン(黒)
Tx Rx +5V GND

データ・フォーマット

 モジュールの出力は9バイトで構成されており、1、2バイトは目印で、9バイト目はチェック・サムです。それぞれ16進のデータです。

byte1 byte2 byte3 byte4 byte5 byte6 byte7 byte8 byte9
0x59 0x59 距離データ下位バイト 距離データ上位バイト 信号強度下位バイト 信号強度上位バイト 予約 元の信号品質の程度 チェックサム

Arduino用ライブラリ

 Arduino IDEは1.8.7を使っています。メイン・メニューのツールからライブラリ管理を選択します。

 検索の欄に「tfmini」を入れ、最初に表示されたライブラリを選択してインストールします。

 サンプル・スケッチ例からBasicReadingを選択します。

 TFminiとはSoftwareserialを使って通信しているので、この部分をSerial2に変更します。

/*
Example code for Benewake TFMini time-of-flight distance sensor.
by Peter Jansen (December 11/2017)
This example code is in the public domain.
This example communicates to the TFMini using a SoftwareSerial port at 115200,
while communicating the distance results through the default Arduino hardware
Serial debug port.
SoftwareSerial for some boards can be unreliable at high speeds (such as 115200).
The driver includes some limited error detection and automatic retries, that
means it can generally work with SoftwareSerial on (for example) an UNO without
the end-user noticing many communications glitches, as long as a constant refresh
rate is not required.
The (UNO) circuit:
* Uno RX is digital pin 10 (connect to TX of TF Mini)
* Uno TX is digital pin 11 (connect to RX of TF Mini)
THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//#include <SoftwareSerial.h>
#include "TFMini.h"
// Setup software serial port
//SoftwareSerial mySerial(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(9600);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
Serial.println ("Initializing...");
// Step 2: Initialize the data rate for the SoftwareSerial port
// mySerial.begin(TFMINI_BAUDRATE);
Serial2.begin(TFMINI_BAUDRATE);
// Step 3: Initialize the TF Mini sensor
// tfmini.begin(&mySerial);
tfmini.begin(&Serial2);
}
 
void loop() {
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);
// Wait some short time before taking the next measurement
delay(25);
}

 実行します。30cm以下は測定できないようです。作業に使っている液晶画面に向けたときの様子です。

 廊下側へセンサを向けました。

前へ

SpresenseでLチカから始める (20) Wireライブラリ 距離RFD77402

次へ

レベル変換 (1) UART その1 初期電圧を調べる