Loop frequency significantly reduced while use serial.print in side loop

Hello
I want to make band pass filter for 20Hz to 1000hz.

As per my knowledge for 1000hz digital filter sampling rate should be 2 time of Max frequency i.e. 2000Hz.

I am monitoring sampling frequency by generating digital output pulse in side loop and connected to external oscilloscope.

Normally getting loop frequency i.e. sampling rate around 2500hz

But when i added serial.print inside loop
For checking performance of filter by serial plotter it's loop frequency reduced to 500hz.

Therefore not able to validate my audio filter.

Is any way to validate filter or increase loop performance with serial.print

Serial.print becomes a blocking function when the transmit buffer is full, the function does not return until there is space in the buffer for what is being printed. You can increase the Serial baud rate, or print fewer characters.

What type of arduino are you using? what baud rate are you using?

On many Arduinos, Print has a 64 byte buffer and when it’s full the call to print() becomes blocking until enough bytes are available to save the whole message to the output buffer. So basically if the rest of the code runs super fast, the throttling factor is proportional to your baud rate.

What baud rate have you set for the serial monitor?

Can you reduce what you are writing?

I am using Arduino Uno.

And printing

Serial.print(xn);
Serial.print(" ");
Serial.print(yn);
Serial.prinln();

Where xn is float value analog read raw signal and float yn is filtered signal.

Bourd rate is 2000000

Remember that the UNO has no floating point hardware. It is all done in software. Eight bits at a time. This could take significant time for a lot of floating point use.

Yes
I will make xn (analog read) to integer an try.

One hypothesis : In the other code (the one not printing) if you were not doing anything with the calculated value then it’s possible that the optimizer got simply rid of all the math and the filtered value altogether. Once you started printing it, the compiler could not throw that code away and suddenly you started paying the right cost for the floating point operations,

Actually Adriano board not having analog output 0-5 V pin .

Otherwise I will take output of filtered signal yn by analog output pin and compare with xn input signal to evaluate low pass filter performance.

Therefore useing serial print to compare xn and yn in serial plotter.

Is anyway I can take output filtered single and compare with input signal with house losing the loop frequency.

Thanks in advance

You could use PWM as a proxy for the calculated filtered data

Ohh yes
But Filtered output is Float yn

And in input of analog write is required in between 0-255.

analogWrite(3, yn)

Can you help how to scale yn in range of 0-255

What's the range of yn ?

the map() function can help you bring yn within 0 255. it won't be perfect but will give you a proxy of what was calculated. PWM being generated in hardware, you would not take a toll like you have with Serial

Assuming yn is in interval [0, maxYN] then you could do

int pwmValue = map(yn, 0, maxYN, 0, 255);
analogWrite(3, pwmValue); // D3 is a PWM pin on UNO

if you add a small RC circuit on D3, you could even generate some sort of "stable" voltage from the PWM or your scope can probably extract information

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.