I am working on a project to calculate line frequency meter by reciprocal counting with Arduino. I am trying to print the the result on a 16*2 LCD but it is not displaying anything. It just gets on and stuck there. I got the project details from this website Line Frequency Meter Based On Reciprocal Counting

I modified the stock program to interface the LCD but it's not working. Here is the code I am using:

//To read and display the Line frequency 
//using Reciprocal counting method
//clk for 'Time Counter'  is generated with 'Tone' function 
//'Time counter' is the physical counter T1
// 'EVENT Counter' is a software counter
#define Fin 6      // input pin for Line freq sig
#define clk 7      // input to ' Time ' counter
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
boolean FState;   // status of the 50 Hz Signal 
int countS;       // Signal count
int countC=0;     //  Clock count
int LineFreq;     // Result

void setup() {
  tone(8, 5000); // generates ref freq of 5000 Hz at Pin 8
  pinMode(Fin, INPUT); // set the Fin pin as INPUT
  pinMode(clk, INPUT); // set the clk pin as INPUT
  lcd.begin(16, 2);

void loop() {  
  lcd.print("Frequency of signal");       
  // to avoid partial cycles, first detect the rising edge
  while(digitalRead(Fin)) {
    //wait for '0' state of the Signal
  //here '0' state
  while(!digitalRead(Fin)) {
    //wait for '1' state
  //this is the rising edge of the Fin
  //counting can start from here
  // clear the TIME Counter T1
  //count 50 of the Line freq signals
  while(countS<50) {
    while(digitalRead(Fin)) {
      //wait for '0' state
    //here '0' state
    while(!digitalRead(Fin)) {
      //wait for '1' state
    // here '1' state
    // here one cycle is just over
    //increment the event counter
  //here 50 Signal cycles over
  // read the contents of T1 & compute frequency
  countC = getCount1();
  LineFreq = 25000000/countC;

  //send to display
  lcd.print(" Hz");
  // clear counter
  // repeat

//The following code is obtained from on-line articles in the public domain
void counterStart1() {
 // hardware counter setup,  for  the 16-bit Timer1 Timer/Counter
 TCCR1A=0; // reset timer/countern control register A
 TCCR1B=0; // reset timer/countern control register B
 TCNT1=0; // initialize the counter value to 0; this register holds the current count
  // set timer/counter1 hardware as a counter; it counts events on pin Tn (Arduino pin 5)
 TCCR1B = TCCR1B | 7; // Counter Clock source = pin Tn (Arduino pin 5) , start counting now
 // 7 in binary is 0111; OR-ing will set CS10,11,12 to 1's

// the following code is obtained from on-line articles in the public domain
// Sub routine  to get the current count
unsigned int getCount1() {
 unsigned int count; // this variable returns the current count from the counting register
 TCCR1B = TCCR1B & ~7; // Gate Off  / Counter T1 stopped, 
 // this operation clears the bits (CS10,11,12)
 count = TCNT1; // read the counting register
 TCCR1B = TCCR1B | 7; // re-start counting by resetting the bits (CS10,11,12)
 return count; // return the retrieved count to the calling function

I am getting a blank screen but the line at very first of the code "Frequency of signal" is printing accurately.

What is wrong with the code and what changes do I need?

Thanks in advance!

  • Which is it? Not displaying anything, or only displaying "Frequency of signal"? – Gerben May 3 '18 at 18:39
  • I fixed your indentation, as it was nearly impossible to read. – Gerben May 3 '18 at 18:43
  • If the latter, it seems like you signal on pin 6 isn't there, or is too weak. Ending up with your code waiting for 50 transitions that just aren't there? – Gerben May 3 '18 at 18:50
  • It is only displaying frequency of the signal only, which is written at the top of code. After that nothing is working. I checked the pin 6 signal And it was working fine. – Joy Mukherjee May 4 '18 at 0:33

Your variables are int, which on an Arduino is a 2-byte signed type by default, and has a range of -32768 to 32767.

When you execute:

LineFreq = 25000000/countC;

is the result of the math a 2-byte int, or a 4-byte long? Regardless, you are possibly assigning a 4-byte value to an int.

You can declare LineFreq to be type long, and also append an L to the end of the constant value 25000000 to force long math, so that you have:

long LineFreq;
LineFreq = 25000000L/countC;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.