1

I am trying to get a sampling rate of over 10k Samples per second as well as send each sample over a serial line. Taking a sample and putting it on the serial line each time took too long. I am using Processing 3.3.6 for datalogging into a csv file. I am unable to achieve more than 6k samples per second even at lower prescaler value and high baudrate. What am I doing wrong? Here is my arduino code.

int val[2000];
void setup()
{
  Serial.begin(250000);

  //ADCSRA = 0;             // clear ADCSRA register
  //ADCSRB = 0;             // clear ADCSRB register
  ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // clear prescaler bits
  //ADCSRA |= bit (ADPS0);                               //   2  
  ADCSRA |= bit (ADPS1);                               //   4  
  //ADCSRA |= bit (ADPS0) | bit (ADPS1);                 //   8  
  //ADCSRA |= bit (ADPS2);                               //  16 
  //ADCSRA |= bit (ADPS0) | bit (ADPS2);                 //  32 
  //ADCSRA |= bit (ADPS1) | bit (ADPS2);                 //  64 
  //ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2);   // 128
  //pinMode(A0, INPUT);

}

void loop()
{
  for(int i=0; i<2000; i++) {
    val[i] = analogRead(A0);
  }
  for(int i=0;i<2000;i++){
    Serial.print(val[i]);
    Serial.print('x');
  }

}

P.S I am new to arduino.

--edit1>> I am using Arduino Mega 2560.

2
  • You don't state what Arduino board you are using. Perhaps you should look for a board with native USB, such as a Arduino Zero, Teensy 3.2 or the like.
    – jogco
    Apr 5, 2018 at 9:18
  • Don't post two questions in quick succession about the same topic. It just annoys people, and doesn't increase your chance of a good answer.
    – Nick Gammon
    Apr 5, 2018 at 11:04

1 Answer 1

1

Probably the serial port is too slow:

Assuming 250 kbits/s, is (including 3 start/stop bits, maybe 2 in your case) in about 250000 (bits/s) / 11 (bits/byte)= about 22 KBytes/s Assuming each sample has 3 or 4 digits and an 'x' is added (which is 5 characters), 22 (KBytes/s) / 5 (digits/sample) = about 5-6 Ksamples/s you can transfer, which is what you experienced.

So you have to increase the speed for the serial port.

5
  • Thanks for the reply. I have a doubt. One symbol in my case is two bytes per sample, with header bits giving 22 bits per sample. Since I have an additional 'x', that amounts to 33 bits in all. I want to send some 10k such samples or 330kbits per sec(> 250kbits per sec, hence the issue?). So merely increasing the serial.begin parameter should show an improvement? Apr 5, 2018 at 9:20
  • You mean that one symbol is 2 bytes (digits?) Since it is sent as ASCII. You can check easily by REDUCING the speed and see if you have less speed, than you know that (at least) the serial port is the bottleneck. But increasing takes just a few seconds to try either. Apr 5, 2018 at 9:23
  • Well, I increased the rate to 1000k and I am still at 6k samples. Apr 5, 2018 at 9:38
  • Than the only reason I can think of is that either the procesing of getting the sensor values, or the processing of the print (some kind of overhead) takes too long ...you can try timing measurements to see what takes up most time Apr 5, 2018 at 10:02
  • 1
    Read this yaab-arduino.blogspot.se/2015/02/…
    – MatsK
    Apr 5, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.