Hi,
So I was basically trying to design a frequency counter which can measure frequencies as high as 433 MHz. For this I have a known clock and the unknown frequency. IDK what's wrong in the code but the output always comes out to be "inf". I'd be grateful if any of you could help me with this.
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const int pulsePin = 8; // Input signal connected to Pin 8 of Arduino
const int clockPin = 12; // CLock Input
int pulseHigh; // Integer variable to capture High time of the incoming pulse
double frequency; // Calculated Frequency
long counter=0; //Counts number of high pulses
double clkTime = 0.0001; //Known time period (here 1 MHz)
double unkTime; //Unknown time period
void setup()
{
pinMode(pulsePin,INPUT);
pinMode(clockPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" Frequency Counter ");
delay(2000);
}
void loop()
{ do
{if (clockPin == HIGH)
{counter = counter + 1;
}
}while(pulsePin == HIGH);
unkTime = counter*clkTime;
frequency = 1/unkTime;
lcd.setCursor(0,1);
lcd.print(frequency);
lcd.print(" Hz");
counter = 0;
delay(500);
}
Sorry, my bad. I posted the wrong version. 1 MHz is supposed to be 10^-6 seconds. I was trying to change the value to see if anything helped. I did change it to digitalRead but nothing has changed.
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const int pulsePin = 8; // Input signal connected to Pin 8 of Arduino
const int clockPin = 12; // CLock Input
int pulseHigh; // Integer variable to capture High time of the incoming pulse
double frequency; // Calculated Frequency
long counter=0; //Counts number of high pulses
double clkTime = 0.000001; //Known time period (here 1 MHz)
double unkTime; //Unknown time period
void setup()
{
pinMode(pulsePin,INPUT);
pinMode(clockPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" Frequency Counter ");
delay(2000);
}
void loop()
{ do
{if (digitalRead(clockPin) == HIGH)
{counter = counter + 1;
}
}while(digitalRead(pulsePin) == HIGH);
unkTime = counter*clkTime;
frequency = 1/unkTime;
lcd.setCursor(0,1);
lcd.print(frequency);
lcd.print(" Hz");
counter = 0;
delay(500);
}
You should give up the idea to measure 433 MHz with software only.
A Raspi moves you farther away from the hardware, it is slower in accessing hardware,
unless you want to learn how to write kernal modules and even then you will not get giga samples.
There could be a counter in the Raspi hardware that is capable of counting external 1 GHz signals,
but I'm not familiar with the SOC used for the Raspi.
There could be a counter in the Raspi hardware that is capable of counting external 1 GHz signals,
but I'm not familiar with the SOC used for the Raspi.
You can’t treat 433 MHz as if it were a few KHz, it requires special handling, techniques and test equipment, plus a whole lot of skill.
The normal technique is to use a heterodyne method where you mix the incoming signal with a fixed accurate frequency to obtain a waveform containing the sum and difference of the two frequencies. Then you filter out the upper frequency normally and then measure the lower one.
This is much easier to say than to do. Given the accuracy required for the gating signal then I can’t see an Arduino having any part to play.
Buy an off-the-shelf frequency counter. Fastest, probably cheapest and definitely most reliable way of getting your measurement done.
If you must involve the Arduino, get a frequency counter that offers digital communication and as such can be connected to an Arduino.
Then I do hope you try to measure a clean signal that's coming in through a wire, rather than picking up a frequency with an antenna or so... because in the latter case you have a lot of tuning and noise filtering to do as well.
Hey, thanks for your suggestions.
I'm aware of the heterodyne method using the intermediate frequency. I've studied that but I wanted to solve this issue just by software. Nonetheless, I guess the table top counter idea should work. I mean I can basically mimic the working using a few decade counters to divide the frequency by 10 to bring it down to a few kHz and then try finding precision.
If you divide by 100 (so 2 decade counters in series - and assuming those counters are fast enough etc) you get to 4.33 MHz. That is a frequency you can handle with an Arduino, but your timing resolution is still limited by the 16 MHz clock.
Problem: the fastest decade counters Digikey lists do 125 MHz.
The 8-bit counter MC10E016FNG can do 900 MHz, two of them get you down to 6.7 MHz. They cost over USD 10 a piece on Digikey. Or one of these counters, and a 98 MHz 12-bit 74HC4040D counter (much cheaper at USD 0.40 each). That gives you a 4.5 MHz signal to count for much lower price.
This all is assuming a perfect world, I never worked with these frequencies directly but I do know that you will have to deal with all kinds of parasitics in your board, high frequency noise, and many other issues. At least this frequency is low enough that as long as you keep it all close enough together the speed of light doesn't come in play. Over about 70 cm and you have a complete wave in your wire, meaning one end of the wire is a full wave behind the other.
I did some web surfing and found some decade counters that operate at about 1.3 GHz for instance MC100EP016A. So idk where is this going but I do need a compact solution for this. Weight is a constraint I can't ignore. If I buy a frequency counter off the shelf, that's heavy. Thus, I thought of building my own but didn't know the frequency I had to tackle would be so high.
I was wondering if something like incrementing a counter would be of any help if I used while {Rpi.GPIO.wait_for_edge(GPIO.FALLING) }
?
Just a random thought, would love the hear you out.
You need a high frequency prescaler something like this. http://www.cel.com/pdf/datasheets/upb1507gv.pdf
Set it to divide by 256 which will bring the 433 Mhz down to around 1.5 Mhz which is within the ability of an Arduino to just measure.
Wont be all that accurate though.
A real frequency counter is the best option.
And: what is the signal, really? What kind of accuracy and precision do you need to get out of your frequency counter?
Data sheets of 433 MHz devices (at least LoRa, I suppose other protocols are similar) talk about bandwidths in the tune of 10-100 kHz. That's the fourth and fifth digit, so to measure that you need at least 6-digit precision. I'm not sure an Arduino can measure the frequency of a 1.5 MHz signal down to the 100 Hz level, let alone the 10 Hz level - as that's probably what you're going to be looking for.
Plutonian:
Hi,
So I was basically trying to design a frequency counter which can measure frequencies as high as 433 MHz.
To what accuracy and precision?
What minimum signal amplitude?
What maximum signal amplitude?
This is normally done with a broadband input stage to amplify, then a switchable prescaler, and then
a gated counter fed from a stable reference oscillator (10MHz is common). This is no beginners
project - at UHF frequencies, knowledge of RF techniques is required. Upto 50MHz or so things are
easier and CMOS processors and logic chips can be employed directly for signals at logic level.
These days one might employ a software defined radio module as a counter, you'd need software to
hunt for the strongest signal.