ちょっとコレ使って作りたいものがあるのですが、
どんなものかを試しておいてみます。
なお、モーターを動かすのにL293Dっていうモータードライブシールドを使います。
今日の素材
- HC-SR04 超音波距離センサーモジュール For Arduino
- GAOHOU L293D モータードライバー 拡張ボード 基板モーター モータ制御シールドArduino用
- AO-1001:FA-130ノーマルモーター
環境
- macOS High Sierra 10.13.6
- arduino IDE 1.8.9
コード
こちらを参考にしました
https://www.makerguides.com/hc-sr04-arduino-tutorial/
まずはシンプルに動かすだけのコード
/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */
// Define Trig and Echo pin:
# define trigPin 2
# define echoPin 3
// Define variables:
long duration;
int distance;
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance= duration*0.034/2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}
5VとGND同士を繋げ、GPIO2とTrig、GPIO03とEchoを繋げます。
すんません。ぜんぜんわからないけど、センサーと障害物(指)の距離がシリアルモニタに出てます。
しかしあっさり動いた。IoT系のがあっさり動くのはけっこううれしい。
距離とモーターの連動
構成はこういう感じ。
arduinoもどきの上にL293Dがガバッと乗っています。
HC-SR04とのPIN接続は前回と同様ですが、外部電源が追加されてますね。
そこの5VとGNDがHC-SR04やL293DのEXP_PWRに繋がってます。
あとモーターがL293DのM4のあたりから繋がってる。
/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */
// HC-SR04のPIN
# define trigPin 2
# define echoPin 3
// Adafruit Motor Shild Libralyより
# include <AFMotor.h>
// DCモーターのM4を指定
AF_DCMotor motor4(4);
// Define variables:
long duration;
int distance;
bool bolMotor4;
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
bolMotor4 = false;
// turn on motor
motor4.setSpeed(200);
motor4.run(RELEASE);
}
void loop() {
uint8_t i;
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance= duration*0.034/2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
//10cm以上ならモーターは動かす、以下なら止める
if (distance >= 10){
bolMotor4 = true;
}else{
bolMotor4 = false;
}
if (bolMotor4){
motor4.run(FORWARD);
for (i=0; i<255; i++) {
motor4.setSpeed(i);
delay(10);
}
}else{
motor4.run(RELEASE);
}
delay(50);
}
動きました!
単純に距離が10cm未満ならモーターを止めて、以上なら回してます。
比較的簡単にできた。
L293D使うとモーターとの併用も簡単だしよさそう。これにBLEモジュール繋げる予定だけどピンにも余裕はあるな。
ただ、超音波センサーの精度的なものはちょっと気になりますかね。
黒いぬいぐるみがうまく検知できなかったりしたし、検知できる対象に何か条件がありそう。