Frequency counting by network frequency

Hi! I want to count the number of pulses in the network (50 Hz) and build my clock from this frequency. I am unable to do this. I read many articles, but did not find any information. I provide my code:

#define F_CPU 16000000UL
int numberOfPeaks; // Number of peaks
int Seconds = 0; // Number of seconds by number of peaks

void setup() {

    PORTC &= ~0b00100000; // Setting to zero (INPUT) pin digital 19 (A5)
    Serial.begin(9600);     // setting the port speed (bps, baud)
  
}

void loop() {


  int y = PINB;
  if (y % 32 == 0) //pin A5    
  {
    
    numberOfPeaks ++;
    
     if (numberOfPeaks % 27346 == 0)
    {
      Seconds ++;
     
    }
  }
  }

I have "+" connected to pin A5, and "-" to "GND". I'm giving somewhere 3.2 volts. It seems that the counter has already started working, but what a disappointment I was when I stopped supplying power, and the counter still counts my peaks and seconds as well. Why? I don't know. Ideally, the number of "1" on pin A5 should be counted and from this number of "1" seconds should be counted, which will be as close to real time as possible.

50Hz is not the most stable way. An RTC like DS3231 is a better choice.

What schematic do you use?

You must make an interrupt to count the numberOfPeaks. Every times the pin rise, you count.

Basic schema with normal transformer not a switched one.

R1 value in according to the used voltage. TO-INT => interrupted pin with pull-up high. D1 to protect the opto coupler led against reverse voltage more then 6V. You can place the diode anti parallel with the opto led too.

I am using the following schema:
Wiring diagram for connecting a transformer to an arduino A5 pin.pdf (15.8 KB)

Which is it, port C or port B? One has to be wrong.

That is a test for 5 zero bits, not one.

digitalRead would have been less error producing.

There is more then one thing wrong.

If the upper connection the voltage is higher then the underside, then there is max 1,88V to GND - 0,6V from the diode is 1,28V (is that enough?) on the GPIO. On the other side if the wave is opposite, there is -18V on the connection. This is not the correct way. At least there must be a diode between the transformer and R1. Better is use the opto coupler. Never more then VCC on your GPIO.

And what will the diode between the transformer and R1 change?

In fact 50 Hz is surprisingly precise in most countries, I didn't know that myself, but then learned that it is specifically compensated so even if at some moment frequency shifts up by some dozens ppm, it will be slowed down so pulse count over a year would be very precise. But that still needs proper schematics and code :slight_smile:

1 Like

I decided to use bitRead. I bring my code:

#define F_CPU 16000000UL
int volatile numberOfPeaks;
int Seconds = 0;
void setup() {
PORTC &= ~0b00100000; //Setting to zero (INPUT) pin digital 19 (A5)
Serial.begin(230400);
}

void loop() {
byte y = bitRead(PINC, 5);
if (y == 1) {
numberOfPeaks ++;
    //Serial.println ("numberOfPeaks");
    **Serial.println (numberOfPeaks);**
    //Serial.println (numberOfPeaks);
    if (numberOfPeaks % 27346 == 0)
    {
      Seconds ++;
      numberOfPeaks = 0;

      //Serial.println ("Seconds");
      //Serial.println (Seconds);

    }
  }
  //Serial.println(digitalRead(14));
  //Serial.println (digitalRead(12));

  //Serial.println(row_excel);




}


The line of code that I highlighted in bold does not work. I cannot look at the data on the number of logical units in the COM port. What's my mistake?

Same approach as 60Hz digital clock:

Arduino Digital Clock Synchronized by the 60Hz Power Line : 8 Steps (with Pictures) - Instructables

Maybe counting while a bit is high, does not count peaks?

Tried to use while instead of if. It did not help, it does not output anything to the COM port.

Obviously, I'm not able to communicate with you.

So y is never 1.

Good luck with your project.

Once true, sadly no longer.

The RTC will also keep time over power outrages, so there's that.

Plus easy fun, and no messing no matter how safely with mains power.

a7

Unfortunately, with the advent of so-called renewable energy using wind and solar, the power distribution system creates glitches in the sine wave every time they switch from one source to another. Our clocks that use the 60Hz for timing gain or loose a second or more in a week.

1 Like

Yes but not when there are lots of PV instalations in the street. My good old alarm clock works fine by rain, night, clouds, fog, ... By good sunny days there is a difference until 5 minutes a day.

DCF or NTP are more accurate then net frequency. My clocks must be correct over a minute, hour, day. Not over a whole year and all the other days incorrect.

Why is there a negative power needed. With a diode the resistors uses only one wave and use half the power.

Indeed, that's a not so nice surprise! Well, now I know :slight_smile:

If you're seeing 5 minutes/day clock drift, it's due to the clock's sensitivity to power line transients, not drift in the fundamental frequency of the grid.

Really bad things happen to the grid if generators are not kept synchronized.

2 Likes

I think the issue that I see regularly here is that suburban areas are often cross-fed from multiple secondary substations. Likely, the substations were placed and growth did not happen as planned.
Just 2 miles down from my subdivision, multiple restaurants and a shopping center exist with 1 supermarket. My UPS(s) all chirp daily as the substation a mile away switches feeds to deal will the restaurant start-up and shutdowns when folks are getting ready for bed.

During the feed switching, a break of a second can be observed, long enough for the UPS to switch. Counting AC sine waves "could" pickup a few extra pulses or drop a few cycles. Like interest, little things add up.

Thanks. I guess my interest here is what would be the requirements to make an accurate clock based on power line frequency and what would be the accuracy of such a clock.

The issue regarding photovoltaic installations in post #16 sounds like some high frequency transients that might reasonably be filtered in the analog cycle detection circuitry or, perhaps, some sanity check in the cycle counting software that rejects cycles that aren't nominally one cycle period after the previous.

The issue you're suggesting in post #20 is a complete line power dropout on the order of a second. This might call for some sort of logic that "coasts" on the microcontroller clock during such dropouts.

Long term power outages remain a problem, of course.