Store a sample of data via analogRead

I start working at a project that uses low frequency to transmit data (around 125 kHz). So the only way to read this data is by using analogRead.

If I read the date as follows:

   void setup()
   {
       Serial.begin(9600);
       pinMode(A0, INPUT);

    }

   void loop()
   {
       int val = analogRead(A0);
       Serial.print(val);
   }

The data is correct and has exactly the form i need.
Sample:
112 113 110 109 112 107 107 114 109 110 15 0 0 122 23 0 0 0 0 0 0 0 112 11 0 0 0 0 0 0 111 121 117 65 7 0 0 116 42 0 0 0 0 0 0 0 115 112 113 50 0 0 0 122 17 0 0 0 0 0 0 34 112 114 113 5 0 0 107 109 107 57 2 0 0 109 116 119 40 0 0 0 111 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 114 115 119 119 116 117 118 113 113 117 31 0 0 117 44 0 0 0 0 0 0 0 111 39 0 0 0 0 0 0 0 106 108 111 39 0 0 0 115 23 0 0 0 0 0 0 11 112 115 112 5 0 0 115 45 0 0 0 0 0 0 0 119 119 118 53 2 0 0 115 119 116 45 0 0 0

It is clear where i have a group of 1 or a group of 0.

The problem occurs when i try to store this data. I do not understand if the data is stored too fast or is another problem.

   int message[600];      // i also tried to store as a byte (char)
   int counter = 0;

   void setup()
   {
       Serial.begin(9600);
       pinMode(A0, INPUT);
       
       for(int i=0; i<600; i++)   // i know this is useless
           message[i] =0;

    }

   void loop()
   {
       message[counter++] = analogRead(A0);

       if(counter > 400)
       {
              for(int i=0; i<counter; i++)
                      Serial.print(message[i]);
              counter = 0;
              // i tried here to use delay and also to set up a timmer1 with interrupt();
       }

   }

Sample:
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 99 102 105 109 111 113 112 112 110 108 107 106 106 107 108 109 111 111 112 111 109 108 106 105 105 107 109 110 111 111 111 109 108 108 107 107 107 108 108 109 108 109 109 109 108 107 108 106 107 108 108 108 109 109 108 108 107 106 106 105 106 107 107 107 108 107 107 107 106 105 105 105 106 107 109 111 111 110 109 107 106 105 105 106 106 106 107 108 108 107 107 106 105 104 104 104 106 107 109 109 109 108 107 105 104 104 104 104 105 107 108 110 109 107 106 104 103 103 103 105 106 107 108 108 108 107 105 104 104 104 106 107 106 110 109 108 107 106 105 104 104 104 105 105 106 107 107 108 107 106 104 104 104 105 105 105 106 106 107 107 106 105 103 103 102 103 105 108 109 109 109 108 107 105 105 105 104 105 106 106 107 107 108 106 106 105 104 103 103 105 106 107
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

It seems i receive large groups of 1 and 0 and is totally different from the first sample.
I do not really care about the Serial (i know it is possible to be a problem here). I need to store a signal that it repeats himself continuously and make some operations over the sample. I tested the operations with the first sample in visual studio c++ and it work perfectly. The operation i need to perform is to identify a large group of 1 as the header and after that groups of 1 or 0 (this is not very important to detail here). But for this i need to store a sample of the signal to analyse and i need a way.

Thanks!!!

I don’t think that the analogRead() can sample at 125KHz

-Malhar

125kHz is kind of fast for the ADC.

But it's a XY-problem. What is that 125kHz device you want to communicate with?

septillion:
125kHz is kind of fast for the ADC.

But it's a XY-problem. What is that 125kHz device you want to communicate with?

Yep. The XYproblem

-Malhar

The device i use is a homemade RFID device that can work in both ways (to transmit data from tag to the reader and vice versa). The problem is when i try to capture the signal from the reader to the tag (i used the voltage to transmit the signal). The protocol and the hardware part works perfectly (tested with an oscilloscope).

This is not so important at this moment. I repeat i just need to store the data from the sample A (data that seems i can read with the analogRead) and do some stuff on it.

Maybe, OP can use assembly to do it faster.

-Malhar

It is XYproblem but I think the problem is interesting per se.
To Y: Did you compare the readings you posted to real data? If I had to guess I would say the second code would sample much faster than the first one. BUT if the signal is really 125 kHz it is impossible to get this result without some huge flaw. Isn't it 125 Hz instead? I don't know default Arduino settings but ATMega datasheet says "Up to 76.9 kSPS (Up to 15 kSPS at Maximum Resolution)". So you MUST be sampling on much lower speed than you claim the signal has. Let's say the signal has 125 Hz. Than the first sample would mean you are sampling at around 1kSPS (probably limited by the speed of Serial) and the second is at around 10kSPS (limited by ADC speed). In this case the results are right in both cases - you just sample much faster when using the buffer and fill it with the header you mentioned before the signal you are interested in comes. Try add delay(1); after the analogRead and see if it helps.
To X: if you are interested in counting 1s and 0s, why you use ADC? Why not digitalRead? You say the signal is slow. It's fine, any level shifter will get it to 5V for digitalRead to work as needed!

If you only want to see a high or a low then analogRead is kind of useless. Why not use a digital pin?

And you sample free running, no synchronization or flow control what so ever. So the data you have is useless, it has no relation to time…

Btw, the code you posted can never give 1 100 120 12 33 13 etc as an output. It will be 697846597834591837987431935490615378314763945347506840214086 (aka, no spaces…) So what was the real code :wink: