I'm currently working on a project to detect the frequency of a sine wave from an ardiuno nano, here's my current code.
const int frequencyPin = A7; //Input Pin will be pin A7
const int oneSecond = 1000; //Meassured time will be milliseconds
int frequencyPinState = LOW; //intially the pin will be powered off
int frequnecyPinState = 0;//the intital read state of the pin
int previousFrequencyPinState = LOW;
unsigned long timer = 0;
double frequency = 0;
void setup()
{
Serial.begin(57600);//bauds per second is set to 57600 bps
pinMode(frequencyPin, INPUT); //Declared Pin 6 as Input
}
void loop()
{
frequencyPinState = digitalRead(frequencyPin); //Reads State of Input Pin
if (frequencyPinState == LOW) //Creates a previous state of the pin on the last read value
{
previousFrequencyPinState = LOW;
}
if (frequencyPinState == HIGH && previousFrequencyPinState == LOW) //Pulses Counter
{
previousFrequencyPinState = frequencyPinState;
frequency++;
}
if (millis() - timer > oneSecond) //Time Counter
{
timer = millis(); //Resets Time
Serial.println(frequency); //Prints Frequency
frequency = 0; //Resets Frequency
}
}
my problem is that when i plug it into the sine wave generator it isn't printing out the results in Hz like i expected it to, any advice on what could i potentially fix?
The code will run so fast that it will probably detect 1000 "highs" for each cycle.
You need a reliable method of detecting the same position in the cycle, the peak, zero crossing or wherever, then you can time that.
One way to do this is to run the (rectified and clipped if it's larger than 5v) waveform through a schmitt trigger and detect the rising edge coming out of that using an interrupt.
What's the actual frequency and what results are you getting?
You might have a hardware problem... You're running an analog AC signal (unknown amplitude?) into a digital input.
And, depending on how much power you're getting from the signal generator you could damage the Arduino with the negative half of the waveform.
You'll probably get better results if you [u]bias[/u] the Arduino input, then subtract-out the bias in software and look for negative-to-positive zero-crossings.
I have solved the problem, it was actually the sine wave generator that i was using and now i'm getting more accurate results. the results update every time i change the frequency and the voltage. now my problem is that when i do change, i noticed that it has a 10-15% difference with my inputs and the results on the serial monitor screen.
Example: 200 Hz, 3.2 Volts are my settings
229 Hz at 0...3427mV
229 Hz at 0...3505mV
229 Hz at 0...3574mV
229 Hz at 0...3491mV
229 Hz at 0...3452mV
229 Hz at 0...3432mV
229 Hz at 0...3427mV
What could i do to make this results more accurate?
Did you bias the signal mid-voltage. Bad idea to just feed 3.2volt AC (9volt peak/peak) into an Arduino pin.
The negative halves of the sine wave will clamp at about -0.6volt.
You could damage the pin if the generator can deliver enough current.
229 Hz at 0...3427mV
If this is the printout, then the code used is not what you have posted.
Leo..