
Introduction
During the Apollo missions astronauts wore ECG sensors to monitor their stress levels and assess their ability to perform complex tasks. These ECG signals were manually monitored from Earth, over a distance of 238,855 miles away, making the process challenging due to delays and data transmission errors. Today, advancements in technology have led to the development of small, inexpensive, offline wearable devices that utilize machine learning to determine stress levels. These modern devices can alert when a worker is unable to perform a critical task, ensuring safety and efficiency without the need for remote monitoring.
Disclaimer: This project is experimental and it is not intended to diagnose or treat any medical conditions.
Requirements
1 x Arduino Portenta H7 1 x AD8232 SparkFun Single Lead Heart Rate Monitor 5 x female-female jumper cables Edge Impulse AccountNote: All Edge Impulse users can extract heart rate and HRV features using this block for testing purposes. However, the Deployment option is only available for Enterprise users.
Circuit
Connect AD8232 Output to A0 Connect AD8232 Lo – to D2 Connect AD8232 Lo + to D3
ECG Intervals
The ECG is separated into two basic intervals: the PR Interval and the QT Interval. The PR interval is the initial wave generated by an electrical impulse traveling from the right atrium to the left. Then inside the QT there is a complex process that generates the signature “beep” in cardiac monitors. During QRS both ventricles begin to pump. After the initial contraction comes the ST segment (the time where the ventricles waiting to be “re-polarized”). Finally the T wave becomes present to actively “re-polarize”, or relax the ventricles.
Data Acquisition
For this project, the data will be the electrical activity of the heart. The AD8232 sensor is designed to extract, amplify, and filter small biopotential signals in the presence of noisy conditions, such as those created by motion or remote electrode placement.
- Yellow pad to the left.
- Red pad to the right.
- Green pad below the red pad.
Note: Remember to disconnect the AC from the laptop before sampling.Upload the Data Forwarding acquisition script to the Arduino Portenta using the Arduino IDE. Check that the values being obtained are stable in the Serial Monitor (Center value of about ~500 with spikes between +300/-200)

Note: Even with all these precautions, the AD8232 module could still produce a noisy signal. For a real deployment it is recommended to use professional equipment like the Elemyo EMG/ECG module MYO v1.5. For this Public Project synthetic data was used.Close the Serial Monitor and run
edge-impulse-data-forwarder
.
Select the Edge Impulse project and check that the frequency shows [SER] Detected data frequency: 50Hz
.
Go to https://studio.edgeimpulse.com/studio/Your-Project-ID/acquisition/training
Select Length 120.000 ms and take around 10 to 20 samples for each category to classify. For example, regular working versus stressed. Set aside 10% of the samples for testing.

The HR-HRV Block
Edge Impulse recently added a learning block specifically designed for this scenario. The HR/HRV Features block processes physiological signals like the electrocardiogram (ECG), with optional accelerometer inputs for enhanced accuracy in motion-prone applications, to extract key metrics such as heart rate (HR) and heart rate variability (HRV). HR measures the number of beats per minute, while HRV measures the time variance between successive heartbeats, also known as the interbeat interval (IBI). The block offers real-time HR estimation and HRV analysis on resource-constrained edge devices and leverages cutting-edge algorithms for precise feature extraction. The extracted features can be used on their own or to inform downstream machine learning tasks such as stress detection or heart health analysis.
Model Training
The training could require some parameters to be modified from the defaults. I have found the following parameters to work well for my dataset, with a 89.3% accuracy. Training cycles 40, learning rate 0.005, batch size 30 and no auto weight.
Deployment
For projects using the Edge Impulse HRV DSP block, a license to deploy is required, so there is a button to authorize this feature.
- Unzip the deployment file into a local folder on your laptop.
- Download a zip containing all files from https://github.com/edgeimpulse/example-hr-lib-arm.
- Unzip the files and copy them to the local folder you created in Step 1.
- Zip the folder, now with the additional files included, back into a single archive /
.zip
file - Add the new
.zip
file as a library in the Arduino IDE (Sketch, Include Library, Add Zip library)

Note: If Arduino Portenta shows Exit status 74
, double click “Reset”, and select the correct port.

Final Notes
Thanks to machine learning, monitoring ECG signals no longer requires transmitting data to a remote computer for expert analysis. Instead, subtle health conditions can be detected by small, offline, wearable devices equipped with machine learning capabilities. These devices can identify over-stressed workers who may be unable to perform their tasks effectively, thus preventing serious harm or consequences.