Press enter to see results or esc to cancel.

Tips For Sampling High Speed Analog Signal in Arduino/Microcontroller

In many Arduino projects, we need to sample high-frequency signals such as an audio or equivalent signal. Think about a situation we want to sample the 50Hz AC signals and want to see the signal in Arduino serial plotter in real-time. In such cases, we make a few common mistakes that can be easily avoided if we understand a few things properly.

What we normally do is sample the analog value and then print the value through the Serial terminal. that’s a very bad idea to sample analog signals and I have seen people working with kilo-hertz level signals and trying to analyze the signal in this way. What they see is not a real signal. They see a different signal that is known as aliasing in the sense of sampling. This happens because of serial printing delay. Consider the baud rate of your serial communication is 9600 bits per second. If we want to send a single byte like character ‘X’ to the PC it will take around 1ms for a 9600 baud rate. 8 data bit, 1 start bit, and 1 stop bit. So in total of 10 bits for each byte. 9600 bit per second. So 10 bits take around 1ms. That’s why it is not a good idea to print analog values just after sampling.

The figure below is an example of aliasing. A solid high-frequency signal is our main signal to sample. Due to Serial printing, our sampling rate is low, as a result, we are getting the sample like the dotted signal. This phenomenon is known as aliasing and we are making this type of mistake frequently

The same thing happens for people working with Line following robots. They read analog data and after that print a lot of serial log into the terminal and expects everything will work fine. So it is very important to remember how to use serial printing in time-critical code.

In such cases, the easiest solution is to use a buffer for reading the sample.

So the steps are as follows.

  1. A buffer to hold all of your analog values. Such an array of 100 value int. Int value[100]
  2. Read the analog value in a for loop and fill up the whole buffer of 100 integers.
  3. Then create another for loop for printing out all of your data from the buffer

In this way, we can sample signals at a higher speed in the range of 8KHz which is the sampling frequency of Arduino Uno. Here is the example code that I explained so far. I hope the blog will help. Happy coding!!

//Take a buffer so that you can store your sample in the buffer
int analogBuffer[500];

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  //First save the sample in a buffer using loop.
 // Do not do other task during sampling
  for(int i = 0; i<500; i++)
  {
    analogBuffer[i] = analogRead(A0);
  }
  //After taking sample do whatever you want to do with the sample
  //Print or postprocess anything
  for( int i = 0 ; i<500; i++)
  {
    Serial.println(analogBuffer[i]);
  }
//  int sensorValue = analogRead(A0);
//  Serial.println(sensorValue);
}