How I get 100 hz frequency in arduino mega 2560

Dear Sir,

I have interfaced multiple sensors (ppg, pressure, ecg sensor) with arduino mega 2560, and get 10 Hz frequency that means I am getting 10samples per second. But I want to get 100 samples per second. So, how I will be achieve the 100 Hz frequency with arduino mega 2560.

Thank You

Your post was MOVED to its current location as it is more suitable.

Start by reading the advice given in How to get the best out of this forum and post your current sketch

Change

delay(100);

in your code to

delay(10);

I Used on delay function, but I got only 10 samples per second.

What is that?

post your code.

This my whole code.Please check and give me feed back.
#include "Protocentral_MAX30100.h" //for PPG MAX30100 sensor library.
#include <Wire.h> //for I2C Comm library.
#include "RTClib.h" //for RTC library
#include <SPI.h> //for SPI comm. library.
#include <SD.h> //for SD Card library.
#include <Q2HX711.h> //for pressure sensor library HX710.

RTC_DS1307 RTC; //RTC_DS1307 class object declaration
const int chipSelect = 53; //chip select pin for arduino mega 2560

#define REPORTING_PERIOD_MS 100 //delay of 100ms
uint32_t tsLastReport = 0;

int sensorValue=0; // store the gsr sensor value
int ecgs =0; // store the ecg sensor value
//-----------------declaration for MAX30100 PPG Sensor----------------
MAX30100 sensor;
uint8_t data_len=8;
uint8_t DataPacketHeader[15];
volatile long IRR,REDD;
//-----------------declaration for pressur HX710 Sensor----------------
const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 2;
int avg_val = 0; // variable for stroing raw data.
float pressure, pres_PSI; //before pressure declared as double
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin);

int state =0; //Create empty string called incomingData
const int air_pump = 5; //Pin 7 for connecting Mini Air Pump via ULN2803A driver
const int solenoid = 4; //Pin 6 for connecting solenoid Air valve via ULN2803A driver

void setup() {
Serial.begin(9600);
RTC.begin();
if (! RTC.isrunning())
Serial.println("RTC is running!");
RTC.adjust(DateTime(DATE, TIME));

while (!Serial)
{
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("Card initialized.");
Wire.begin();
sensor.begin(pw1600, i50, sr100 );
sensor.printRegisters();
pinMode(8, INPUT); // Setup for leads off detection LO +
pinMode(9, INPUT); // Setup for leads off detection LO -
pinMode(air_pump, OUTPUT);
pinMode(solenoid, OUTPUT);
digitalWrite(air_pump, HIGH); //Inflates pressure cuff
digitalWrite(solenoid, LOW);
}

void loop() {
if (Serial.available())
{
state = Serial.parseInt();
if(state == 1)
{
// Serial.println("Activated");
digitalWrite(air_pump, LOW); //Deflates pressure cuff
digitalWrite(solenoid, HIGH);
}
if (state == 2) {
// Serial.println("deactivated");
digitalWrite(air_pump, HIGH); //Inflates pressure cuff
digitalWrite(solenoid, LOW);
}
}

String dataString = "";
String dataStringA = "";
String dataStringB = "";
String dataStringC = "";
String dataStringD = "";

dataString = String(REDD,DEC);
dataStringA = String(IRR,DEC);
dataStringB = String(sensorValue);
dataStringC = String(pres_PSI);
dataStringD = String(ecgs);

DateTime now = RTC.now(); 
sensor.readSensor();
IRR=sensor.IR;
REDD=sensor.RED;
DataPacketHeader[0] = 0x0A;
DataPacketHeader[1] = 0xFA;
DataPacketHeader[2] = (uint8_t) (data_len);
DataPacketHeader[3] = (uint8_t) (data_len>>8);
DataPacketHeader[4] = 0x02;
 
DataPacketHeader[5] = REDD;
DataPacketHeader[6] = REDD>>8;
DataPacketHeader[7] = REDD>>16;
DataPacketHeader[8] = REDD>>24; 

DataPacketHeader[9] = IRR;
DataPacketHeader[10] = IRR>>8;
DataPacketHeader[11] = IRR>>16;
DataPacketHeader[12] = IRR>>24; 

DataPacketHeader[13] = 0x00;
DataPacketHeader[14] = 0x0b;

//
// for(int i=0; i<15; i++) // transmit the data
// {
// Serial.write(DataPacketHeader[i]);
// }
//-------------GSR Sensor Signal Reading-------------------------------
sensorValue=analogRead(A0);
//-------------Pressure Sensor Signal Reading--------------------------
avg_val =(hx711.read()/100);// add multiple ADC readings
//-------------ECG Sensor Signal Reading--------------------------
ecg();
//Serial.println(sensor.getPartID());

// Serial.println(REDD,HEX);
// Serial.println(IRR,HEX);
// Serial.print(now.day(), DEC),Serial.print('/');
// Serial.print(now.month(), DEC),Serial.print('/');
// Serial.print(now.year(), DEC),Serial.print(' ');
// Serial.print(now.hour(), DEC),Serial.print(':');
// Serial.print(now.minute(), DEC),Serial.print(':');
// Serial.print(now.second(), DEC),Serial.println();
// Serial.println(REDD,DEC);
// Serial.println(IRR,DEC);
// Serial.print("GSR = "),Serial.println(sensorValue);
// Serial.print("Pressure = "),Serial.println(pres_PSI);

if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
File dataFile = SD.open("tuesday.txt", FILE_WRITE);
if (dataFile)
{
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(',');
dataFile.print(dataString );
dataFile.print(',');
dataFile.print(dataStringA);
dataFile.print(',');
dataFile.print(dataStringB);
dataFile.print(',');
dataFile.print(dataStringC);
dataFile.print(',');
dataFile.println(dataStringD);
Serial.println(dataStringD);
dataFile.close();
// Serial.println(dataString);
}
else
{
Serial.println("error opening datalog.txt");
}
tsLastReport = millis();
}
}
void ecg() {
if((digitalRead(8) == 1) || (digitalRead(9) == 1)){ //check if leads are removed
//Serial.println("leads off!");
}
else
{
ecgs = analogRead(A1);
//Serial.println(ecgs);
}
}

