# Speed limitations in basic data transfer

**URL:** <https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401>\
**Category:** Programming\
**Created:** [July 6, 2023, 8:51pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401 "2023-07-06T20:51:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptnz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baptnz/32/983583_2.png) [@baptnz](https://forum.arduino.cc/u/baptnz)\
**Post date:** [July 6, 2023, 8:51pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/1 "2023-07-06T20:51:55Z")

</div>

Very basic question, but I can't reconcile what I've been reading in other posts with what I see in my own experiments.

In an introductory teaching lab, I would like to illustrate the ability of Arduino (Uno) to acquire analog signals, convert them to digital, and send them to a computer. This should also show the limitations (ADC resolution with 10 bits, as well as maximum sampling / transfer frequency). To this aim, I hooked up a signal generator set to a sine wave, 200mV peak to peak, offset by 300mV to be sure that the signal is always positive, and I vary the frequency.

The signal generator is connected to A0 on the Arduino, and I monitor the signal on the Serial Monitor of the Arduino IDE. It works fine: I can see the discrete levels due to the ADC resolution, and for low frequencies I get a sine wave that looks just like the one on the oscilloscope. However, I am puzzled with the fact that after about ~300Hz, the sine wave already shows significant degradation due to poor sampling rate. I would have expected this to happen at a substantially higher frequency.

I changed the baud rate for the Serial connection, assuming this was the problem, but even at the maximum option available (2000000 bauds) input signals above ~300Hz don't seem to be well resolved in time. I wonder what I'm doing wrong, and how I should make sense of the results, and possibly improve the situation (though it's not critical: I don't actually need high transfer rate, but I would like to understand where the limitations come from).

Basic code, slightly edited from the AnalogReadSerial example:

```arduino
void setup() {
  Serial.begin(2000000);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1); // delay in between reads for stability
}

```

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [July 6, 2023, 8:54pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/2 "2023-07-06T20:54:27Z")

</div>

I moved your topic to an appropriate forum category @baptnz.

In the future, please take some time to pick the [forum category](https://forum.arduino.cc/categories) that best suits the subject of your topic. There is an " **About the \_\_\_\_\_ category**" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in [the " **How to get the best out of this forum**" guide](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

---

<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:** [July 6, 2023, 9:02pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/3 "2023-07-06T21:02:49Z")

</div>

Hello @baptnz ,

> [@baptnz](#):
>
> ` delay(1); // delay in between reads for stability`

What do you think would be unstable without this? How do you think having a delay makes it stable?

What is the maximum possible number of samples you can get when there is a delay of at least 1ms between them?

What is the minimum number of samples you need to take to get a half decent digitisation of a sine wave?

How does all the above relate to your results?

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [July 6, 2023, 9:03pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/4 "2023-07-06T21:03:25Z")

</div>

Umm. With a 1 ms delay, plus the actual execution time, you're likely not going to sample much faster than 800, maybe 900 samples per second. For 300 Hz, that's barely above Nyquist.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [July 7, 2023, 5:45am UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/5 "2023-07-07T05:45:34Z")

</div>

here is an example using an ESP32 reading ADC every 100uSec and displaying using DAC

```cpp
// ESP32 DAQ
// ESP32 timer interrupts 10000times/second to read ADC and output to DAC

// from ESP32 Sine Wave Example https://deepbluembedded.com/esp32-dac-audio-arduino-examples/

/*
* LAB Name: ESP32 Sine Wave Generation Example
* Author: Khaled Magdy
* DeepBlueMbedded 2023
* For More Info Visit: www.DeepBlueMbedded.com
*/

#include <driver/dac.h>

// Timer0 Configuration Pointer (Handle)
hw_timer_t *Timer0_Cfg = NULL;

volatile int counter = 0;

// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)
void IRAM_ATTR Timer0_ISR() {
  // read ADC output to DAC
  unsigned int adc=analogRead(35)>>4; // adc/16 - 12bits to 8bits
  dac_output_voltage(DAC_CHANNEL_1, adc);
  counter++;
}

void setup() {
  Serial.begin(115200);
  
  Serial.println("\nESP32 DAC sine wave");
  // Configure Timer0 Interrupt 10000/second
  Timer0_Cfg = timerBegin(0, 800, true);
  timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
  timerAlarmWrite(Timer0_Cfg, 10, true);
  timerAlarmEnable(Timer0_Cfg);
  // Enable DAC1 Channel's Output
  dac_output_enable(DAC_CHANNEL_1);
  analogSetWidth(12); // ADC 12bit resolution
  analogSetClockDiv(1); // fastest converion time
}

void loop() {
  static long timer = millis();
  if (millis() - timer > 1000) {
    Serial.println(counter);
    counter = 0;
    timer = millis();
  }
}

```

scope displays (blue is from signal generator yellow is DAC output)  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/3/8/4/384ba55250f12a80ef61031e230fdaaeec786be9.jpeg)

serial monitor displays samples/second

```arduino
10006
10006
10006
10005
10005
10005
10006

```

---

<div class="post-metadata">

**Author:** ![baptnz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baptnz/32/983583_2.png) [@baptnz](https://forum.arduino.cc/u/baptnz)\
**Post date:** [July 8, 2023, 9:10pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/6 "2023-07-08T21:10:54Z")

</div>

Good point – I didn't have the code with me so I copied over the AnalogSerial example and kept this line by mistake in my post. I'd commented it out in my experiments, but you're right: I did not think enough of what a decent digitisation would require – I just saw a clear degradation after 300Hz and that seemed strange. After doing more experiments, and checking the actual timestep between measurements, it makes more sense.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 8, 2023, 9:38pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/7 "2023-07-08T21:38:41Z")

</div>

If you use the default settings on a 5V Arduino, the resolution of the ADC is about 5 mV/step. You can use another ADC reference, such as the internal 1.1 V reference, to get about 1 mV/step.

You need to individually calibrate the internal reference as the accuracy is +/- 10%, but it is very stable. Of course, input signal voltages above the ADC reference are converted to 1023.

---

<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:** [January 4, 2024, 9:39pm UTC](https://forum.arduino.cc/t/speed-limitations-in-basic-data-transfer/1145401/8 "2024-01-04T21:39:02Z")

</div>

Closed on January 4, 2024, 9:39pm UTC

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