Set up sample rate of arduino

Hello Everyone!

I have been inputing a sine wave in arduino and I'll make the analog-digital conversion. How can I set up the sample rate to 9600 samples or more? I know that I will change the clock frequency but I don't know how to do that. Initially, I have 4400 samples per seconds, but I want 9600 samples.

// Variáveis Globais
float analogIN = 0.00;
unsigned long tempo = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // clear prescaler bits
ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2); // 128 - Prescaler
//analogReference(EXTERNAL);
}

void loop() {
// put your main code here, to run repeatedly:
analogIN = analogRead(A0);
ADC = (analogIN*5.0)/1023;
Serial.println(ADC);
//Serial.print(",145,680");
//Serial.println();
tempo = millis();
}
Best Regards

Hello, 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).


As the analogRead() - Arduino Reference documentation states

On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

So if you use analogRead and want 9600 samples per second your board likely won’t have the time to do anything else…

Fix that too.

@webergaia, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (or advice on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Not reading lazy posts any more.

@webergaia
I have removed your duplicate topic on the same subject and moved this one to a more appropriate place.

Please take some time to read How to get the best out of this forum
Please go back and post your code as instructed and please only post a question once, taking care to select an appropriate section of the forum.

Thanks,

Drop the floating point, converting to volt. It doesn't add any precision. Print the integer coming from the ADC reading.

Don't use analogRead().

Would 9615.38... samples per second be OK? You can get that with a prescale of 128 (125 kHz ADC clock) and selecting 'free running' mode. Every 13 ADC clock cycles (9615.38... Hz) you will get an ADC Complete interrupt. See: ADIE (ADC Complete Interrupt Enable).

In the 'ADC Complete' ISR, grab the data from the ADC and shove it in a circular buffer. In loop(), send data from the buffer whenever it is not empty.