# Set up sample rate of arduino

**URL:** <https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460>\
**Category:** Programming\
**Created:** [August 14, 2021, 2:32am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460 "2021-08-14T02:32:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![webergaia](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@webergaia](https://forum.arduino.cc/u/webergaia)\
**Post date:** [August 14, 2021, 2:32am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/1 "2021-08-14T02:32:21Z")

</div>

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](https://Serial.print)(",145,680");  
[//Serial.println](https://Serial.println)();  
tempo = millis();  
}  
Best Regards

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 14, 2021, 2:53am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/2 "2021-08-14T02:53:22Z")

</div>

Hello, do yourself a favour and please read [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) and modify your post accordingly (including code tags and necessary documentation of your ask).

* * *

As the [analogRead() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) 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…

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [August 14, 2021, 6:10am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/3 "2021-08-14T06:10:37Z")

</div>

> [@webergaia](#):
>
> Serial.begin(9600);

Fix that too.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 14, 2021, 6:54am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/4 "2021-08-14T06:54:25Z")

</div>

@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 😉 See [About the Installation & Troubleshooting category](https://forum.arduino.cc/t/about-the-installation-troubleshooting-category/847458).

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.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [August 14, 2021, 7:11am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/5 "2021-08-14T07:11:08Z")

</div>

**Not reading lazy posts any more.**

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [August 14, 2021, 7:47am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/6 "2021-08-14T07:47:19Z")

</div>

@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](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)  
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,

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [August 14, 2021, 9:43am UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/7 "2021-08-14T09:43:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 14, 2021, 2:28pm UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/8 "2021-08-14T14:28:21Z")

</div>

> [@webergaia](#):
>
> analogIN = analogRead(A0);

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 12, 2021, 2:28pm UTC](https://forum.arduino.cc/t/set-up-sample-rate-of-arduino/895460/9 "2021-12-12T14:28:22Z")

</div>

Closed on December 12, 2021, 2:28pm UTC

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