I'm trying to use the digital pin to calculate frequency, I can read up to about 35 kHz but it tops out there. Is there anyway I can improve the read speed? I need to get to 1.6 Mhz, is this even possible with the Arduino Uno I have? Here's my code: (modified from the Button example)
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int Count = 0;
int mult = 0;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(115200);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
Count++;
}
}
lastButtonState = buttonState;
if (Count == 10000) {
mult++;
Count = 0;
Serial.println(millis());
Serial.println(mult);
}
}
You are welcome.
14 minutes. That was quick.
If you are looking for a frequency counter that works very well just search in the Arduino forum for "arduinoaleman" or "frequency counter with LCD display" or "frequency counter preamplifier".
I have posted a project, that is based on serveral other projects. I have included the ideas of many projects and my code is reduced to the very minimum. So I do not use any libraries - you do not really need them. My code is straight forward and will give you an easy to read output.
My code was developed to be a frequency counter only.
I have analyzed the FrequCnt library and other stuff to optimize the code.
I have also posted a design for a preamplifier that works from 2 HZ to over 10 MHz with a 100mV input or higher (my oscilloscope does not go higher than that). The input might be even lower -however i did not test it.
OK, i admit that for higher frequencies I might have used a gate time of 100ms instead of 1000ms.
However, if you are not in a hurry, the gate time is good enough to get a frequency measurement every second.
The accurancy of the measurement does NOT depend on my code. It depends only on the accuracy of your quartz and the gatetime which you can easily change in the code.
For testing you need nothing than a signal source. You will see some basic output on the serial monitor. The output is NOT formatted. Only when using a LCD display you will have formatted output that will automatically show you if the output is HZ, KHZ ot MHz.
And you can extend the display accurancy from 4 digits to more digits. Below 1 kiloherz this does not make sense if you do not extend the gatetime used for measurement.
It costs nothing to copy my code into your Arduini IDE envirinment - and if you do not like it - just throw it away - and in case you do not like it, please tell me why.
*If you want more information on the frequency counter project just have a look at my threads * (arduinoaleman)