Go Down

Topic: Problem with serial printing values at high sampling frequencies (Read 339 times) previous topic - next topic


Hi, I'm trying to sample data from two Wheatstone bridges at a sampling frequency of 1KHz. The data would be logged using an external program (putty) for post processing on Matlab. I'm using a Teensy 3.1/3.2 board. I have an interval timer to ensure that the sampling rate is a constant 1KHz. The problem I'm having is with the Serial.print function. Once in every ~7,000 samples, the transmitted data is mashed up( an example is attached below). Introducing a delay solves the problem but reduces the sampling rate to < 1KHz. Any suggestions on how to reliably print data at high sampling frequencies?

Code provided below

Code: [Select]
// Wheatstone Bridges
static int WB1Pin = A5;
static int WB2Pin = A4;

volatile int16_t WB1Val; // Variables for storing readings
volatile int16_t WB2Val;

IntervalTimer myTimer; // Teensy specific interrupt function

void setup() {
  myTimer.begin(printReadings,1000);  // Interrupt once every millisecond


void loop() { // Not doing anything in the loop

void printReadings(){
  WB1Val= analogRead(WB1Pin); // Read from the two Wheatstone bridges
  WB2Val = analogRead(WB2Pin);
  Serial.print(WB2Val); // Print values
  Serial.println(millis()); // Printing time in milliseconds to keep track of sampling rate

Things I've tried:
Increasing the baudrate: I experimented with a range of baudrates and I settled on 250,000.
Introducing shorter delays: The most stable delay I could find reduces the sampling rate to 500Hz
Using the elapsedMillis function instead of an interrupt: Didn't work

Addendum: In the attached picture, note that the timer is on the last column from the left. It sampled at 1KHz until line 4 and then skipped about 2 seconds. That's the problem I've been having.

Thanks in advance!


I fixed the problem and I figured I should provide an update for future Arduino users.

Solution: Keep the sampling frequency low if possible. If you really need a microcontroller for high frequency sampling, use an an advanced chip (like a beagle-bone).

If you absolutely have to use an Arduino, use the native logger. External loggers (like putty) sometimes hang, which leads to jumps in data collection. The serial monitor works just fine. Note that there's a storage cap for the serial monitor.


Go Up