What The Tech

Finger on the Pulse

Build your own Heartbeat Sensor

Johann Wyss

Issue 36, July 2020

This article includes additional downloadable resources.
Please log in to access.

Log in

We get hands-on with a heartbeat sensor module to see how effective they are.

In this month’s What The Tech, we explore and test a Heartbeat sensor module designed for microcontrollers.

Measuring heart rates and related vitals used to be the domain of doctors, nurses and fitness professionals, by connecting up expensive electrocardiograms (EKG) or bulky chest wearables. Nowadays, we can get some vital fitness stats from a smartwatch, fitness wearable, and more recently, from affordable sensors that makers can connect to their favourite microcontroller.

Shopping List

THE HEARTBEAT SENSOR

There are many types of heart rate sensors now available to makers, including some from Jaycar, Altroncs and Core Electronics. In this article, we will test the XC3740 Heartbeat Sensor Module for Arduino from Jaycar, which sells for $9.95.

This sensor measures just 20mm x 15mm and communicates with your microcontroller using standard I2C communications. The Jaycar website describes features that include ambient light suppression and low-noise circuitry for optimum performance, with power requirements from 1.8V to 5V with optional 1.8V or 3.3V I2C voltage switching onboard.

It is interesting that this module is described as a heartbeat sensor, rather than what it actually is: a pulse oximetry sensor which can display your pulse rate and the oxygen concentration of your blood.

This diagram is the standard form of Electrocardiogram showing a human’s heartbeat.

This diagram illustrates the standard form for a SpO2 / pulse oximetry reading of a human’s pulse.

Whilst it’s true you can detect the heartbeat rate in beats per minute (BPM) using such a sensor, we were pleasantly surprised that this little sensor can do so much more than detect a pulse. This sensor can actually tell you the concentration of oxygen in your blood using a simple but very cool trick with light.

Before the development of pulse oximetry technology in the 1980s, physicians would need to take samples of a patient’s blood, which would be sent to a pathology lab for a test known as arterial blood gas analysis. This process would take significant time and resources and could delay the diagnosis for patients.

This prompted engineers in a Tokyo-based biomedical engineering company to begin researching a portable means to provide near-realtime feedback on a patient’s pulse and oxygen saturation. From this research, the lab designed the pulse oximetry technology widely used in all aspects of healthcare today. This same technology has made its way into consumer electronics such as smartwatches, fitness trackers, and sensors such as these.

HOW IT WORKS

The theory behind this technological feat is actually quite simple. Hemoglobin (Hgb) is a protein in our red blood cells that carries oxygen (O2) to the important areas of our body such as the organs and tissues. It is also responsible for transporting carbon dioxide (CO2) from said organs and tissues to the lungs where it can be expelled from the body via respiration. Oxygenated and Deoxygenated hemoglobin absorbs red and infrared (IR) light differently as shown here.

Thus, by shining a red light with a wavelength between 650nm and 670nm as well as an IR light source with a wavelength between 865nm and 905nm into the human body’s extremities such as fingers, toes or even the ear lobes and measuring the absorbed light with a photodetector, we can calculate the concentration of oxygenated hemoglobin and Deoxyhemoglobin.

During a pulsatile flow of blood, the light will travel through the soft tissues, venous or deoxygenated blood as well as the highly oxygenated arterial blood, whereas, during the non-pulsatile flow there is little to no arterial blood, reducing the amount of light absorbed. We can then calculate the blood oxygen saturation using the formula:

HgbO2 / HgbO2 + Hgb = SpO2

Where:

SPO22 = Blood oxygen saturation

HgbO22 = Concentration of Oxyhemoglobin

Hgb = Concentration of Hemoglobin

The good news for us makers, is that the heartbeat sensor module takes care of all of this for us. The module utilises the MAX30102 IC, which has been designed as a complete all-in-one solution for mobile phones and wearable devices such as fitness trackers. The IC is equipped with ambient light cancellation which helps to reduce spurious readings by adjusting for the levels of ambient light. It also uses a temperature sensor to calibrate itself. Likewise, the LEDs require a precise constant current driver, as any deviation in the supply to the LEDs will result in a change in the brightness, and thus, will create erroneous readings. Therefore, the IC also implements inbuilt LED driver circuits.

