3人と1匹の日常とモノづくりブログ

興味のあるものをとりあえず動かしてみた、実装してみたシリーズをガンガン上げていきたいと思います。あくまでも自身の備忘録としてですが、誰かの助けになったらうれしいです。

【低価格マイコン】【実装】Arduino Nanoで心拍センサ (MAX30102)を動かす

f:id:bamboomush:20210305190650p:plain

 Arduino Nanoを用いて心拍センサを実装したので、そのやり方について紹介したいと思います。

この記事を読むことで、安価なマイコンであるArduino Nanoで心拍センサが実装できるようになります。

生体センサはロボット開発にはあまり有用性はないかもしれませんが、以前実装した環境センサと組み合わせることでIoTとしてはかなり重要な役割を果たすと思います。また、体調管理を目的としたデバイスには重宝され、現にApple Watchなどの高価なデバイスにも使用されています。

 

Youtube

youtu.be

 

ESP32版はこちら

 

◆Arduino関係の実装記事◆ 
  • DCモータ実装

www.takeshi-1222.com

  •  サーボモータ実装

www.takeshi-1222.com

  • OLED実装

www.takeshi-1222.com

  • 超音波センサ実装

www.takeshi-1222.com

  • LCDディスプレイ実装

www.takeshi-1222.com

  • 温湿度センサ (低コスト)

www.takeshi-1222.com

  • 温湿度センサ (ハイスペック)

www.takeshi-1222.com

  • 温湿度センサ (複数)

www.takeshi-1222.com

  • CO2センサ 

www.takeshi-1222.com 

 

  • ほこりセンサ

www.takeshi-1222.com

  • 皮膚電位センサ

www.takeshi-1222.com

Arduino Nano, MAX30102の簡単な説明

Arduino Nano

Arduino Nanoは超安価で購入ができるマイコンで、電気やプログラミングの深い知識を持っていない電子工作初心者でも扱いやすい、オープンソースマイコンです。

Arduinoと比較すると、性能は劣るものの、安価で広い用途で使用可能なので初心者に適しています。

ちなみに、上記が純正品ですが、互換品である以下も性能面では変わらないので強いこだわりがない限りは、互換品の方がおすすめです。

心拍センサ (MAX30102)

心拍センサは赤と緑のIRを使って脈拍や血中酸素濃度などを推定することができるセンサです。生体情報のセンシングはかなり難しく、このセンサ単体で出来ることは限られています。なので、これに加えて環境センサやほかの生体センサを使うことを強くお勧めします。

実験構成

 今回の実験に使用する部品は以下のものです。
  • Arduino Nano
  • 心拍センサ (MAX30102)
  • ジャンパ線多数

Arduino Nano、心拍センサは上記のものを使用しています。

 

ジャンパ線は何を使用してもいいですが、一応リンクを張っておきます。

実験

配線

まず配線は以下のようにします。

f:id:bamboomush:20210305192730p:plain

配線ができると以下のような画像のようになります。

f:id:bamboomush:20210305192752j:plain

実行コード

ここまで出来たら以下のコードを実行することで動作確認ができます。

/*
  Optical Heart Rate Detection (PBA Algorithm) using the MAX30105 Breakout
  By: Nathan Seidle @ SparkFun Electronics
  Date: October 2nd, 2016
  https://github.com/sparkfun/MAX30105_Breakout

  This is a demo to show the reading of heart rate or beats per minute (BPM) using
  a Penpheral Beat Amplitude (PBA) algorithm.

  It is best to attach the sensor to your finger using a rubber band or other tightening
  device. Humans are generally bad at applying constant pressure to a thing. When you
  press your finger against the sensor it varies enough to cause the blood in your
  finger to flow differently which causes the sensor readings to go wonky.

  Hardware Connections (Breakoutboard to Arduino):
  -5V = 5V (3.3V is allowed)
  -GND = GND
  -SDA = A4 (or SDA)
  -SCL = A5 (or SCL)
  -INT = Not connected

  The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
  but it will also run at 3.3V.
*/

#include <Wire.h>
#include "MAX30105.h"

#include "heartRate.h"

MAX30105 particleSensor;

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred

float beatsPerMinute;
int beatAvg;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing...");

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void loop()
{
  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20)
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.print(beatAvg);

  if (irValue < 50000)
    Serial.print(" No finger?");

  Serial.println();
}

今回は脈拍センサの数値とその平均値をシリアルモニタで確認できるようにしています。


これらの値とほかのセンサを組み合わせることで、いろんなIoT機器を製作できるようになります。

まとめ

今回は、Arduino Nanoで脈拍センサを動かすために必要なものの紹介と、サンプルプログラムの紹介をしました。