Skip to content

Wi-Fi Heart Rate Monitor

Project 47 • Beginner

Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Property Value
Manual Section Beginner Projects
Project Level Beginner
Board Raspberry Pi Pico 2 W
Programming Language MicroPython
Version 1.0
Date May 2026
Prepared for STEMAIDE Africa

Overview

Build a Wi-Fi heart-rate monitor page using a MAX30102 sensor and the Pico 2 W.

This project demonstrates biometric sensing, basic signal processing, and browser-based display.

The final result should show an estimated BPM value on a local web page after a finger is placed on the sensor and kept still.

!!! note "Project Story" Beginner Extension Project: This project is more advanced than the earlier beginner projects. Complete the basic projects first before attempting this one.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
MAX30102 module
MAX30102 module
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
MAX30102 VIN 3.3V Physical pin 36 Check your module label
MAX30102 GND GND Physical pin 38 —
MAX30102 SDA GPIO 8 GPIO 8 / physical pin 11 I2C data line
MAX30102 SCL GPIO 9 GPIO 9 / physical pin 12 I2C clock line

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

circuit_2

Step 2: Place the MAX30102 Module

  • Place the MAX30102 module on the breadboard.
  • Identify VIN, GND, SDA, and SCL before wiring.
  • Check the printed pin labels on your module because pin order can vary.

circuit_3

Step 3: Connect MAX30102 Power

  • Connect MAX30102 VIN to 3.3V.
  • Connect MAX30102 GND to GND.

circuit_4

Step 4: Connect MAX30102 I2C Pins

  • Connect MAX30102 SDA to GPIO 8.
  • Connect MAX30102 SCL to GPIO 9.
  • These are the I2C pins used by the code.

circuit_5


Wiring Check

Pico 2W is placed correctly across the breadboard center gap
MAX30102 VIN connects to 3.3V
MAX30102 GND connects to GND
MAX30102 SDA connects to GPIO 8
MAX30102 SCL connects to GPIO 9
No loose jumper wires

!!! note "Beginner Note" This project is for learning only. It should not be used for medical diagnosis or health decisions.


Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

I2C Scanner Test

Check that the Pico can see the MAX30102 sensor on the I2C bus.

from machine import SoftI2C, Pin

i2c = SoftI2C(sda=Pin(8), scl=Pin(9), freq=400000)
print([hex(addr) for addr in i2c.scan()])

Expected test result: You should usually see the MAX30102 address 0x57.

Raw Pulse Signal Test

Check that the sensor returns changing raw values before BPM estimation.

from machine import SoftI2C, Pin
from max30102 import MAX30102, MAX30105_PULSE_AMP_MEDIUM
import time

i2c = SoftI2C(sda=Pin(8), scl=Pin(9), freq=400000)
sensor = MAX30102(i2c=i2c)
sensor.setup_sensor()
sensor.set_sample_rate(400)
sensor.set_fifo_average(8)
sensor.set_active_leds_amplitude(MAX30105_PULSE_AMP_MEDIUM)

print('Place finger on sensor')
while True:
    sensor.check()
    if sensor.available():
        print(sensor.pop_ir_from_storage())
        time.sleep_ms(20)

Expected test result: The raw values should change when your finger is placed on the sensor and kept still.

Wi-Fi Connection Test

Check that the Pico connects to Wi-Fi and prints its IP address.

import network
import time

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

for _ in range(15):
    if wlan.isconnected():
        break
    print('Connecting...')
    time.sleep(1)

print('Connected:', wlan.isconnected())
if wlan.isconnected():
    print('IP address:', wlan.ifconfig()[0])

Expected test result: The Shell should show Connected: True and print an IP address.


Full Project Code

Upload and run this code after the individual tests work correctly.

import network
import socket
import time
from machine import SoftI2C, Pin
from max30102 import MAX30102, MAX30105_PULSE_AMP_MEDIUM

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'


