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 3 years have passed since last update.

DigisparkでAnalogRead~GP2Y0A21YKを使う~

Last updated at Posted at 2020-11-14

前回に引き続きDigisparkを使います。
DigiSparkの良さは、8ピンでUSBが搭載されており、AnalogReadやI2Cが使える点です。

AnalogReadのメリット

RaspberryPiやJetsonにはDigitalWriteやDigitalReadはありますが
AnalogReadがありません。
AnalogReadは温度センサーや、電力測定、測距モジュール等に使われていますが使用できないのです。
DigiSparkを使う事でこの欠点を補う事ができます。

複数のマイコンを使う

なお、Raspberry piの記事の多くはADC用ICを使うようです。
これは、別マイコンにプログラムを書くよりも、Raspberry piでコントロールする方が良いとの考えだと思います。
逆に複数のマイコンに個別にプログラムすることで、Raspberry pi等の処理を簡単にすることができます。

GP2Y0A21YKを使う

今回は、シャープの測距モジュールGP2Y0A21YKを使用します。

引用プログラム

DigisparkとLM61で温度を測るを引用します。
https://atotto.hatenadiary.jp/entry/digispark_lm61_usb
GP2Y0A21YKの計算式は測距モジュール(GP2Y0A21)を使ってみたを引用しました。
http://taroooyan.hatenablog.com/entry/2015/08/16/000000

# include <DigiUSB.h>

void setup()
{
  pinMode(2, INPUT); // P2 = anolog input 1
  pinMode(1, OUTPUT);
  DigiUSB.begin();
}

void loop()
{
  digitalWrite(1, HIGH);

  int step;
  float dis;
  step = analogRead(1);
  dis = 6787.0/((float)step-3.0)-4.0;
  DigiUSB.print("{\"Distance\":");
  DigiUSB.print(dis);
  DigiUSB.println("}");
    
  digitalWrite(1, LOW);
  DigiUSB.delay(1000);
}

ファームウェアを書き込むと、デバイスマネージャーからはDigiUSBと認識されます。
10-2-1.jpg

配線

GP2Y0A21YKの赤線を5V、黒線をGND、黄線をP2に接続します。
DSC_0554 (2).JPG

動作確認

DigiSparkの出力は、PCではmonitorというソフトウェアなどで見ることができます。
https://github.com/digistump/DigisparkExamplePrograms
10-3.jpg
以上で、DigiSparkを使ってアナログリードをすることが可能になりました。
後は、RaspberryPiやPCでソフトウェアを作ることで、近接センサーの値を使用する事ができます。

GP2Y0A21YKの性能が悪い

さて、ここで大失敗がおきました。
それは、GP2Y0A21YKの性能が想定以上に低かったのです。
購入が中国だったせいか、10~25㎝のレンジでしか使用できず、実使用で使えるレベルではないために、使うか悩んでしまっていたりします・・・

次は、GP2Y0A21YKを2個使えるようにします。
DigisparkでAnalogRead~GP2Y0A21YKを使う2~P5を使う
https://qiita.com/usashirou/items/e023d536719bcd883448

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?