I have to read a sinusoidal signal having (1V amplitude and 0.5 offset). I have a separate program in Labview to graph this serial data back into its original waveform shape. I want to read sinusoidal signals starting from 10Hz up to 150Hz. I have used the following code: However the sinusoidal signal has 'discontinuities'.
int inPin = A0;
int val = 0;
void setup()
{
Serial.begin(115200);
}
void loop() {
val = analogRead(inPin);
Serial.println(val);
delay(1);
}
reva23:
I have used the following code: However the sinusoidal signal has 'discontinuities'.
For establishing equal timings between samples I'd use the micros() function, and I'd keep the code running within the loop function only, never leaving loop():
int inPin = A0;
int val = 0;
void setup()
{
Serial.begin(115200);
}
unsigned long lastSampleTime;
void loop() {
while(1)
{
if (micros()-lastSampleTime>=1000)
{
lastSampleTime+=1000;
val = analogRead(inPin);
Serial.println(val);
}
}
}
Do you observe also 'discontinuities' with such timing logic?
even though an arduino is not the best machine to use when reading Sine signals , you should remove the delay , and should not use the "Serial.print" function , because it takes too much space and too much time . instead use the USART registers that will be much faster , then when using the ADC i also recommend not using the analogRead since this task is considered as a time sensitive application , also dis-activate the interrupts
and should not use the "Serial.print" function , because it takes too much space and too much time
Not sure what you mean by "too much space", but let's do the arithmetic.
11520 characters per second.
Up to four digits (0...1023) plus CR/LF = 1920 measurement reports per second.
At 150 Hz, that's a dozen samples per cycle and at 10Hz, 192 samples per cycle.
then when using the ADC i also recommend not using the analogRead since this task is considered as a time sensitive application , also dis-activate the interrupts
I would say at best this is muddled advice and at worst wrong.
the OP already knows the phase and the amplitude , he wants to image the wave .
that's why he said that it has "discontinuities" .
so it would be better to gain more accuracy and time , even if the main frequency of the wave is low .
Grumpy_Mike:
I would say at best this is muddled advice and at worst wrong.
well i thought since the "analogRead" function is used for many devices and get's a single pin number as input , then it has to waste some machine time , that's why i recommended to use the registers directly , though after reading the function in the "wiring_analog.c" file , it seems to almost be the same thing . so it's wrong not muddled
AWOL:
Not sure what you mean by "too much space", but let's do the arithmetic.
11520 characters per second.
Up to four digits (0...1023) plus CR/LF = 1920 measurement reports per second.
At 150 Hz, that's a dozen samples per cycle and at 10Hz, 192 samples per cycle.
Mr Nyquist should be satisfied.
1920 measurements
the priod is around 0.0066s for 150Hz
then he would get 12 reports or at best 13 reports per period
is that enough to picture a sine wave ? not really
gaining time per measurement even if it's 10 or 20 clocks that can get you to gain alot of accuracy
over the long run .
gaining time per measurement even if it's 10 or 20 clocks that can get you to gain alot of accuracy
over the long run .
Accuracy is the wrong word here, you do not gain it. What you do gain is the number of samples, this is known as over sampling because you are taking more samples than you strictly need. That is why I posted code that gathers 500 samples and then fires them off. This reduces the sampling error, but as long as the Nyquist criteria is met you should be able to recover all the relevant information held in the wave. We are just talking about a beauty contest here however.
Grumpy_Mike:
Accuracy is the wrong word here, you do not gain it. What you do gain is the number of samples, this is known as over sampling because you are taking more samples than you strictly need. That is why I posted code that gathers 500 samples and then fires them off. This reduces the sampling error, but as long as the Nyquist criteria is met you should be able to recover all the relevant information held in the wave. We are just talking about a beauty contest here however.
indeed (y) thank you Mike, 500 samples are guaranteed to cover the needs . though then they don't need to be in a loop (y) a single loop in the setup should suffice
amine2:
1920 measurements
the priod is around 0.0066s for 150Hz
then he would get 12 reports or at best 13 reports per period
is that enough to picture a sine wave ? not really
Psst, I already did the arithmetic, ^^^^ up there.
And referenced Nyquist.
Do you need to check when the signal is present or absent?
Do you need to get the frequency of the signal?
How clean is the signal?
If it's just a matter of counting the peaks on a nice sine wave, then implement a schmitt trigger and count the number of times the signal goes from below a low threshhold to above a high threshold in a fixed period. Maybe even use a hardware schmit trigger on a digital input.