clearly you have not read the recommendations for posting... hard to expect us to invest time in reading your post if you don't match the minimum expected...

So do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

That's about 1 millisecond per character. If you output more than 10 characters per iteration you will never be able to keep up 100 iterations per second. Try a higher baud rate like 115200 or even 250000 if your PC supports it.

What if you reduce that to 10ms for 100 Hz?

Sir, I tried this one but I got only 9 to 10 sample per second. And Same code checked with teensy4.1, I got 10 to 11 sample per second.

Sir, I tried on 1152000, 250000, 500000 baud rate but I got only 9 to 10 sample per second. And Same code checked with teensy4.1, I got 10 to 11 sample per second.

Most probably the data file is the bottleneck. Keep it open for 100 records and try again.

Sir I tried this one but i am not getting 100 sample per second. This way I was try to read one analog sensor data and store on memory card. Please check the code and output.
Code:
#include <SPI.h>
#include <SD.h>
#include <Wire.h> //for I2C Comm library.
#include "RTClib.h" //for RTC librarystrong text

RTC_DS1307 RTC; //RTC_DS1307 class object declaration
const int chipSelect = 53;

void setup()
{
Serial.begin(115200);
RTC.begin();
if (! RTC.isrunning())
Serial.println("RTC is running!");
RTC.adjust(DateTime(DATE, TIME));

while (!Serial)
{
;
}

Serial.print("Initializing SD card...");

if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("Card initialized.");
}

void loop()
{
Serial.println("Data LOG");
DateTime now = RTC.now();

String dataString = " ";
String dataStringa = " ";
String dataStringb = " ";
String dataStringc = " ";
String dataStringd = " ";
String dataStringe = " ";
String dataStringf = " ";

for (int i = 0; i <= 100; i++)
{
int sensor = analogRead(A0);
dataString = String(now.day(), DEC);
dataStringa = String(now.month(), DEC);
dataStringb = String(now.year(), DEC);
dataStringc = String(now.hour(), DEC);
dataStringd = String(now.minute(), DEC);
dataStringe = String(now.second(), DEC);
dataStringf += String(sensor);
dataStringf += ",";
}

File dataFile = SD.open("mon.txt", FILE_WRITE);
if (dataFile)
{
// dataFile.println("Data Log");
dataFile.print(dataString);
dataFile.print('/');
dataFile.print(dataStringa);
dataFile.print('/');
dataFile.print(dataStringb);
dataFile.print(' ');
dataFile.print(dataStringc);
dataFile.print(':');
dataFile.print(dataStringd);
dataFile.print(':');
dataFile.print(dataStringe);
dataFile.print('=');
dataFile.print(dataStringf);
dataFile.close();
Serial.print(dataString),Serial.print("/");
Serial.print(dataStringa),Serial.print("/");
Serial.print(dataStringb),Serial.print(" ");
Serial.print(dataStringc),Serial.print(":");
Serial.print(dataStringd),Serial.print(":");
Serial.print(dataStringe),Serial.print(" =");
Serial.print(dataStringf);
Serial.print(",");
}
else
{
Serial.println("error opening datalog.txt");
}
}

Output:

thanks
himanshu

Why do you 100 times crap?

because, I want 100 sample so I put a loop 100 times. If this is wrong please suggest me right direction.

Update the date and time strings once, before taking 100 sample.
Write out everything immediately, don't build a string from 100 samples.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

First of all, make sure that your sensor can actually be polled at that frequency. For a lot of sensor, the maximum polling frequency is much lower.