果報は寝て待て: 6月 2020

2020年6月2日火曜日

コロナに負けるな!パルスオキシメータを作ろう

コロナに負けるな!自分に何ができるかを考えよう。
ということでパルスオキシメータの制作を思いつきました。
 理由  
1 急に重症化するときがあり、その目安の一つになるらしい。  
2 どうも安い部品でできそう。  
3 Aruduino Unoに標準のライブラリがある  
4 おうちでできる。  
5 年配の家族がいるので一台あれば安心。
6  Aruduino Unoがあるのでそれを使える。もし大量生産が必要なら、ATMEGA  だけ購入して増産できる。

使用するものはAruduino Unoと16桁2行のlcdとパルスオキシメータのモジュール。

ebayで385円でポチりました。
香港からの発送で、届くのが5月末の予定。
コロナが収束して必要なくなってたら、それはそれで良い事だね。

 いつも思うのですが、返品、交換の送料は購入者の負担だと言うこと。
 一体送料はいくらかかるのでしょうか?すくなくとも385円以上は間違いないと思うのでこういう安いものを返品交換した人は実際にいるのでしょうか。
 使えなくてもあきらめのつく金額に押さえておくというのは販売者にとって大切なことかもしれませんね。


回路は以下の通りです。
このモジュールは1.8vでの出力なので出力電圧の変換が必要です。
2N7000のFETで変換します。


Aruduino Unoのコードですが、AruduinoIDEのスケッチ →ライブラリをインクルード→ライブラリを管理の画面でmax30102を検索します。

 これをインストールします。
そのライブラリのなかの SparkFun_MAX3010x_Pulse_and_Proximity_Sensor_Library→exsamples→Example8_SPO2→Exsample8_SPO".inoを開きます。

コードです。
  1. /*
  2. Optical SP02 Detection (SPK Algorithm) using the MAX30105 Breakout
  3. By: Nathan Seidle @ SparkFun Electronics
  4. Date: October 19th, 2016
  5. https://github.com/sparkfun/MAX30105_Breakout
  6.  
  7. This demo shows heart rate and SPO2 levels.
  8.  
  9. It is best to attach the sensor to your finger using a rubber band or other tightening
  10. device. Humans are generally bad at applying constant pressure to a thing. When you
  11. press your finger against the sensor it varies enough to cause the blood in your
  12. finger to flow differently which causes the sensor readings to go wonky.
  13.  
  14. This example is based on MAXREFDES117 and RD117_LILYPAD.ino from Maxim. Their example
  15. was modified to work with the SparkFun MAX30105 library and to compile under Arduino 1.6.11
  16. Please see license file for more info.
  17.  
  18. Hardware Connections (Breakoutboard to Arduino):
  19. -5V = 5V (3.3V is allowed)
  20. -GND = GND
  21. -SDA = A4 (or SDA)
  22. -SCL = A5 (or SCL)
  23. -INT = Not connected
  24. The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
  25. but it will also run at 3.3V.
  26. */
  27.  
  28. #include
  29. #include "MAX30105.h"
  30. #include "spo2_algorithm.h"
  31. MAX30105 particleSensor;
  32. #define MAX_BRIGHTNESS 255
  33. #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  34. //Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
  35. //To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
  36. uint16_t irBuffer[100]; //infrared LED sensor data
  37. uint16_t redBuffer[100]; //red LED sensor data
  38. #else
  39. uint32_t irBuffer[100]; //infrared LED sensor data
  40. uint32_t redBuffer[100]; //red LED sensor data
  41. #endif
  42. int32_t bufferLength; //data length
  43. int32_t spo2; //SPO2 value
  44. int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
  45. int32_t heartRate; //heart rate value
  46. int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
  47. byte pulseLED = 11; //Must be on PWM pin
  48. byte readLED = 13; //Blinks with each data read
  49. void setup()
  50. {
  51. Serial.begin(115200); // initialize serial communication at 115200 bits per second:
  52. pinMode(pulseLED, OUTPUT);
  53. pinMode(readLED, OUTPUT);
  54. // Initialize sensor
  55. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  56. {
  57. Serial.println(F("MAX30105 was not found. Please check wiring/power."));
  58. while (1);
  59. }
  60. Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  61. while (Serial.available() == 0) ; //wait until user presses a key
  62. Serial.read();
  63. byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  64. byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  65. byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  66. byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  67. int pulseWidth = 411; //Options: 69, 118, 215, 411
  68. int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  69. particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  70. }
  71. void loop()
  72. {
  73. bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
  74. //read the first 100 samples, and determine the signal range
  75. for (byte i = 0 ; i < bufferLength ; i++)
  76. {
  77. while (particleSensor.available() == false) //do we have new data?
  78. particleSensor.check(); //Check the sensor for new data
  79. redBuffer[i] = particleSensor.getRed();
  80. irBuffer[i] = particleSensor.getIR();
  81. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  82. Serial.print(F("red="));
  83. Serial.print(redBuffer[i], DEC);
  84. Serial.print(F(", ir="));
  85. Serial.println(irBuffer[i], DEC);
  86. }
  87. //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  88. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  89. //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
  90. while (1)
  91. {
  92. //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
  93. for (byte i = 25; i < 100; i++)
  94. {
  95. redBuffer[i - 25] = redBuffer[i];
  96. irBuffer[i - 25] = irBuffer[i];
  97. }
  98. //take 25 sets of samples before calculating the heart rate.
  99. for (byte i = 75; i < 100; i++)
  100. {
  101. while (particleSensor.available() == false) //do we have new data?
  102. particleSensor.check(); //Check the sensor for new data
  103. digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
  104. redBuffer[i] = particleSensor.getRed();
  105. irBuffer[i] = particleSensor.getIR();
  106. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  107. //send samples and calculation result to terminal program through UART
  108. Serial.print(F("red="));
  109. Serial.print(redBuffer[i], DEC);
  110. Serial.print(F(", ir="));
  111. Serial.print(irBuffer[i], DEC);
  112. Serial.print(F(", HR="));
  113. Serial.print(heartRate, DEC);
  114. Serial.print(F(", HRvalid="));
  115. Serial.print(validHeartRate, DEC);
  116. Serial.print(F(", SPO2="));
  117. Serial.print(spo2, DEC);
  118. Serial.print(F(", SPO2Valid="));
  119. Serial.println(validSPO2, DEC);
  120. }
  121. //After gathering 25 new samples recalculate HR and SP02
  122. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  123. }
  124. }

「メモリが不足しています」とのメッセージが出ます。



4月30日にebayで注文したのが6月1日にやっと到着しました。
製作意欲は20%くらいに低下しました。
それでも動作確認してますと、うまく動きません。
どうやら I2Cのレベル変換がうまくいってないようです。

試しに次の回路でスイッチをオンオフして出力側がオンオフするのか確認してみました。


そうすると入力側の電圧が2.5vを下回ると出力側でオンオフしないことがわかりました。
つまり最初の回路では、5vと3.3vの変換はできても5vと1.8vの変換はできないようです。
専用のレベル変換ICではなく、ありあわせの部品でやりたいのでしばらく検討します。