Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 14: Pulse Oximeter and Heart Rate Sensor Module (MAX30102)

In this tutorial, you’ll learn to operate the MAX30102 sensor using a Raspberry Pi, streamlined through the use of the open-source MAX30102 Python driver available on GitHub. This approach makes it easier to interface with the module, allowing you to focus on understanding the basics of sensor data collection and analysis. Ideal for novices, the project provides hands-on experience with sensor implementation and Python coding on the Raspberry Pi platform.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi 5

BUY

Pulse Oximeter and Heart Rate Sensor Module (MAX30102)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_14_MAX30102_pi_bb.png

Code

from heartrate_monitor import HeartRateMonitor
import time

# Print a message indicating the sensor is starting
print('sensor starting...')

# Set the duration for which the sensor data will be read (in seconds)
duration = 30

# Initialize the HeartRateMonitor object
# Set print_raw to False to avoid printing raw data
# Set print_result to True to print the calculated results
hrm = HeartRateMonitor(print_raw=False, print_result=True)

# Start the heart rate sensor
hrm.start_sensor()

try:
    time.sleep(duration)
except KeyboardInterrupt:
    print('keyboard interrupt detected, exiting...')

# Stop the sensor after the duration has elapsed
hrm.stop_sensor()

# Print a message indicating the sensor has stopped
print('sensor stopped!')

Code Analysis

  1. Importing Modules

    • The heartrate_monitor module is used to interface with the sensor. For more information about the heartrate_monitor library, please visit doug-burrell/max30102 .

    • The time module helps in managing the duration of the sensor data collection.


    from heartrate_monitor import HeartRateMonitor
    import time
    
  2. Initializing the Heart Rate Monitor

    • A HeartRateMonitor object is created with specific print options.

    • print_raw controls whether raw sensor data is printed.

    • print_result controls the printing of processed results (heart rate and SpO2).


    hrm = HeartRateMonitor(print_raw=False, print_result=True)
    
  3. Starting the Sensor

    The start_sensor method activates the heart rate sensor.

    hrm.start_sensor()
    
  4. Running the Sensor for a Set Duration

    • The program sleeps for a specified duration, during which the sensor collects data.

    • time.sleep(duration) halts the program for the given number of seconds.


    try:
        time.sleep(duration)
    except KeyboardInterrupt:
        print('keyboard interrupt detected, exiting...')
    
  5. Stopping the Sensor

    After the duration, the stop_sensor method is called to stop data collection.

    hrm.stop_sensor()
    
  6. Finalizing the Program

    Prints a message when the sensor stops.

    print('sensor stopped!')