Hello friends, I hope you all are doing great. Today, I am going to share the MAX30102 Heart Rate Zone Trainer on ESP32 in Proteus Simulation, the fifty-seventh project of our IoT Projects series. If you have searched for an ESP32 heart rate monitor, a MAX30102 pulse oximeter project, or a Proteus simulation of SpO2 measurement, here is a complete one. A fingertip rests on a MAX30102 sensor; the ESP32 turns the light it measures into the pulse and the blood oxygen, shows them on a small OLED, beeps with every heartbeat like a hospital monitor, and coaches a workout: it names the heart-rate zone you are training in, counts the seconds in each zone, and raises an alarm when the oxygen drops. A dashboard on a laptop or a phone starts and stops the workout and draws the last 160 seconds.
Pulse oximeters clip onto a finger in every hospital ward, and the same sensor sits on the back of every fitness watch. In this tutorial, we will go through the circuit, see how two LEDs and a photodiode can measure a pulse and the oxygen in the blood, find the beats in the infrared wave, work out SpO2 from the red and infrared pulses, turn the pulse into training zones, make the beep's pitch follow the oxygen, walk through the code, and then run the simulation: rest, a walk, a run, a drop in oxygen that sets off the alarm, a finger taken off the sensor, and the workout summary. At the end, you can download the project with the sketch and every library file.
NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the MAX30102 heart rate zone trainer on the ESP32 in Proteus simulation:
What Does This Zone Trainer Do?
- Measures the pulse and SpO2: a MAX30102 lights the fingertip with a red and an infrared LED, 25 times a second; every second the ESP32 works out the beats per minute and the blood oxygen from the last 4 seconds.
- Beeps with the heart: a piezo disc beeps on every beat, and the beep's pitch follows SpO2: 98 % is a bright 1140 Hz, 85 % a duller 750 Hz, as on a bedside monitor.
- Names the training zone: for a 40-year-old the maximum heart rate is about 220 - 40 = 180 BPM; under 50 % of it is REST, then five zones from WARM UP to MAX.
- Coaches a workout: the button (or the dashboard) starts and stops a workout; every second goes to the zone you are in, and STOP prints the summary.
- Watches the oxygen: SpO2 under 92 % for 5 seconds is an alarm: the red LED, a high tone every second and LOW SpO2 on the screen.
- Knows when there is no finger: without a finger the readings stop, and the workout waits.
- Shows it all: a 0.91-inch OLED with the pulse, a beating heart, SpO2 and the zone, and a dashboard with the workout and a trend picture of the last 160 seconds.
It is a great project for learning optical heart-rate sensing, photoplethysmography, signal processing on a small microcontroller, I2C with two devices, sound with a piezo, and a small web server, all on the ESP32. In the simulation, the MAX30102's pop-up lets you set the pulse and SpO2 the fingertip has, take the finger off, and use presets for rest, walking, running and low oxygen; it also shows exactly what the sketch read. The OLED's pop-up shows the screen and every I2C transfer; the piezo's pop-up shows the tone, its note and how loud it is.
Components Used in the Proteus Simulation
All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.
| Part | Proteus device | Library post | Connection |
|---|---|---|---|
| ESP32 DevKit V1 | ESP32 DEVKIT V1 | ESP32 DevKit V1 Library for Proteus | U1, ESP32_Zone_Trainer.hex, TEP-WiFi 192.168.1.101 |
| MAX30102 pulse oximeter | MAX30102ADVTEP | MAX30102 Library for Proteus | U2, I2C at 0x57 |
| 0.91" OLED 128x32 (SSD1306) | OLED12832WHITEADVTEP | OLED 128x32 Library for Proteus | OLED1, I2C at 0x3C |
| 27 mm piezo disc | PIEZODISC27TEP | Passive Buzzer Library | BZ1, D26 |
| Push button | BUTTONGREENTEP | Push Button Library | SW1, D25, START / STOP |
| SMD LEDs green / red | LEDSMDGREENTEP, LEDSMDREDTEP | SMD LED Library | D1 the beat (D18), D2 the alarm (D19) |
| SMD resistors | RESISTORSMD1206TEP | SMD Resistor Library | R1 / R2 220 ohm, R3 10 kohm |
| Laptop WiFi Monitor | LAPTOP WIFI MONITOR | Laptop WiFi Monitor | PC1 |
| Mobile WiFi Monitor | MOBILE WIFI MONITOR | Mobile WiFi Monitor | MOB1 |
Proteus Simulation of the Zone Trainer: the Circuit
| Part | Pins | ESP32 |
|---|---|---|
| U2 MAX30102, OLED1 | SDA / SCL | D21 / D22 |
| U2 / OLED1 | VIN / VCC | the supply (3.3 V) |
| BZ1 piezo | + / - | D26 / GND |
| D1 green, D2 red | A (through 220 ohm) / K | D18, D19 / GND |
| SW1 | 1 / 2 | 3V3 / D25, with R3 10 kohm from D25 to GND |
Both I2C parts share one bus: the MAX30102 at address 0x57 and the OLED at 0x3C. The GY-MAX30102 board has its own regulators, so it takes 3.3 V or 5 V on VIN; in Proteus its VIN sits on the default power rail, because its model reads the supply as a logic level. A piezo disc is electrically a small capacitor, so a GPIO pin drives it directly with a square wave, no transistor needed. The button connects D25 to 3.3 V when pressed, and the 10 kohm resistor holds D25 low the rest of the time.
How the Zone Trainer Works
Light Through a Fingertip: the PPG Wave
Each heartbeat pushes a little more blood into the fingertip, and blood absorbs light. The MAX30102 shines its red (660 nm) and infrared (880 nm) LEDs into the finger and measures the light that comes back with a photodiode and an 18-bit converter. The result is a steady level, the skin, bone and still blood, with a small ripple on top, the pulsing blood: a photoplethysmogram, or PPG. Every heartbeat makes one ripple, a fast rise and a slower fall. The sketch asks for 100 samples a second averaged in groups of four, so 25 samples a second reach it through the sensor's 32-sample FIFO.
Finding the Beats
The sketch keeps the last 100 samples, 4 seconds, in a ring buffer. Every second it finds the lowest and the highest infrared value in that window and takes the middle level between them. Each time the wave crosses the middle level upward, that is one beat. The pulse comes from the spacing of the crossings, not from counting them in a fixed time, which keeps it accurate with only a few beats in the window. Worked example: at 72 BPM a beat lasts 60 / 72 = 0.83 s, about 21 samples; with five crossings at samples 8, 29, 50, 71 and 92, the four gaps span 84 samples, so the pulse is 60 x 25 x 4 / 84 = 71.4 BPM. The median of the last three estimates is shown, so one odd window cannot make the number jump. The same middle level drives a live beat detector on every new sample: the green LED and the beep come on each upward crossing, at most once every 300 ms.
Blood Oxygen from Two Colours
Oxygen-rich haemoglobin absorbs less red light than oxygen-poor haemoglobin, while both absorb infrared about the same. So the size of the red ripple compared with the infrared ripple tells how much of the haemoglobin carries oxygen. Each ripple (AC) is first divided by its own steady level (DC), which cancels the LED brightness and the thickness of the finger: R = (AC red / DC red) / (AC IR / DC IR). Then a calibration curve turns R into SpO2; this sketch uses Maxim's approximation, SpO2 = -45.060 R x R + 30.354 R + 94.845. For example R = 0.5 gives -11.27 + 15.18 + 94.85 = 98.8 %, and R = 0.9 gives -36.50 + 27.32 + 94.85 = 85.7 %. Real oximeters are calibrated against blood samples; the curve here is the reference one.
Heart-Rate Training Zones
Training zones are shares of the maximum heart rate. The simplest estimate of the maximum is 220 minus the age; for a 40-year-old that is 180 BPM. Under 50 % of it (under 90 BPM) is REST; 50 - 60 % (90 - 108 BPM) is Z1 WARM UP; 60 - 70 % (108 - 126) Z2 FAT BURN; 70 - 80 % (126 - 144) Z3 CARDIO; 80 - 90 % (144 - 162) Z4 HARD; and from 90 % (162 BPM) Z5 MAX. A new zone only counts after 3 seconds in it, so a pulse climbing from walking to running does not flash through every zone on the way.
A Beep That Tells the Oxygen
Bedside monitors beep with every heartbeat, and the beep's pitch falls as the oxygen falls, so a nurse can hear a problem without looking at the screen. The sketch does the same: the pitch is 300 Hz + 30 Hz for every percent of SpO2 above 70 %. At 98 % that is 300 + 28 x 30 = 1140 Hz; at 86 % it would be 780 Hz. During an alarm the heartbeat beep stops and a 2 kHz tone sounds once a second instead.
The Arduino Code Explained
The sketch is ESP32_Zone_Trainer.ino. It uses SparkFun's MAX3010x library for the sensor, Adafruit SSD1306 and GFX for the OLED, and the ESP32's WiFi and WebServer for the dashboard. The settings are at the top:
const int AGE = 40, HR_MAX = 220 - AGE; // 180 BPM
const float SPO2_LOW = 92, SPO2_OK = 94; // the alarm: under 92 % for 5 s, cleared at 94 %
The sensor is set up in one line: 12 mA LEDs, an average of 4, red and infrared, 100 samples a second:
sensor.setup(60, 4, 2, 100, 411, 4096); // 12 mA LEDs, average 4, red + IR, 100 Hz, 411 us, 4096 nA
The main loop empties the sensor's FIFO into the sketch's own ring buffer:
while (sensor.available()) { sample(sensor.getFIFOIR(), sensor.getFIFORed()); sensor.nextSample(); }
Each new sample can be a beat, an upward crossing of the middle level:
if (up && !above && finger && millis() - beatAt >= MIN_BEAT_MS) beat();
A finger is a bright infrared signal with a pulse in it; an empty sensor sees little light and no ripple:
bool f = irSum / WIN > FINGER_IR && irMax - irMin > 100;
Every second the window gives the pulse, from the spacing of the crossings, and SpO2, from the ratio of ratios:
float b = 60.0 * SPS * (n - 1) / (last - first); // beats a minute from the crossings' spacing
float R = ((rMax - rMin) / (rSum / WIN)) / ((irMax - irMin) / (irSum / WIN));
float s = constrain(-45.060 * R * R + 30.354 * R + 94.845, 0, 100);
The beep's pitch follows SpO2:
if (!alarmOn && spo2 > 0) tone(PIEZO, (unsigned int)constrain(300 + (spo2 - 70) * 30, 300, 1500), 40); // 98 % = 1140 Hz
The zone of a pulse is its share of the maximum:
return p < 0.5 ? 0 : p < 0.6 ? 1 : p < 0.7 ? 2 : p < 0.8 ? 3 : p < 0.9 ? 4 : 5;
About once a second, during a workout, the time since the last tick goes to the current zone. It is the real elapsed time from millis(), not a count of ticks, so the seconds the ESP32 spends sending a page to the dashboard are not lost:
zoneMs[zone] += dt; woMs += dt; woBeats += bpm * dt / 1000.0;
The dashboard's pages are / (the dashboard), /start and /stop (the workout), /trend.bmp (a 160 x 64 picture of the last 160 seconds, a point a second, drawn by the ESP32 itself and sent eight rows at a time, so it needs only 640 bytes of memory) and /data (everything as JSON). Every finger change, zone, alarm and workout is printed on the serial monitor.
How to Run the Proteus Simulation
- Download the project zip below and install the library files (the steps are under the download button).
- Open ESP32-Zone-Trainer.pdsprj from the Proteus Simulation folder. Keep ESP32_Zone_Trainer.hex and the DLL files next to it.
- Press Play. The MAX30102 starts with a finger on it at 72 BPM; after a few seconds the OLED shows the pulse, SpO2 and REST, and the piezo beeps with each beat.
- Press SW1 (click it, or its pop-up's Press) to start a workout.
- On the MAX30102's pop-up (U2), press WALK 95, RUN 150 or LOW SPO2, or move the PULSE RATE and SPO2 TARGET sliders; FINGER OFF takes the finger away.
- Open the Laptop or the Mobile WiFi Monitor and press Go for the dashboard, with START and STOP.
Simulation Results: the Zone Trainer in Proteus
Here is the run that made the screenshots of this tutorial, 114 seconds of simulation: rest, a walk, a run, a drop in oxygen, a finger taken off the sensor and back, and the workout summary. The times are the sketch's own clock, as it printed them on the serial monitor; the parts' numbers come from their pop-ups.
At Rest, and the Workout Starts
The MAX30102 starts with a finger on it at 72 BPM. At 6.3 s the sketch sees it ("a finger on the sensor"), and at 10.0 s, after its first full 4-second window, it reads 72 BPM and SpO2 98 %: REST, 40 % of the maximum. With the REST 60 preset the pulse settles at 60 BPM; the MAX30102's pop-up shows what the sketch read over I2C, IR 50990 and RED 50528, and decodes it as 60.0 BPM and 98.0 %, the same numbers the sketch works out. At 18.1 s SW1 starts the workout. The OLED shows 60, a heart, 98 % SpO2, REST and the workout clock; the dashboard shows the zone times and the trend picture, the pulse in grey (REST) and SpO2 in white in the band below.
A Walk: Zone 1
WALK 95 raises the pulse. The window catches up over a few seconds, and at 36.7 s: "Z1 WARM UP - 95 BPM, 53 % of HRmax", with SpO2 97 %. The piezo's pop-up catches a heartbeat beep: 1111 Hz, the note C#6. The sketch asked for 300 + (97 - 70) x 30 = 1110 Hz; the 1 Hz difference is the timer's step. The pop-up also shows the beep 21 dB below the disc's loudest, because a 27 mm piezo disc resonates near 4.6 kHz.
A Run: Zone 4
RUN 150: at 50.7 s "Z4 HARD - 150 BPM, 83 % of HRmax", SpO2 95 %. The pulse passed through zones 2 and 3 on the way, but never stayed 3 seconds in either, so the trainer went straight from Z1 to Z4. The MAX30102's pop-up shows the fast pulse wave and the sketch's reading decoded as 150.0 BPM, 95.0 %. On the dashboard the trend line climbs from grey through blue to orange, and the workout shows REST 19 s, Z1 14 s and Z4 7 s so far.
Low Oxygen: the Alarm
The LOW SPO2 preset gives the fingertip 80 BPM and 85 % oxygen. SpO2 falls under 92 %, and 5 seconds later, at 69.1 s: "ALARM: SpO2 85 % - under 92 % for 5 s". The red LED comes on, the OLED shows LOW SpO2!, and the heartbeat beep gives way to the alarm tone, measured by the piezo's pop-up at 2000 Hz, the note B6. A second later the pulse drops the zone to REST (80 BPM). The phone shows the alarm at the top of the dashboard, and the trend shows SpO2 under the dotted 92 % line. With REST 60 again, SpO2 returns, and at 85.8 s: "SpO2 back to 98 % - the alarm cleared".
No Finger, Then the Summary
FINGER OFF at 92.8 s: "no finger - put a fingertip on the sensor". The OLED says NO FINGER, the readings stop and the workout clock waits at 1:15. With the finger back, at 98.8 s, the first window fills and the zone returns at 104.8 s. At 105.6 s STOP on the laptop: "workout 1 done: 1:16 - REST 43 s, Z1 WARM UP 14 s, Z4 HARD 19 s; peak 150 BPM, average 94 BPM, lowest SpO2 85 %". The dashboard shows the same zone times in whole seconds, rounded down (Z4 18 s), and the whole workout on the trend picture, with the gap where the finger was off.
| What | Result |
|---|---|
| At rest | 60 BPM, SpO2 98 % (the sketch read IR 50990 RED 50528); REST |
| Walk | 95 BPM, 97 %: Z1 WARM UP at 36.7 s; the beat beep 1111 Hz |
| Run | 150 BPM, 95 %: Z4 HARD at 50.7 s (83 % of HRmax) |
| Low oxygen | 80 BPM, 85 %: the alarm 5 s later (69.1 s), 2000 Hz; cleared at 98 % |
| No finger | the readings and the workout paused; back 6 s later |
| Workout | 1:16 - REST 43 s, Z1 14 s, Z4 19 s; peak 150, average 94 BPM, lowest SpO2 85 % |
| Simulation | 114 s of simulation |
Download the Project Files
Click the button below to download MAX30102-Heart-Rate-Zone-Trainer-ESP32-Proteus-Simulation-v1.0.zip (about 4.9 MB, without the source code of the models):
MAX30102 Heart Rate Zone Trainer on ESP32: Proteus Simulation V1.0- Proteus Simulation: ESP32-Zone-Trainer.pdsprj, ESP32_Zone_Trainer.hex and copies of the models.
- Arduino Code: ESP32_Zone_Trainer.ino, the sketch.
- Proteus Library Files: the LIB files of the ESP32 DevKit V1, the MAX30102, the 128x32 OLED, the piezo, the button, the SMD LED and resistor and the two WiFi Monitors.
- Proteus Model Files: the DLL models.
- README.txt: the installation steps, the properties, the test results and the links to every library post.
No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.
How to Install the Project Files
- Close Proteus and extract the whole zip file, for example to your Desktop.
- Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
- Copy the files from Proteus Model Files into the MODELS folder.
- Start Proteus and open the project from the Proteus Simulation folder.
Changing the Code
Open ESP32_Zone_Trainer.ino in the Arduino IDE with our TEP Boards for Proteus, choose ESP32 Dev Module (TEP Proteus), and export the compiled binary; U1's PROGRAM property names the .hex file it runs. Your age, the alarm level and the zone time are at the top. On a real ESP32 the same sketch runs with the standard ESP32 board package and the SparkFun MAX3010x and Adafruit SSD1306 libraries from the Library Manager.
Ideas to Extend the Project
- Enter your age on the dashboard, or use a measured maximum heart rate instead of 220 minus the age.
- Add a target zone: a double beep when you drop below it, a long beep when you go above it.
- Estimate the calories from the time in each zone, your weight and your age.
- Log every workout to an SD card or send it to a server after STOP.
- Add the MAX30102's die temperature to the dashboard, and a skin-temperature sensor.
- Measure heart-rate recovery: how many beats the pulse drops in the first minute after STOP.
From Simulation to Real Hardware
- Keep the finger still: motion makes the PPG wave jump; real trainers combine the sensor with an accelerometer to reject it.
- Shade the sensor: room light reaches the photodiode too; press the finger gently and cover it.
- Do not press too hard: squeezing the fingertip pushes the blood out, and the pulse wave shrinks.
- Power: the GY-MAX30102 and the OLED from 3.3 V; the piezo straight from a GPIO.
- Not a medical device: a hobby oximeter is fine for learning and fitness, never for medical decisions.
Troubleshooting
- MAX30102 not found at 0x57: check SDA / SCL on D21 / D22 and the board's supply.
- NO FINGER all the time: the IR level must be above 20000; on real hardware raise the LED current.
- The pulse is half or double: the wave is noisy; average more samples or lower the LED current if it saturates.
- No beep: the piezo between D26 and GND; tone() needs the pin as an output.
- The button does nothing: it must switch 3.3 V to D25, with the 10 kohm resistor to GND.
Limitations of the Simulation
- The fingertip is the MAX30102 model's: a clean pulse wave whose rate and SpO2 you set on its pop-up; real fingers move, breathe and press.
- SpO2 uses Maxim's reference curve, not a curve calibrated for a particular sensor and finger.
- The zones use 220 minus the age, a rough population estimate.
- The network to the laptop and the phone is simulated inside Proteus.
- The MAX30102 is a sensor of Analog Devices (Maxim Integrated) and the SSD1306 a controller of Solomon Systech; this project is not made or endorsed by them or by Labcenter, and it is not a medical device.
So, that was all about the MAX30102 Heart Rate Zone Trainer on ESP32 in Proteus Simulation. I hope it helps you understand how light through a fingertip carries the pulse, how two colours of light measure the oxygen in the blood, how a small microcontroller finds the beats in a noisy wave, and how training zones turn a pulse into coaching, all tested in a simulation before you build a real one. More IoT projects with the ESP32 and the Raspberry Pi 5 are coming in this series. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!