class HeartRateMonitor:
    def __init__(self, window_size=150, smoothing_window=5):
        self.window_size = window_size
        self.smoothing_window = smoothing_window
        self.samples = []
        self.times = []
        self.filtered = []

    def add_sample(self, sample):
        now = time.ticks_ms()
        self.samples.append(sample)
        self.times.append(now)

        if len(self.samples) >= self.smoothing_window:
            average = sum(self.samples[-self.smoothing_window:]) / self.smoothing_window
            self.filtered.append(average)
        else:
            self.filtered.append(sample)

        if len(self.samples) > self.window_size:
            self.samples.pop(0)
            self.times.pop(0)
            self.filtered.pop(0)

    def calculate_bpm(self):
        if len(self.filtered) < 3:
            return None

        recent = self.filtered[-self.window_size:]
        minimum = min(recent)
        maximum = max(recent)
        threshold = minimum + (maximum - minimum) * 0.5

        peaks = []
        start_index = max(1, len(self.filtered) - self.window_size + 1)
        end_index = len(self.filtered) - 1

        for i in range(start_index, end_index):
            if (self.filtered[i] > threshold and
                self.filtered[i - 1] < self.filtered[i] and
                self.filtered[i] > self.filtered[i + 1]):
                peaks.append(self.times[i])

        if len(peaks) < 2:
            return None

        intervals = []
        for i in range(1, len(peaks)):
            intervals.append(time.ticks_diff(peaks[i], peaks[i - 1]))

        average_interval = sum(intervals) / len(intervals)

        if average_interval <= 0:
            return None

        return 60000 / average_interval


i2c = SoftI2C(sda=Pin(8), scl=Pin(9), freq=400000)
sensor = MAX30102(i2c=i2c)

if sensor.i2c_address not in i2c.scan():
    raise OSError('MAX30102 not found on I2C bus')

sensor.setup_sensor()
sensor.set_sample_rate(400)
sensor.set_fifo_average(8)
sensor.set_active_leds_amplitude(MAX30105_PULSE_AMP_MEDIUM)

monitor = HeartRateMonitor(window_size=150, smoothing_window=5)
latest_bpm = None
last_update = time.ticks_ms()

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))
print('Place finger on sensor and keep still')


def web_page(bpm_value):
    if bpm_value is None:
        return '''<!DOCTYPE html>
<html>
<head><meta name='viewport' content='width=device-width, initial-scale=1'><meta http-equiv='refresh' content='5'><title>Heart Rate Monitor</title></head>
<body style='font-family:Arial;text-align:center;padding:40px'>
    <h1>Heart Rate Monitor</h1>
    <p>Place finger on sensor and wait...</p>
    <p>This educational project is not for medical use.</p>
</body>
</html>'''
    return '''<!DOCTYPE html>
<html>
<head><meta name='viewport' content='width=device-width, initial-scale=1'><meta http-equiv='refresh' content='5'><title>Heart Rate Monitor</title></head>
<body style='font-family:Arial;text-align:center;padding:40px'>
    <h1>Heart Rate Monitor</h1>
    <h2>{} BPM</h2>
    <p>This educational project is not for medical use.</p>
</body>
</html>'''.format(int(bpm_value))


address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)
server.settimeout(0.2)

while True:
    sensor.check()
    if sensor.available():
        monitor.add_sample(sensor.pop_ir_from_storage())

    if time.ticks_diff(time.ticks_ms(), last_update) >= 2000:
        latest_bpm = monitor.calculate_bpm()
        if latest_bpm is None:
            print('Waiting for stable pulse...')
        else:
            print('Heart Rate:', int(latest_bpm), 'BPM')
        last_update = time.ticks_ms()

    try:
        client, client_address = server.accept()
    except OSError:
        continue

    print('Client connected from', client_address)
    client.recv(1024)
    response = web_page(latest_bpm)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
MAX30102 setup Creates the sensor object and configures its sample settings The sensor must be configured before useful pulse data can be collected
HeartRateMonitor class Stores recent samples and estimates BPM from pulse peaks This turns raw pulse values into a simple BPM estimate
latest_bpm Stores the newest BPM estimate The browser page can show the most recent result
web_page() Shows either a waiting message or the estimated BPM This makes the project easier to understand in a browser

Expected Result

After entering your Wi-Fi details and running the code, the Shell should print an IP address. Opening that address in a browser should show a waiting message at first. After you place a finger on the sensor and keep still, the page should show an estimated BPM value.


Troubleshooting

Problem Possible Cause Solution
No BPM appears Finger is moving or signal is weak Place your finger gently on the sensor and keep still for several seconds
Import error for max30102 Library folder structure is wrong Use lib/max30102/init.py and lib/max30102/circular_buffer.py exactly
Sensor not found Wrong wiring or wrong I2C pins Recheck VCC, GND, SDA, and SCL on GPIO 8 and GPIO 9