[Python] Calculating SpO2 with MAX30101 Sensor in Python on Raspberry Pi 4

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have recently purchased a MAX30101 breakout board / sensor and have been using it with a Raspberry Pi 4 and Python. I have been using the sparkfun-qwiic-max3010x library provided by Sparkfun to communicate with the sensor and collect data. The GitHub page for the Python sparkfun-qwiic-max3010x library includes an example for calculating heart rate but not SpO2. The Arduino library includes an example for calculating heart rate and SpO2.

    I was wondering if there is an algorithm available or if it is possible to calculate SpO2 accurately in Python on the Raspberry Pi 4.

    I have seen an algorithm in the user guide of the MAX30101 sensor but have not been able to accurately get an SpO2 reading with it in Python in the following code. The SpO2 value sometimes goes in the negatives and will give extremely different consecutive values.

    import time
    import numpy as np
    import qwiic_max3010x

    # Initialize the MAX30101 sensor
    sensor = qwiic_max3010x.QwiicMax3010x()

    sensor.begin()
    sensor.setup()

    sensor.setPulseAmplitudeGreen(0)

    # Function to read raw values from the sensor
    def read_raw_values(num_samples=100):
    red_values = []
    ir_values = []

    for _ in range(num_samples):
    red = sensor.getRed()
    ir = sensor.getIR()
    red_values.append(red)
    ir_values.append(ir)
    time.sleep(0.01) # Adjust the delay as needed

    sensor.shutDown()

    return np.array(red_values), np.array(ir_values)


    # Function to calculate AC and DC components
    def calculate_ac_dc(raw_values):
    dc = np.mean(raw_values) # DC component
    ac = raw_values - dc # AC component
    return ac, dc


    def calculate_spo2(red_ac, red_dc, ir_ac, ir_dc):
    r = (np.mean(red_ac) / red_dc) / (np.mean(ir_ac) / ir_dc)
    spo2 = 104 - (17 * r)
    return spo2


    # Main loop
    try:
    while True:
    # Read raw values
    red_raw, ir_raw = read_raw_values(num_samples=100)

    # Calculate AC and DC components
    red_ac, red_dc = calculate_ac_dc(red_raw)
    ir_ac, ir_dc = calculate_ac_dc(ir_raw)

    spo2 = calculate_spo2(red_ac, red_dc, ir_ac, ir_dc)

    print("Spo2: ", spo2)

    # Wait for a bit before the next reading
    time.sleep(1) # Adjust the sleep time as needed

    except KeyboardInterrupt:
    print("Program stopped.")

    Continue reading...

Compartilhe esta Página