Introduction
In this tutorial we are going to learn how to obtain a very simple PPG wave on the Arduino IDE serial plotter, using the ESP32 and a Heart Rate Sensor. We will be using the Arduino core to program the ESP32.
For an introductory tutorial on how to get started with the Heart Rate Sensor module (including an electric diagram explaining how to connect it to the ESP32), please check here.
The tests shown below were performed on a ESP32-E FireBeetle board from DFRobot. The Arduino core version used was 2.0.0 and the Arduino IDE version was 1.8.15, working on Windows 8.1.
The code
We will start our code by defining a variable that will hold the number of the pin connected to the sensor. In my case I’m using pin 25, in accordance with the electric diagram from the previous tutorial. Note that we should be using a pin that supports analog measurements.
const int sensorPin = 25;
Our setup function will consist on opening a serial connection, so we can output the sensor measurements we will obtain periodically in the Arduino main loop.
void setup() {
Serial.begin(115200);
}
Finally, in the Arduino main loop, we will take care of getting the actual sensor measurements. We will start by calling the analogRead function, passing as input the number of the pin we previously stored in a global variable.
int value = analogRead(sensorPin);
We will then print this value directly to the serial port. Note that these are the measurements that the Arduino IDE serial plotter will display later, when testing the code.
Serial.println(value);
Finally, we will add a small 10 milliseconds delay between each iteration of the Arduino main loop. Note that we are using delays to keep the code simple. Nonetheless, if you want to use a more sophisticated approach, you can use timer interrupts or the Ticker library, for example.
delay(10);
The full loop is shown below.
void loop() {
int value = analogRead(sensorPin);
Serial.println(value);
delay(10);
}
The complete code is available on the snippet below.
const int sensorPin = 25;
void setup() {
Serial.begin(115200);
}
void loop() {
int value = analogRead(sensorPin);
Serial.println(value);
delay(10);
}
Testing the code
Before uploading the code to the ESP32, make sure that all the connections between the microcontroller and the sensor module are appropriately done. Additionally, make sure to have the sensor properly attached to your finger or wrist. In my case, I’ve tested with the sensor attached to the index finger of my right hand.
Also make sure to have the sensor module switch position to Analog mode (signaled with an “A“).
After this, simply compile and upload the code to the ESP32 using the Arduino IDE. Once the procedure finishes, open the IDE Serial Plotter. If you haven’t ever done it, this option is located in Tools -> Serial Plotter, right below the Serial Monitor option. Make sure to confirm the Baud Rate of the serial monitor is the same that we used in our code (115200).
If everything is working correctly, you should obtain a result similar to figure 1. Note that we are just taking raw samples every 10 milliseconds, without doing any processing of the data, meaning that it is expected that the wave has noise. In order to obtain a stable waveform, you should try to keep the sensor as stable as possible.
IMPORTANT: Regardless of how good your results may seem, this sensor is not intended to be used as a professional medical device. Taking this in consideration, the sensor should not be used to diagnose or treat any medical condition [1].
References
[1] https://www.dfrobot.com/product-1540.html