Raw value of pulse sensor to BPM

How to convert the raw value from the pulse sensor to BPM? Should I definetly use an arduino or can i directly use the BOLT wifi module?. Please provide the code for the same?

1 Like

Formula:
To convert the raw value from a pulse sensor to beats per minute (BPM), you need to follow these steps:

  1. Measure the time between two consecutive pulses in seconds. Let’s call this value “T”. This represents the time it takes for your heart to beat once.
  2. Calculate the pulse rate in beats per second (BPS) by dividing 1 by the time between pulses. This tells you how many times your heart beats in one second.
  3. Finally, convert the pulse rate from beats per second to beats per minute by multiplying it by 60. This gives you the number of beats your heart makes in one minute.
    eg: time between two pulses is 0.8 seconds.

To calculate the pulse rate:
BPS = 1 / 0.8 = 1.25 beats per second.

To convert to beats per minute:
BPM = BPS * 60 = 1.25 * 60 = 75 beats per minute.

What to use?
It’s possible to perform this conversion using the Bolt Wi-Fi module, it might be more convenient to use an Arduino or another microcontroller with analog input capabilities for signal conditioning and processing.

Code:

const Bolt = require('boltiot');

// Configure the Bolt Wi-Fi module
const bolt = new Bolt({
  apiKey: '<BOLT_API_KEY>',
  deviceName: '<BOLT_DEVICE_NAME>',
  debug: false
});

const pulseSensorPin = 'A0';   // Pin connected to pulse sensor
const numSamples = 10;         // Number of samples to average
let pulseSensorSum = 0;        // Sum of sensor values
let pulseSensorAvg = 0;        // Average of sensor values
let lastBeatTime = 0;          // Time of the last beat
let bpm = 0;                   // Calculated BPM

// Read raw sensor value and calculate average
function readPulseSensor() {
  bolt.analogRead(pulseSensorPin, numSamples, (data) => {
    pulseSensorSum = data.value.reduce((sum, value) => sum + value, 0);
    pulseSensorAvg = pulseSensorSum / numSamples;
    pulseSensorSum = 0;

    calculateBPM();
  });
}

// Calculate BPM
function calculateBPM() {
  if (pulseSensorAvg > 550) {
    if (pulseSensorAvg > bolt.analogValue) {
      lastBeatTime = Date.now();
    }
  }

  if (Date.now() - lastBeatTime > 2000) {
    bpm = 0;
  } else {
    bpm = 60000 / (Date.now() - lastBeatTime);
  }

  sendBPMToBolt();
}

// Send BPM to Bolt Wi-Fi module
function sendBPMToBolt() {
  const data = {
    pulse: bpm
  };

  bolt.digitalWrite('0', 'HIGH', (response) => {
    if (response.success) {
      console.log('BPM:', bpm);
    } else {
      console.log('Failed to send BPM to Bolt:', response.error);
    }
  });
}

// Start reading the pulse sensor
readPulseSensor();

Code Explanation:
This code uses the Bolt IoT Node.js library to communicate with the Bolt Wi-Fi module. It reads the raw sensor value from the pulse sensor by performing an analog read on the specified pin (A0 ). It then calculates the average value over a specified number of samples and detects beats based on a threshold value (550 in this example). The BPM is calculated based on the time interval between beats and sent to the Bolt Wi-Fi module.

Note:

  1. Install the Bolt IoT Node.js library by running the following command in your project directory: npm install boltiot.
  2. Replace <BOLT_API_KEY> with your Bolt API key and <BOLT_DEVICE_NAME> with the name of your Bolt device.
  3. Connect the pulse sensor to the Bolt Wi-Fi module as per the manufacturer’s instructions.
  4. Run the JavaScript code using Node.js (node your_script_name.js).
  5. Make sure you have a working internet connection and the Bolt Wi-Fi module is properly set up and configured before running the code.

Hi, @s.ashwinji

To convert the raw value from the pulse sensor to beats per minute (BPM), you typically need to process the raw sensor data and perform calculations to determine the heart rate. While an Arduino board is commonly used for this purpose, it is also possible to directly use the Bolt WiFi module to process the pulse sensor data and calculate the BPM.

Here’s an example of code that demonstrates how you can convert the raw sensor value to BPM using the Bolt WiFi module:

import math
from boltiot import Bolt

Configure your Bolt WiFi module

API_KEY = “YOUR_API_KEY”
DEVICE_ID = “YOUR_DEVICE_ID”

Initialize the Bolt object

mybolt = Bolt(API_KEY, DEVICE_ID)

Function to calculate BPM from raw sensor value

def calculate_bpm(raw_value):
# Calculate heart rate using your preferred algorithm
# Example: A simple calculation based on the raw sensor value
bpm = math.floor(raw_value / 10) # Adjust the calculation as per your requirements
return bpm

Function to read the raw sensor value and calculate BPM

def read_pulse_sensor():
response = mybolt.analogRead(‘A0’) # Replace ‘A0’ with the appropriate analog pin
sensor_value = int(response[‘value’])
bpm = calculate_bpm(sensor_value)
return bpm

Main program loop

while True:
bpm = read_pulse_sensor()
print(“BPM:”, bpm)

In the code above, the calculate_bpm function takes the raw sensor value as input and performs the necessary calculations to convert it to BPM. Modify the calculation based on your specific pulse sensor and algorithm.

The read_pulse_sensor function reads the raw sensor value from the Bolt WiFi module’s analog pin (replace 'A0' with the appropriate pin connected to your pulse sensor). It then calls the calculate_bpm function to convert the raw value to BPM.

The main program loop continuously reads the sensor value, calculates the BPM, and prints the result. Make sure you have the Bolt IoT Python library (boltiot) installed before running the code. You can install it using pip install boltiot.

Please note that the example code provided assumes you have a pulse sensor connected to the Bolt WiFi module and have configured the Bolt WiFi module with your API key and device ID.