# what will be the sampling frequecy of my arduino Mega 2560 code. How would I calculate it?

``````void setup() {
// initialize the serial communication:
Serial.begin(9600);
pinMode(10, INPUT); // Setup for leads off detection LO +
pinMode(11, INPUT); // Setup for leads off detection LO -
}

void loop() {
if ((digitalRead(10) == 1) || (digitalRead(11) == 1)) {
Serial.println('!');
}
else {
// send the value of analog input 0:
}
//Wait for a bit to keep serial data from saturating
delay(1);
}
``````

EDIT

This sketch is for plotting raw ECG signal having a baud rate of 9600. but when I use these data for calculating heart rate then I want sampling rate for calculation but I don't get the value by this sketch. How will I calculate the frequency from the coming raw data? As it does not show a clear picture.

On the Arduino Mega, the ADC is clocked at `F_CPU`/128 = 125 kHz (period = 8 µs). Except for the very first one, each ADC conversion takes 13 ADC clock cycles, i.e. 104 µs. If you run `analogRead()` in a tight loop, like

``````for (;;)
``````

the CPU has to do some work in between the conversions, and since the ADC always starts by waiting for a rising edge of the ADC clock, you loose at least one full ADC cycle in between the conversions. You then get one conversion every 14 ADC cycles, which results in a sampling period of 112 µs, and the corresponding sampling frequency of about 8.93 kHz.

The code you showed does a little bit more than an `analogRead()` per iteration: two `digitalRead()`, a test, and a `Serial.println()` that involves a binary to decimal conversion. I am not sure the CPU will be able to handle all that in a mere 8 µs. You might end up using 15 ADC clock cycles per iteration (13 for the conversion, 2 for the computation), which would result in 120 µs (8.33 kHz) sampling period. Maybe even more. You will have to test if you want to know for sure. The sampling period is not even guaranteed to be constant: the code will take longer to run whenever it's interrupted by the timer interrupt, which the Arduino core uses for timekeeping purposes...

All this applies as long as the serial output buffer is not full. Once you have 64 bytes waiting to be sent, the buffer will be full, and `Serial.println()` will start delaying the output, effectively throttling down your loop to the speed of the serial connection. C.f. the busybee's answer for the details. Note that the sampling period is still not constant, as sending a 1-to-4-digit number through the serial port can take anywhere between 3.125 and 6.25 ms.

If you want a constant sampling period, you have to take control over the timing yourself. A straightforward example would be:

``````const uint32_t SAMPLING_PERIOD = 6500;  // 6.5 ms

void loop() {
static uint32_t last_sample;
if (micros() - last_sample >= SAMPLING_PERIOD) {
last_sample += SAMPLING_PERIOD;
}
}
``````

You could go faster with a higher baud rate, but you have to always keep `SAMPLING_PERIOD` consistent with your chosen baud rate. The code above will still produce some jitter in the sampling, mostly due to the timer interrupt. If this is not acceptable for your application, you will have to read the datasheet of the microcontroller and learn how to set the ADC to be “auto-triggered” from a timer.

The sampling frequency is calculated by the number of samples per time. I think you need to know how fast you're able to do this in your `loop()`.

For this you could fetch the start time, do a number of sampling, fetch the end time, and calculate the frequency.

This is the straight forward and simple way.

Please be aware the the turn-arund time for your sketch will include the transmission time for the result. This will be much longer than the time for the "pure" analog conversion.

Alternatively you can lookup the source code of Arduino's library function for `analogRead()`, get the data sheet, learn how the conversion works, and calculate the time.

But this is not at all easy.

BTW, `delay(1)` is by far not enough to "saturate" the serial transmission you do. You are using 9600 baud which gives about 960 characters per second. The biggest number you send is "1023" and the sequence "\r\n" is appended due to `println()`. So you are transmitting 6 characters which need 6 characters / 960 characters/s = 0.00625 s. These are a bit more than 6 ms, so you would `delay(7)` at least.

If you don't, the call to `println()` will do the delay if the transmitter's buffer gets full. Therefore your sketch will wait anyway. So you might consider to remove the delay.

And please, use the correct formatting the next time. ;-)

• THANKS...But still, I didn't get my answer. I want to calculate sampling frequency by calculation method not by just rough idea. So if you have any better method please share with me. Thanku Nov 20, 2019 at 6:57
• Well, my answer targets real-world aspects. For the detailed alternative see @EdgarBonet's answer. My hint in the second part is just a pointer to start with. -- It is not clear, for which situation you need to know the frequency. You should elaborate on this issue in your question. As your sketch is written, the frequency is by far determined by the serial transmission, not by the sampling itself. Nov 20, 2019 at 13:24