By Alton Olson and Eliza Pratt

Description

This project detects a user’s pulse and triggers a servo motor to move in sync. When a heartbeat is detected, the servo pushes a micro switch, which in turn lights up a configuration of LEDs.

Project Images

Overview: Double transducer contains a pulse sensor with pipecleaner cover, a servo, a microswitch, and LEDs with a heart display.
Transducer reading a pulse
Intermediate interaction: a servo is positioned to push a micro switch
 

Double transducer in action

Process Images

Testing a force sensor as the secondary input. While we had originally imagined using a button, we needed a component the servo could realistically push.
Ziptying the servo to the breadboard to prevent it from pushing itself upwards.

Pulse sensor picking up too much noise and detecting false pulses.

Device adjusted successfully with accurate pulse representation.

Schematic

Code

  1. <span class="com">/*
  2. * Heartbeat Visualizer
  3. * By Alton Olson and Eliza Pratt
  4. *
  5. * Description: This program reads input data from a pulse
  6. * sensor and tracks the maxes and mins to determine heart rate.
  7. * When a heartbeat is detected, it triggers a servo to push
  8. * a switch that turns on a configuration of LEDs.
  9. *
  10. * Credit: Code for measuring heart rate was adapted from
  11. * user VE7JRO's contribution to the StackExchange Arduino forum.
  12. * View discussion: https://tinyurl.com/y3vuvgpp
  13. */</span><span class="pln">
  14. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><Servo.h></span><span class="pln">
  15. </span><span class="com">// Pin constants</span><span class="pln">
  16. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> BUTTONPIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
  17. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVOPIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln">
  18. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED1PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">4</span><span class="pun">;</span><span class="pln">
  19. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED2PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pun">;</span><span class="pln">
  20. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED3PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln">
  21. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED4PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln">
  22. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED5PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">8</span><span class="pun">;</span><span class="pln">
  23. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> PULSEPIN </span><span class="pun">=</span><span class="pln"> A1</span><span class="pun">;</span><span class="pln">
  24. </span><span class="com">// Servo position constants</span><span class="pln">
  25. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_DOWN_POSITION </span><span class="pun">=</span><span class="pln"> </span><span class="lit">155</span><span class="pun">;</span><span class="pln">
  26. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_UP_POSITION </span><span class="pun">=</span><span class="pln"> </span><span class="lit">135</span><span class="pun">;</span><span class="pln">
  27. </span><span class="com">// We have two thresholds to detect a pulse</span><span class="pln">
  28. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> RISING_THRESHOLD </span><span class="pun">=</span><span class="pln"> </span><span class="lit">520</span><span class="pun">;</span><span class="pln">
  29. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> FALLING_THRESHOLD </span><span class="pun">=</span><span class="pln"> </span><span class="lit">510</span><span class="pun">;</span><span class="pln">
  30. </span><span class="com">// Ignore reading keeps track of the state of the pulse</span><span class="pln">
  31. </span><span class="kwd">bool</span><span class="pln"> ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  32. </span><span class="com">// Time in ms of the last beat</span><span class="pln">
  33. </span><span class="kwd">long</span><span class="pln"> lastBeatTime </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  34. </span><span class="typ">Servo</span><span class="pln"> servo</span><span class="pun">;</span><span class="pln">
  35. </span><span class="kwd">void</span><span class="pln"> setup</span><span class="pun">(){</span><span class="pln">
  36. </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">9600</span><span class="pun">);</span><span class="pln">
  37. servo</span><span class="pun">.</span><span class="pln">attach</span><span class="pun">(</span><span class="pln">SERVOPIN</span><span class="pun">);</span><span class="pln">
  38. pinMode</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  39. pinMode</span><span class="pun">(</span><span class="pln">LED1PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  40. pinMode</span><span class="pun">(</span><span class="pln">LED2PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  41. pinMode</span><span class="pun">(</span><span class="pln">LED3PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  42. pinMode</span><span class="pun">(</span><span class="pln">LED4PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  43. pinMode</span><span class="pun">(</span><span class="pln">LED5PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  44. pinMode</span><span class="pun">(</span><span class="pln">PULSEPIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  45. </span><span class="pun">}</span><span class="pln">
  46. </span><span class="kwd">void</span><span class="pln"> loop</span><span class="pun">(){</span><span class="pln">
  47. </span><span class="kwd">int</span><span class="pln"> reading </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PULSEPIN</span><span class="pun">);</span><span class="pln">
  48. </span><span class="typ">Serial</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">reading</span><span class="pun">);</span><span class="pln">
  49. </span><span class="com">// Heart beat leading edge detected.</span><span class="pln">
  50. </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">reading </span><span class="pun">></span><span class="pln"> RISING_THRESHOLD </span><span class="pun">&&</span><span class="pln"> </span><span class="pun">!</span><span class="pln">ignoreReading
  51. </span><span class="pun">&&</span><span class="pln"> millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lastBeatTime </span><span class="pun">></span><span class="pln"> </span><span class="lit">500</span><span class="pun">){</span><span class="pln">
  52. </span><span class="com">// Trigger servo if <120 bpm</span><span class="pln">
  53. servo</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">SERVO_DOWN_POSITION</span><span class="pun">);</span><span class="pln">
  54. ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  55. lastBeatTime </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
  56. </span><span class="pun">}</span><span class="pln">
  57. </span><span class="com">// Heart beat trailing edge detected.</span><span class="pln">
  58. </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">reading </span><span class="pun"><</span><span class="pln"> FALLING_THRESHOLD </span><span class="pun">&&</span><span class="pln"> ignoreReading</span><span class="pun">){</span><span class="pln">
  59. servo</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">SERVO_UP_POSITION</span><span class="pun">);</span><span class="pln">
  60. ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  61. </span><span class="pun">}</span><span class="pln">
  62. </span><span class="com">// Turn on LEDs if the switch is pressed</span><span class="pln">
  63. digitalWrite</span><span class="pun">(</span><span class="pln">LED1PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  64. digitalWrite</span><span class="pun">(</span><span class="pln">LED2PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  65. digitalWrite</span><span class="pun">(</span><span class="pln">LED3PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  66. digitalWrite</span><span class="pun">(</span><span class="pln">LED4PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  67. digitalWrite</span><span class="pun">(</span><span class="pln">LED5PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  68. delay</span><span class="pun">(</span><span class="lit">150</span><span class="pun">);</span><span class="pln">
  69. </span><span class="pun">}</span>
<span class="com">/*
 * Heartbeat Visualizer
 * By Alton Olson and Eliza Pratt
 * 
 * Description: This program reads input data from a pulse
 * sensor and tracks the maxes and mins to determine heart rate. 
 * When a heartbeat is detected, it triggers a servo to push
 * a switch that turns on a configuration of LEDs. 
 * 
 * Credit: Code for measuring heart rate was adapted from 
 * user VE7JRO's contribution to the StackExchange Arduino forum.
 * View discussion: https://tinyurl.com/y3vuvgpp

 */</span><span class="pln">

</span><span class="com">#include</span><span class="pln"> </span><span class="str"><Servo.h></span><span class="pln">

</span><span class="com">// Pin constants</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> BUTTONPIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVOPIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED1PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">4</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED2PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED3PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED4PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED5PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">8</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> PULSEPIN </span><span class="pun">=</span><span class="pln"> A1</span><span class="pun">;</span><span class="pln">

</span><span class="com">// Servo position constants</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_DOWN_POSITION </span><span class="pun">=</span><span class="pln"> </span><span class="lit">155</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_UP_POSITION </span><span class="pun">=</span><span class="pln"> </span><span class="lit">135</span><span class="pun">;</span><span class="pln">

</span><span class="com">// We have two thresholds to detect a pulse</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> RISING_THRESHOLD </span><span class="pun">=</span><span class="pln"> </span><span class="lit">520</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> FALLING_THRESHOLD </span><span class="pun">=</span><span class="pln"> </span><span class="lit">510</span><span class="pun">;</span><span class="pln">
</span><span class="com">// Ignore reading keeps track of the state of the pulse</span><span class="pln">
</span><span class="kwd">bool</span><span class="pln"> ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
</span><span class="com">// Time in ms of the last beat</span><span class="pln">
</span><span class="kwd">long</span><span class="pln"> lastBeatTime </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

</span><span class="typ">Servo</span><span class="pln"> servo</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> setup</span><span class="pun">(){</span><span class="pln">
  </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">9600</span><span class="pun">);</span><span class="pln">
  servo</span><span class="pun">.</span><span class="pln">attach</span><span class="pun">(</span><span class="pln">SERVOPIN</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED1PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED2PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED3PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED4PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED5PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">PULSEPIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> loop</span><span class="pun">(){</span><span class="pln">
  </span><span class="kwd">int</span><span class="pln"> reading </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PULSEPIN</span><span class="pun">);</span><span class="pln">

  </span><span class="typ">Serial</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">reading</span><span class="pun">);</span><span class="pln">
  
  </span><span class="com">// Heart beat leading edge detected.</span><span class="pln">
  </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">reading </span><span class="pun">></span><span class="pln"> RISING_THRESHOLD </span><span class="pun">&&</span><span class="pln"> </span><span class="pun">!</span><span class="pln">ignoreReading
    </span><span class="pun">&&</span><span class="pln"> millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lastBeatTime </span><span class="pun">></span><span class="pln"> </span><span class="lit">500</span><span class="pun">){</span><span class="pln">
    </span><span class="com">// Trigger servo if <120 bpm</span><span class="pln">
    servo</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">SERVO_DOWN_POSITION</span><span class="pun">);</span><span class="pln">
    ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
    lastBeatTime </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// Heart beat trailing edge detected.</span><span class="pln">
  </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">reading </span><span class="pun"><</span><span class="pln"> FALLING_THRESHOLD </span><span class="pun">&&</span><span class="pln"> ignoreReading</span><span class="pun">){</span><span class="pln">
    servo</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">SERVO_UP_POSITION</span><span class="pun">);</span><span class="pln">
    ignoreReading </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// Turn on LEDs if the switch is pressed</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">LED1PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">LED2PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">LED3PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">LED4PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">LED5PIN</span><span class="pun">,</span><span class="pln"> digitalRead</span><span class="pun">(</span><span class="pln">BUTTONPIN</span><span class="pun">));</span><span class="pln">

  delay</span><span class="pun">(</span><span class="lit">150</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span>

 

Discussion

Decision Making: Throughout this project, one of our biggest realizations was that we can’t rely on physical components to behave exactly as we expect. While we knew we wanted a servo to be our initial output, finding the proper device to respond to it proved to be harder than we expected. We ended up testing buttons, force sensors, and a variety of switches in order to find the right fit. Large buttons presented a problem as they were too springy and would require a stronger servo to actuate. FSRs solved the recoil issue, but we weren’t able to get a solid reading from the small servo arm. We ended up opting for the micro switch as it required very little force to actuate but provided a reliable binary input.

One design decision we had to make early on was whether to have the transduced signal be continuous or discrete. Originally we were going to transduce the user’s heartrate, having the servo move to different positions based on the frequency of the user’s pulse. It was difficult to get a reliable heartrate reading, though, and we thought that reading individual pulses would be a simpler problem and easier to tune.

Challenges: Getting reliable input from the pulse sensor was by far our biggest challenge. The raw data was noisy and unreliable, and it was difficult to pinpoint the source of the noise. We discovered that where the sensor is on your finger, how hard you press on the sensor, and the amount of oil and sweat on your skin all affect the data. Our first algorithm used a simple threshold, where we’d count a heartbeat if the pulse sensor data crossed from below to above the threshold. The noise in the signal generated lots of false positives. So we added a second “falling” threshold, below the first, such that the signal had to drop below the falling threshold before the rising threshold would count again. This helped isolate only the bigger spikes in the waveform and filter out some of the noise. Lastly, to try and throw out more false positives, we added a max heartrate, so that the algorithm would ignore pulses that were within a certain time of other pulses.

Three small fixes we could have made:

  1. A better filtering algorithm. We still had issues with noise fooling our algorithm, and maybe an additional filter could have helped. Perhaps a low pass filter could eliminate some of the higher frequency noise while keeping the pulses intact.
  2. Adjustable thresholds. We hardcoded the rising and falling thresholds, but allowing the user to adjust these with a potentiometer could allow the device to perform better under different conditions.
  3. Taking pulse readings from somewhere else. The documentation for the pulse sensor recommends to try using an ear clip attachment to take readings from a user’s ear. We didn’t have the ear clip attachment, but this could have provided better data than the finger.