The MAX30102 shown here contains both the IR and red LEDs, the photo detector as well as the required driver circuits.

The MAX30102 all in one integrated circuit contains the red and IR LEDs, along with the photosensor and processor. The sensor does all of the heavy lifting, exporting the result via an I²C 2 wire protocol.

For us makers, all we need to do is receive the I²C data from the sensor by using a microcontroller and use an algorithm to process the raw data into a readable format. Lucky for us, the team over at SparkFun Electronics has already done that for us and has a beautiful Arduino library written for the IC and includes excellent example code.

GETTING IT RUNNING

To get started with the Jaycar sensor, we first download the product datasheet/manual from the Jaycar website. The manual stated that they used the SparkFun MAX3010x library. After a quick search on the SparkFun website, we found a hook-up guide for a similar sensor based on the same IC. This included various sample codes.

At the bottom of the hook-up guide, we found a link to the GitHub page containing the Arduino Library named SparkFun_Bio_Sensor_Hub_Library. You can download the library for this project by following this link: https://github.com/sparkfun/SparkFun_Bio_Sensor_Hub_Library

Practical Example 1:

Serial Output

Parts Required:JaycarAltronicsCore Electronics
1 x Arduino Nano or Compatible BoardXC4414Z6372A000005
1 x Heartbeat SensorXC3740--

Parts Required:

* A breadboard and prototyping hardware also required

Our first hands-on test was to confirm that the Jaycar sensor was fully compatible with the Sparkfun library and could display results on the Arduino IDE serial monitor.

Given that the sensor uses the same IC to SparkFun’s, we didn’t expect there to be any compatibility issues, but it’s always better to check first to save you time and effort troubleshooting later if things don’t work.

We were curious to see how the heartbeat plotter worked, so for this test, we chose the ‘4 heartbeat plotter’ example code from the library.

SENSOR HEADER OPTIONS

The sensor module comes supplied with two different styles of pin headers, providing an option straight and 90° headers depending on your application. For our example. we used the straight headers so we could easily insert the sensor into the breadboard. If you’re wanting to make it portable, we recommend you install the 90° headers as this will allow you to have the joining wires leave the sensor at a more practical angle. Removing the headers can be a bit of a pain too, so choose your option carefully.

WIRING GUIDE

To replicate our example, all you need is an Arduino Nano or equivalent, the heartbeat sensor, a breadboard and jumper wires. Follow the Fritzing diagram or the following guide to wire up your prototype.

We only need to use the lower set of pins and do not use the top GND, RD, IRD or INT.

THE RESULTS

By uploading SparkFun’s example code to the Arduino Nano and using the serial plotter in the Arduino IDE, we can see a graph of the detected heartbeat.

In our case, the example is only showing the results from the IR sensor and not a combined IR and RED, and thus, it isn’t showing the entire combined pulse oximetry reading.

Usually, a pulse oximeter would use both LEDs to improve the sensitivity. Oxyhemoglobin absorbs infrared light and allows red light to pass through more freely while Deoxyhemoglobin absorbs red light and allows infrared light to pass through more freely. Thus, with this example, we are only seeing the level of absorption of the IR light. As the heart pumps we can see the surge of oxyhemoglobin absorbing more of the IR LED leaving less being reflected back to the photodetector.

This program only uses the IR LED as its only used to count pulses / heartbeats.

Considering that the sensor communicated successfully with our Nano and SparkFun library, we decided to move onto another experiment where we could display results on an LCD display.

Practical Example 2:

Realtime Display using a Nokia 5110

Parts Required:JaycarAltronicsCore Electronics
1 x Arduino Nano or Compatible BoardXC4414Z6372A000005
1 x Heartbeat SensorXC3740--
1 x 330Ω Resistor*RR0560R7040PRT-14490
5 x 4.7kΩ Resistor*RR0588R7054COM-10969 **
5 x 10kΩ Resistors*RR0596R7058PRT-14491
1 x Nokia 5110 LCDXC4616Z6348RTL-10773

Parts Required:

* Quantity required, may only be sold in packs. A breadboard and prototyping hardware also required.

