Reading sine wave output using arduino analogread()

Hi. I’m trying to read a signal like this (attached below) on my arduino due. Actually I’m hoping to get the peak negative voltage values as seen on this graph.

I’ve tried using the analogread() function on arduino IDE, and outputted that onto the serial to read the values. For this, I’m using port A0 and just using a simple sample code to read the analog signal. However, I’m not getting anything meaningful as it seems that I’m just getting random values.

I’m not sure if this is the correct approach or if there is a better way of reading the signal? Any help is greatly appreciated ! Thank you.

Ok, but how often are you reading the signal and how often are you outputting onto serial? Makes a world of difference.
What options do you think there are for reading the analog pin?
If you want the negative values, search the forum for posts that show how to bias the signal so there is never a real negative voltage.
Paul

What is the time scale on your post photo?


Sorry, I did not realise that the photo has been cropped out.

Okay, 10us per division, you show about 10 cycles per division, so 1us per cycle. That means you have to sample at 1/1us*nyquist = 2.0 MHz. Can your ADC do that?

Also if you aren't oversampling you will miss peaks due to aliasing. So you might need a sample rate much greater than 2.0MHz.

Also, what hardware anti-aliasing filter will you use?

An Arduino M0 SAMD21 processor will sample up to 350ksps.

The Raspberry PI Pico can perform the A/D conversion in polling, interrupt & FIFO with DMA mode. The ADC conversion speed per sample is 2μs that is 500kS/s .

In my experience practical application cannot reach the "advertised" sample rates. So the above is likely a little overstated in actual usage.

Neither of the above is going to do a really good job capturing the waveform you've shown.

Thank you for your reply.

My adc? Do you mean the in built adc on the arduino due? I read online that the maximum sampling frequency of the arduino due is 9600Hz. Does that mean I won’t be able to read this signal using my ArduinoDue?

And sorry, I’m not really sure what you mean by anti aliasing filter. Do you mean one that could be constructed with an op amp?

I understand, what is completely obvious to us (because we learned it long ago) is still foreign to you.... no problem.

For your DUE:
First, know: time = 1 / frequency so your 9600Hz will sample 9600 times a second.

time = time between samples = 1 / 9600 = 104µs.
Looking at your scope, one division is 10µs so your due will be able to make an A/D conversion every 10 divisions on you scope. I don't see how that data will be useful.

Thank you for your reply.

If so, is there any way that I could possibly modify the output (like perhaps adding any components) so that I could make sense of it on my due?

I don't think so....... or maybe I have an idea but it will be more advanced than you are looking for.

But, before I go into that, what is your goal? What would you do with the data if you have it in a DUE array?

This depends on the nature of the signal. In particular, what information you need to extract from it. You are limited by orders of magnitude in the available sample rate, but since you are (or seem) focused on amplitude peaks, you could take spot samples for example. That would give you a very slow but believable quantity. The built in problem is that by its nature, averaging or low pass filtering defeats peak measurements. So for spot sampling, you would not filter at all. As you know, a peak detection function must have some defined fall off rate, which can be zero if necessary. But most applications of peak detection have a slow fall off to zero, in order to differentiate between this peak and the next one.

Peak detection is effectively an active filter, in the analog domain the difference between charge/discharge is provided by a diode or synchronized transistor switch. In fact, it makes an interesting question to ask, "have you thought about using a diode-RC peak detector, in front of the ADC?".

In another example, sometimes excessive frequency can be handled with digital prescalers or IF downconversion.

Else as far as I can see, if you need something in the range of a few microsecond response, then you would need the high speed ADC hardware.

Thank you for your reply.

Basically I need to measure the first negative peak voltage that shows up on the signal. Assuming that I’ve gathered an array of all the signals samples, I’d first sort them so I get the most negative value. Then, I would perform some calculations with the value and use it to compare if it is within safe limits. If not, my arduino would perform actions to reduce the gain of the amplifier. Something like that.

But looking and what you guys are explaining, I doubt I can get anything useful out of the signal.

IF your scope picture is representative of what you want to capture, We can see it is not a reoccurring signal. At least not on a basis we can see.
So you will need as high speed conversion that can run continuously until you can find the part of the signal you are wanting to study. Pretty tall assignment!
Paul

Thank you for your reply

I’m sorry, it’s kinda hard for me to understand your explanations. I’m not that advanced in signal processing or analogue devices, I’m sorry about that.

Do you mean that I’d use spot sampling and peak detection as my method to get info from the signals? But I’m not sure if that’s feasible as you mentioned if it’s slow. Because If the arduino is too slow in interpreting the signals, then I might miss out on the most negative peak, hence meaning I get a inaccurate reading? Again, sorry if I’m interpreting your words wrongly.

Then you are ONLY looking for negative signals. Invert your signal with a transistor and then you will have a positive you can look for.
Paul

Thank you for your reply

Hmm, what exactly do you mean by a high speed conversion? Is it like a hardware or chip that I can purchase? Would this chip’s sampling rate match that of the Arduino Due?

Sorry, but Even if I invert the signal, the signal frequency would still be the same right? Hence I would still be back to the issue whereby my arduino due isn’t fast enough to sample it

To do this you would need to create a opAmp based peak detector. With such a fast signal the design would not be trivial but is very doable. I think the biggest issue would be keeping random noise from providing false triggering.
Does you device know when your waveform occurs? i.e. like after something else is triggered.

Look at using a comparitor for a peak voltage. That could cause an interrupt and you could save the value that was read. You are not looking for a specific value, but only for something exceeding a set value. Right? The set value can be changed from time to time.
Paul

Thank you for your reply.

yeah I’m looking for a peak value, but the issue with this is that I’m not sure what range this peak value would be before looking at the signal beforehand.

Since this peak value could vary (with the gain of the amplifier and intensity of wave), I’m not sure if I can come up with an accurate and definitive set boundary value to use with a comparator.

So the comparator route is out. However as I mentioned you can build a peak hold circuit that can then be read by your DUE before resetting it.

see this link