0
0

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.

赤外線距離センサ使ってみた

Posted at

概要

赤外線距離センサを購入したので試してみました。
物体からの距離を計測するセンサーです。

部品

距離センサー:赤外線距離センサモジュール
43円
image.png

表示機:TM1637が組み込まれた7セグLED
73円
image.png

コンピュータ:Arduino UNO互換機
699円
img.png

配線

スクリーンショット 2020-03-26 7.52.05.png

ソースコード

アナログ信号を受け取り、10回の平均を表示するようにしています。



# include <Arduino.h>
# include <Wire.h>
# include <TM1637Display.h>
# define SERIAL_BAUD 115200

# define CLK 2
# define DIO 3
TM1637Display display(CLK, DIO);
# define IR_LEN 10
int IRs[IR_LEN] = {0,0,0,0,0,0,0,0,0,0};
int IRs_idx = 0;

void setup() {
  Serial.begin(SERIAL_BAUD);
  while(!Serial) {}

  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
  display.setBrightness(0x0f);
  display.setSegments(data);
  delay(1000);
}

void loop() {
  int ave = 0;
  int IR;
  IRs_idx++;
  if(! (IR_LEN > IRs_idx) )
    IRs_idx = 0;
  IRs[IRs_idx] = analogRead(A0);
  for(int i=0 ; i<IR_LEN;i++){
    ave+=IRs[i];
  }
  IR = ave/IR_LEN;

  display.showNumberDec(IR, false); 
  Serial.print("IR: ");
  Serial.print(IR);
  delay(100);
}

github

少し感想

初期設定だと5cmから6cm程度の範囲で検知できます。
その範囲以外だとある100025などの最大/最小値になってしまうため、検知できません。
上部にある可変抵抗をまわすことにより調整が可能です。

動作確認

gif動画で上げても分かりにくかったので、Youtubeにて確認いただけると幸いです。
IMAGE ALT TEXT HERE

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?