I'm using proteus software to do microcontroller project of arduino uno.
the project is analog signal sampling of 1Hz sinusoidal signal and show the value through serial reading.
I do the programming using Arduino IDE and put *.hex file onto proteus to run it.
below are the code that i took from example with reading signal at pin A1.
I capture all the readings and plot using excel.
The amplitude was correct, but the signal frequency was not correct.
the input signal was set sinusoidal 1Hz, while the captured output was ~5Hz.
As per my understanding, arduino capability to capture analog signal should be faster than 1Hz,
which mean i will not encounter aliasing issue or any incorrect readings.
I have tried alternative code using timer0 with register manipulation, set prescaler to get precise "signal sampling rate", and call out analogread() function on timer interrupt.
the capture signal frequency was following the timer frequency, not the correct input.
Given the output even by using program from example,
I started to suspect the incorrect output was due to function analogread();
Can someone enlight me how to set the precise sampling rate of ADC for analog signal sampling?
Unfortunately, i didn't bring my physical arduino on my business trip.
i figured that i can use proteus to play with arduino in my computer which is useful for me at this moment.
yudhi:
actually what i aim is to be able to define precise sampling rate.
but i would say 10Hz is enough since the signal has frequency 1Hz.
Then why did you use
delay(1);
in your program ?
How do you want to be able to define the sampling rate ? Will it be fixed in the program or will there be user input ? If the latter then what sort of input do you envisage using ?
I took this code from arduino IDE sample code "AnalogReadSerial" and change only the pin to A1 as I configure the input signal.
I was thinking by modifying this delay, then this is how i define sampling rate.
in the code i wrote delay(1) which i thought sampling rate will be 1000Hz. CMIIW.
But the output signal that i got still not match with initial input signal frequency.
e.g. input signal frequency = 1Hz, the output signal frequency is 5Hz with delay(1).
I want to define the sampling rate fixed by program, not by user input.
You need to avoid the delay() function. One way is to use the technique of the blink without delay example from the IDE examples (File, Examples, Digital). Like so:
const byte analogInPin = A1;
unsigned long sampleTimer = 0;
unsigned long sampleInterval = 100; //100 ms = 10Hz rate
void setup()
{
Serial.begin(115200);
}
void loop()
{
unsigned long currMillis = millis();
if(currMillis - sampleTimer >= sampleInterval) // is it time for a sample?
{
sampleTimer = currMillis;
Serial.println(analogRead(analogInPin));
}
}
Make sure the serial monitor baud rate is set to 115200.