Example 1 was great to show results in a computer screen, but we wanted something that didn’t need to be tethered to the computer. For our second experiment, we chose to use a common and inexpensive Nokia 5110 LCD. This screen can be found at most electronics component retailers aimed at a hobbyist market.

For this example we are using the Nokia_5110 library written by Hossein Baghayi, which you can download here: https://github.com/baghayi/Nokia_5110

This is an awesome basic library that mimics the Arduino serial print functions to write strings to the Nokia 5110 LCD.

Note: In our previous projects using this Nokia LCD, such as the USB soldering iron from issue 16, we used the graphing library from Rinky Dink Electronics. The Rinky Dink library isn’t as simple compared to the library from Hossein, but it is many times more powerful and can enable you to use graphics and graphs. If you’re wanting to develop this project, we recommend you take a look at that Rinky Dink library.

The circuit is very similar to our first example, but we are now attaching the Nokia 5110 LCD.

VOTAGE DIVIDER

The Nokia 5110 is controlled via the Phillips PCD8544 LCD controller, which is a 3.3V driver and should be powered by the 3.3V regulator on the Arduino Nano.

However, it should also ideally have a logic level shifter on the logic lines. For this project, we will negate the level shifter and just use some cheap resistors to make a voltage divider.

To do this, we can use the voltage divider formula:

Vout = (Vs x R1) / (R1 + R2)

We want to rearrange the formula to make R1 the subject, so it becomes:

R1 = ((R2 x Vs) / Vout) - R2

Where:

R2 is 10kΩ (pick a high value to reduce current wastage)

Vs = The supply voltage or 5V

Vout = the desired 3.3V

Which becomes:

R1 = ((10k x 5) / 3.3) - 10k == R1 = 5.15KΩ

5.15kΩ isn’t a standard E12 value so we can just pick the closest which is 5.6kΩ. We didn’t have this in our SparkFun resistor kit, but it did have 4.7kΩ that would give an output of 3.4V, which will be fine for this situation.

We chose to use 10kΩ and 4.7kΩ resistors to create our voltage divider/makeshift logic level converter.

WIRING THE CIRCUIT

Wire the sensor and LCD to your Nano:

To connect the LCD pins to the Arduino Nano via a voltage divider, consult this schematic.

With the circuit wired up, it’s now time to upload some code. We used example 5 from the SparkFun library which reads and displays the pulse rate on the serial monitor. We have modified the code that you can find on our website so that it will display the data on the LCD, as well as the serial monitor.

In this configuration the device was capable of reading and displaying the pulse rate on the LCD however, due to the breadboard construction it was very prone to erroneous readings.

TESTING

The sensor is very sensitive to movement, so to avoid erroneous readings, ensure to use the included length of ribbon and spring-loaded toggle to firmly hold your finger or toe securely to the sensor.

If you don’t have the sensor properly secured, the readings will be sporadic and nonsensical, which is a very common issue with these sensors and most high impedance sensors.

This diagram shows some common erroneous pulse oximeter readings that you can expect when viewing the SpO2 data in graphical/plotting form.

POWER OBSERVATIONS

We tested the current demands of the circuit and module separately using our Powertech lab bench supply, Uni-T UT804 bench multimeter, and Digitech DMM. The entire circuit, including the Arduino Nano, Nokia 5110 LCD, and the heartbeat monitor consumed 31mA when supplied with 4.96V.

We measured the sensor by itself and found that the module would draw 4.69mA with nothing touching the sensor. When supplied with 5.13V, this changed slightly to 5.81mA when the sensor had a finger on the sensor.

The sensor does not automatically enter any power saving modes and the datasheet does not suggest that any are available. This current demand appears high for a device that you see used as portable wearable devices.

It may be possible to lower the current to the two LEDs, and adjust the sample rate in software, to drop the overall power consumption, however, we have not experimented with this. We also read that it is also possible to software shut down the device to which the device will draw less than 0.7μA. If you’re planning on making a portable wearable device, we recommend you look into these functions to reduce the size of the battery you would require.

All in all, for its $10 price point, this little sensor was enjoyable to experiment with. Whilst it’s clearly not going to replace a specifically designed medical device, it could be very useful for designing your own exercise routine, fitness tracker or even just logging your SpO2 and heartbeat for fun.

Johann Wyss

Johann Wyss

Staff Technical Writer.