Back to overview

Detect your pulse using Computer Vision and flash an LED at the same rate

• 1
Install software for PC application
1. Install Python 2.7 including pip package manager
2. Use pip to install OpenCV, Numpy, and serial with:
3. `pip install opencv-python matplotlib serial`
• 2
Install tools for microcontroller

This step will change depending on the microcontroller that you use, but for the Digispark, follow the instructions here: (BTW, the Tomu looks like a modern replacement to the Digispark)

http://digistump.com/wiki/digispark/tutorials/connecting

After installing the board in the Arduino IDE, a warning will be displayed in the Arduino console with the location of the drivers. Move to this directory and install the drivers manually. Use the drivers linked in this forum post if you're using Windows 10.

Copy this code into a new Arduino project and flash it to the Digispark (note that you hit upload and then insert the Digispark into the USB port when prompted):

```#include <DigiCDC.h>

// onboard LED pin
//#define LED 1

// RGB Shield Red LED pin
#define LED 0

void setup()
{
// begin serial
SerialUSB.begin();

// set LED as output
pinMode(LED, OUTPUT);
}

void loop()
{
if (SerialUSB.available())
{
// read the pulse (in beats per minute) sent over serial

// convert pulse to beats per second
float pulse_BPS = pulse_BPM/60;

// calculate the period of one beat
float period = 1/pulse_BPS;

// calculate floating point value of the time where the light should be on
// assuming the systole lasts 3/8 of a pulse period
float time_on_f = 0.375 * period;

// convert that time on to an int in milliseconds
int time_on = time_on_f * 1000;

// calculate the remaining floating point time in the period - "the diastole"
float time_off_f = period - time_on_f;

// convert the time off to an int in milliseconds
int time_off = time_off_f * 1000;

// set LED high
digitalWrite(LED, HIGH);

// delay for the on time
SerialUSB.delay(time_on);

// set LED low
digitalWrite(LED, LOW);

// delay for the off time
SerialUSB.delay(time_off);
}

// need to call a SerialUSB function at least every 10ms
SerialUSB.refresh();
}```

After the device is programmed, make note of the serial port that shows up for the communications. It's different than the port that was used to program it and the new port will be needed for the next step.

• 3
Start the PC application

Open a terminal at the location of the project from step 1.4. Use the following command to start in serial mode (ensure you change the COM number to the one you found in step 2.

`python get_pulse.py --serial COM5`

The PC application will start your webcam. Aim the square at your forehead and press the "s" key. Your heart rate will be determined and the LED on the microcontroller will start to blink. Congrats, you're done!