Arduino NANO based 14 MHz frequency counter



















Wanted to select four 10 MHz quartz crystals from a handful, to make a SSB filter. 
They need to be 40 Hz of each other. My HB frequency counter has a resolution of
100 Hz and is useless for the task.

A simple frequency counter with  about 5 Hertz resolution should do the job.
This counter should measure 14 MHz reliably. An Arduino NANO can be used to
put together such a simple frequency counter.





Time interval generation using internal ceramic resonator of  Arduino was not preferred.  An external  10 MHz  quartz crystal oscillator  was used instead. Though  gate based quartz crystal  oscillators  are not very stable, a 74HC4060 oscillator / divider  was used  with  a 10 MHz  quartz crystal, for simplicity and lower component count. 
A 10 MHz OCXO was not available 😞




14 MHz frequency counter








External signal ( of frequency F ) goes to base of Q1 via C. Q1 shapes the signal suitable for 
digital  circuit.  Shaped signal clocks half of 74HC74 , configted as a T flip-flop. Q output, at
pin 5, has half the input frequency, with 50% duty cycle. This signal is connected to digital pi
5 of NANO. Pin 5 is external input  (T1) for the internal 16-bit Timer/Counter1.




" bc - An arbitrary precision calculator language" was used to calculate time intervals.

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
scale=40
10000000/16384
610.3515625000000000000000000000000000000000
610.3515625/2
305.1757812500000000000000000000000000000000


10 mHz is dived by 16384 with 74HC4060.
Resulting   610.3515625 Hertz, clocks other half of 74HC74 to produce 305.17578125 Hertz signal
with 50% duty cycle. Total division is 32768.


305.17578125 Hertz signal is connected to digital pin 2,  INT0 .

The magic number 3.2768 used in sketch was arrived at using <bc>.



..
..
..
..
scale =20
1 / 305.17578125
.00327680000000000000
.0032768 * 1000
3.2768


305.17578125 Hertz  has a time period of 0.0032768 seconds.  A two second counting interval
should produce one Hertz resolution. More than two second counting interval is used to ensure
one Hertz resolution.
Frequency is calculated after 1000 INT0 interrupts.  1000  interrupts produce a counting interval
of  3.2768 seconds.



Here is the sketch.
/////////////////////////////////////////////////////// start of sketch  //////////////////////////////////////////////////////////////


/*

 Arduino  NANO XTAL selector

 It is a simple 14 MHz frequency counter.

 Input signal is connected to digital pin 5 (T1).

 T1 is external input for internal 16-bit Timer/Counter1.

 Unknown frequency F is divided by two ( with one half of  74HC74 ).
 F/2 with 50% duty cycle, is fed to T1.

 The 16-bit counter of Arduino NANO ( with 16 mHz clock ) can measure
 7 mHz without error when input signal has 50% duty cycle.

 This simple frequency counter can measure 14 mHz. Accuracy is not known
 but crystals can be grouped within 40 Hz with it.



 10 MHz quartz XTAL oscillator is used for timing.

 10 MHZ is divided by 16384 with 74HC4060, then by two, with other half
 of  74HC74 .
 Total division of 10 MHz is 32768 to get 305.17578125 Hz timing signal.

 Schematic is at https://blogspot/vu2nil
 305.17578125 Hz is fed to  INT0 ( digital pin 2 ) as the interrupting
 signal.



  Display connections:

 <--- 16x2 LCD Display -->       Connected to

    -LED  - Display pin-16  -      GND ( MCU/power source)
    +LED  - Display pin-15  ---- 1K  resistor-----+5 Volt
 Contrast  - Display pin-3   ----  See schematic
      VCC  - Display pin-2   ---- +5 Volt of power source
      VSS   - Display pin-1   ---- GND
      R/W   - Display pin-5   ---- GND

       RS    - Display pin-4   ----  pin9 of NANO
        E     - Display pin-6    ---- pin8 of NANO
      DB4  - Display pin-11  ---- pin7 of NANO
      DB5  - Display pin-12  ---- pin6 of NANO
      DB6  - Display pin-13  ---- pin4 of NANO
      DB7  - Display pin-14  ---- pin3 of NANO
   
   
*/




#include <avr/interrupt.h>
#include <LiquidCrystal.h>


unsigned   long          countTimer1;
                               
volatile     uint16_t     overflow1;     // Timer/Counter1 overflow counter

volatile     float           freq;               // Frequency



volatile     uint16_t     count;            // number of INT0 interrupts

boolean                     Ready;


char  freqStr[12];

char  line1[17];


// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 4, 3);

void setup_Timer1() {  // 16-bit Timer/Counter1 setup

    //  PD5 ( T1 ) is set as input
    DDRD = DDRD  &  0b11011111;
 
    // normal mode of operation
    TCCR1A = 0b00000000;

    // Input frequency on T1 pin. Clock on rising edge
    TCCR1B =  0b00000111;

    // enable interrupt on Timer/Counter1 overflow
    TIMSK1 =  0b00000001;

}



