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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
Wiring
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
Importing Modules
The
heartrate_monitormodule is used to interface with the sensor. For more information about theheartrate_monitorlibrary, please visit doug-burrell/max30102 .The
timemodule helps in managing the duration of the sensor data collection.
from heartrate_monitor import HeartRateMonitor import time
Initializing the Heart Rate Monitor
A
HeartRateMonitorobject is created with specific print options.print_rawcontrols whether raw sensor data is printed.print_resultcontrols the printing of processed results (heart rate and SpO2).
hrm = HeartRateMonitor(print_raw=False, print_result=True)
Starting the Sensor
The
start_sensormethod activates the heart rate sensor.hrm.start_sensor()
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...')
Stopping the Sensor
After the duration, the
stop_sensormethod is called to stop data collection.hrm.stop_sensor()
Finalizing the Program
Prints a message when the sensor stops.
print('sensor stopped!')