Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

This is an Arduino code for measuring heart rate using a heart rate sensor module.

Here's what the code does:

The necessary libraries for communicating with the OLED display and the heart rate
sensor module are included at the beginning of the code.
The pin to which the heart rate sensor module is connected is defined as
"heartBeatPin" and is set to A0.
In the setup() function, the OLED display is initialized and a message "Measuring
heart beat..." is displayed on it. Serial communication is also initialized at a
baud rate of 9600.
In the loop() function, an array "samples" is created to store the sample values,
and "beatInterval" is initialized to 0.
The code takes 10 samples of the analog signal from the heart rate sensor module,
with a delay of 50ms between each sample. These samples are stored in the "samples"
array.
The average of the samples is computed and stored in the "average" variable.
The code then searches for the peak and trough of the pulse wave in the "samples"
array.
The heart beat interval is calculated by subtracting the trough value from the peak
value and multiplying the result by 10.
The heart beat interval is displayed on the OLED display and sent to the serial
port.
Finally, there is a delay of 500ms before taking the next set of samples.
Overall, this code measures heart rate by detecting the peak and trough of the
pulse wave using an analog signal from a heart rate sensor module and calculating
the time interval between them. The heart rate is then displayed on an OLED display
and sent to the serial port for further processing.

Aron Eshetu
make it deep
Sure, I'd be happy to explain this code in more depth!

This code is used to measure a person's heart rate using a KY-039 heart rate sensor
connected to an Arduino board. The heart rate sensor works by detecting changes in
the blood volume as blood flows through the blood vessels in the fingertip, and
converting those changes into an electrical signal that can be read by the Arduino.

The code starts by including the necessary libraries for using the OLED display
(Adafruit_SSD1306.h) and for communicating with the Arduino (Wire.h). It then sets
up some constants, including the pin that the KY-039 sensor is connected to
(heartBeatPin), and initializes the Adafruit_SSD1306 display.

In the setup() function, the OLED display is initialized with some text that lets
the user know that the program is measuring heart rate. Serial communication is
also initialized, which is used later in the code to send the heart rate data to a
computer or other device.

The main loop() function is where the heart rate is measured. The loop starts by
creating an array (samples) to store the sensor readings and a variable
(beatInterval) to store the interval between heart beats.

The loop then takes a series of readings from the heart rate sensor by calling the
analogRead() function and storing the results in the samples array. A delay of 50
milliseconds is added between each reading to ensure that the sensor has time to
settle between readings.
After taking the readings, the loop calculates the average value of the samples by
adding up all the readings in the array and dividing by the number of samples. This
average value is used later in the code to calculate the heart rate.

The loop then searches for the peak and trough of the pulse wave by iterating
through the samples array and looking for the highest and lowest values. The peak
value represents the maximum blood volume in the fingertip, while the trough
represents the minimum blood volume.

The heart beat interval is then calculated by subtracting the trough value from the
peak value and multiplying the result by 10. This value is stored in the
beatInterval variable.

The heart rate data is then displayed on the OLED display using the
Adafruit_SSD1306 library. The display is cleared, and the text "Heart beat
interval: " is printed, followed by the calculated beatInterval value. The display
is then updated to show the new values.

Finally, the loop sends the heart rate data to the computer or other device using
the Serial.println() function, and waits for 500 milliseconds before starting the
loop again.

You might also like