ISR(TIMER1_OVF_vect) {  // counts overflow of Timer/Counter1
 
    overflow1++;
 
}




ISR(INT0_vect) {

    // Read current 16 bit value in Timer/Counter1
    countTimer1 = TCNT1;
 
    count = count + 1;
    if( count >= 1000 ) { // 3.2768 second counting
   
     
       countTimer1 = countTimer1 + ( 65536 * overflow1 );
       freq = ( countTimer1 / 3.2768 ) * 2;
     
       //freq = ( countTimer1 / 3.2766 ) * 2; // trim
       Ready = true;
     
       // clear three variables
       overflow1 = 0;
       count = 0;     
       TCNT1 = 0; // reset 16 bit Timer/Counter1
     
    }

 
 
}   // end of INT0 ISR



void setup() {

      // enable serial output
      Serial.begin(9600);

      // set up the LCD's number of columns and rows:
      lcd.begin(16, 2);

      lcd.setCursor(0,0);
      lcd.print("Arduino NANO");
      lcd.setCursor(0,1);
      lcd.print("crystal selector");

 
      Serial.println("Arduino NANO crystal selector");


      cli(); // disable interrupts

      // External interrupt INT0 setup
      DDRD = DDRD  &  0b11111011;   // D2 is set  as input
      EICRA = 0b00000011;           // Interrupt on rising edge
      EIMSK = EIMSK || 0b00000001;  // INT0 triggers interrupt

      setup_Timer1();

      sei(); // enable interrupts

}     // end of setup()
 
 


void loop() {


  if( Ready == 1 ) {
 
      Ready = false;
 
   
      dtostrf ( freq,10,1,line1 );
      lcd.clear();
      lcd.print( "Frequency " );
      lcd.setCursor( 0,1 );
      lcd.print( line1 );
   
      lcd.setCursor(11,1);
      lcd.print("Hertz");
 

 
      Serial.print ( "Frequency = ");
      Serial.print ( line1 );
      Serial.println ( " Hertz" );
 
 
  } 

 
} // end loop()



/////////////////////////////////////////////////////// end of sketch  //////////////////////////////////////////////////////////////







A crystal oscillator was made using  circuit from EMRFD.




Crystal oscillator








































The XTAL oscillator was made using ugly construction on a copper foil. Here is its picture.


An IC socket is used to take in different crystals.
XTALs of different frequencies were tried to find the range of this oscillator.


Following crystals produced stable oscillation...

3.5795 MHz , 4 MHz , 5MHz , 6 MHz , 6.144 MHz ,  8 MHz , 10 MHz  and 12 MHz ,


18.432 MHz and 20 MHz also produced stable oscillation. These two were checked with another HB frequency counter.


A 14.31818 MHz quartz crystal on the crystal oscillator produced this reading on serial monitor.

Frequency = 14319157.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319154.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319154.0 Hertz
Frequency = 14319159.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319159.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319159.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319157.0 Hertz
Frequency = 14319158.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz
Frequency = 14319156.0 Hertz


Average reading was taken as  14319158.0 Hertz.




A 4 MHz quartz crystal on the crystal oscillator produced these readings
on serial monitor.

Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.5 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz
Frequency =  3999363.0 Hertz
Frequency =  3999362.3 Hertz


Average reading was taken as  3999363.0 Hertz.





A 3.58 mHz ceramic resonator on the crystal oscillator produced these readings
on serial monitor after 20 minute warm up.



Frequency =  3507384.8 Hertz
Frequency =  3507383.0 Hertz
Frequency =  3507383.0 Hertz
Frequency =  3507385.3 Hertz
Frequency =  3507386.0 Hertz
Frequency =  3507385.3 Hertz
Frequency =  3507384.0 Hertz
Frequency =  3507386.0 Hertz
Frequency =  3507390.3 Hertz
Frequency =  3507393.3 Hertz
Frequency =  3507390.3 Hertz
Frequency =  3507384.8 Hertz
Frequency =  3507379.8 Hertz
Frequency =  3507383.0 Hertz
Frequency =  3507386.0 Hertz
Frequency =  3507388.5 Hertz
Frequency =  3507390.3 Hertz
Frequency =  3507392.0 Hertz
Frequency =  3507397.5 Hertz
Frequency =  3507403.0 Hertz
Frequency =  3507408.5 Hertz
Frequency =  3507414.0 Hertz
Frequency =  3507421.3 Hertz
Frequency =  3507427.5 Hertz
Frequency =  3507435.5 Hertz
Frequency =  3507442.0 Hertz
Frequency =  3507453.0 Hertz
Frequency =  3507460.5 Hertz
Frequency =  3507465.3 Hertz
Frequency =  3507470.8 Hertz
Frequency =  3507472.0 Hertz
Frequency =  3507467.3 Hertz
Frequency =  3507469.5 Hertz
Frequency =  3507472.5 Hertz
Frequency =  3507477.5 Hertz
Frequency =  3507478.0 Hertz
Frequency =  3507478.0 Hertz
Frequency =  3507476.3 Hertz
Frequency =  3507480.5 Hertz
Frequency =  3507484.3 Hertz
Frequency =  3507482.5 Hertz
Frequency =  3507483.0 Hertz
Frequency =  3507477.5 Hertz
Frequency =  3507473.8 Hertz
Frequency =  3507470.3 Hertz
Frequency =  3507464.0 Hertz
Frequency =  3507456.8 Hertz
Frequency =  3507452.5 Hertz
Frequency =  3507445.8 Hertz
Frequency =  3507444.0 Hertz
Frequency =  3507444.0 Hertz
Frequency =  3507431.8 Hertz
Frequency =  3507427.5 Hertz










For selecting crystals, display on serial monitor is adequate. External 12 Volt supply is not
essential for  serial monitor, but  reading on  serial monitor changed a little with and without the
external 12 Volt.

With external 12 Volt, a regulated 8 Volt goes to Vin pin of NANO.  Hopefully this results
in more stable reading ! So while selecting crystals, 12 Volt supply was used.



External 12 Volt is required for LCD. LCD was added so that it can be used as a standalone
frequency counter.



Forty two  10 MHz crystals were in my junk box. Some were larger ( HC-49/U ? ) than the
rest ( HC-49/s ? ).

This picture shows their relative size.













































10 mHz crystals were marked ( put inside small zip bags with sample number ) starting with larger crystals, as xtal01, xtal02 .......... xtal41, xtal42.


Average frequency of oscillation were entered for each sample in a text file xtals.

$ cat xtals
xtal01 9996535
xtal02 9995982
xtal03 9996661
xtal04 9995454
xtal05 9995499
xtal06 9996603  
xtal07 9995301
xtal08 9995450
xtal09 9996335 
xtal10 9995559
xtal11 9996234 
xtal12 9997406 
xtal13 9995563 
xtal14 9995564
xtal15 9995774 
xtal16 9996834 
xtal17 9996311
xtal18 9995522     
xtal19 9995578
xtal20 9995463
xtal21 9995806
xtal22 9995517
xtal23 9995407
xtal24 9996632
xtal25 9996981
xtal26 9995926
xtal27 9997357     smaller
xtal28 9997277     smaller
xtal29 9997390     smaller
xtal30 9997688     smaller
xtal31 9997498     smaller
xtal32 9998108     smaller
xtal33 9997348     smaller
xtal34 9997473     smaller
xtal35 9997634     smaller
xtal36 9997389     smaller
xtal37 9997563     smaller
xtal38 9997942     smaller
xtal39 9997461     smaller
xtal40 9997445     smaller
xtal41 9997631     smaller
xtal42 9996911     smaller





The file xtals was sorted on 2nd field


$ sort -k   2   xtals   -o  sorted


$ cat sorted
xtal07 9995301
xtal23 9995407   
xtal08 9995450
xtal04 9995454
xtal20 9995463   
xtal05 9995499
xtal22 9995517   
xtal18 9995522         
xtal10 9995559
xtal13 9995563 
xtal14 9995564
xtal19 9995578   
xtal15 9995774 
xtal21 9995806   
xtal26 9995926
xtal02 9995982
xtal11 9996234 
xtal17 9996311 
xtal09 9996335 
xtal01 9996535
xtal06 9996603  
xtal24 9996632   
xtal03 9996661
xtal16 9996834 
xtal42 9996911     smaller
xtal25 9996981   
xtal28 9997277     smaller   
xtal33 9997348     smaller
xtal27 9997357     smaller
xtal36 9997389     smaller
xtal29 9997390     smaller
xtal12 9997406 
xtal40 9997445     smaller   
xtal39 9997461     smaller
xtal34 9997473     smaller
xtal31 9997498     smaller
xtal37 9997563     smaller
xtal41 9997631     smaller
xtal35 9997634     smaller
xtal30 9997688     smaller
xtal38 9997942     smaller
xtal32 9998108     smaller




These four are 49 Hertz of each other.
xtal08 9995450
xtal04 9995454
xtal20 9995463   
xtal05 9995499



These four are 19 Hertz of each other, best group!     
xtal10 9995559
xtal13 9995563 
xtal14 9995564
xtal19 9995578


These four are 42 Hertz of each other.
xtal33 9997348     smaller
xtal27 9997357     smaller
xtal36 9997389     smaller
xtal29 9997390     smaller



These four are 53 Hertz of each other.
xtal40 9997445     smaller   
xtal39 9997461     smaller
xtal34 9997473     smaller
xtal31 9997498     smaller



The crystals   xtal10 ,  xtal13 ,  xtal14 and xtal19 can be used to build a XTAL filter.
As of May 13, 2020, no filter was made  😊


73 !    VU2NIL












Comments

Popular posts from this blog

Signal generator using Si5351 and Arduino NANO

Direct Conversion Receiver using sub-harmonic mixer

Crude low frequency counters with Raspberry Pi Pico