-2
\$\begingroup\$

will this code work to display the frequency of input signal of zero crossing detector?

#define SECONDS 2.0

int count = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Zero Cross Test");
  attachInterrupt(0, cross, RISING);
}

void loop()
{
  delay(SECONDS * 1000);
  noInterrupts();
  float hz = count / SECONDS / 2;
  count = 0;
  interrupts();
  Serial.print(hz);
  Serial.println("Hz");
}

void cross() {
  count++;
}
\$\endgroup\$
  • \$\begingroup\$ Where are you stuck? Do we assume that you are measuring mains frequency? You can either (1) measure the time between two zero-crosses or (2) count the number of zero-crosses in a given time. Without a schematic we can't answer the second part. There's a schematic button on the editor toolbar. Update the post rather than put the missing info in the comments. \$\endgroup\$ – Transistor Mar 23 '18 at 7:19
  • \$\begingroup\$ Yes, i want to measure mains frequency and display it in serial monitor. i have updated my post. I hope for your help. thank you. \$\endgroup\$ – Ryadiff Mar 23 '18 at 8:58
  • \$\begingroup\$ Actually, what is tour requirement? You didn't post anything regarding schematics and merely asking that whether that code will work or not? What if you have a correct working code and wrong circuit connections in your requirement? \$\endgroup\$ – Dhans Mar 23 '18 at 10:49
  • \$\begingroup\$ @Ryadiff: The code is a mess. Use the code formatting button on the editor to tidy it up. Remove blank lines (unless to separate sections) and add comments, if required. \$\endgroup\$ – Transistor Mar 23 '18 at 11:48
0
\$\begingroup\$

You can use H11AA1 optocoupler to execute a concept of Zero Crossing Detection. It will turn on the phototransistor for every half cycle of the main AC signal. By detecting this signal using an MCU such as Arduino, we can find the frequency of the signal. Here is a simple workout of Arduino code for zero cross detection.

#define ZCD 2
#define LED_PIN 13

volatile byte counter=0;

void setup()
{
  pinMode(ZCD,INPUT_PULLUP);
  pinMode(LED_PIN,OUTPUT);
  digitalWrite(LED_PIN,LOW);
  attachInterrupt(digitalPinToInterrupt(ZCD),zerocrossing,RISING);
}

void loop()
{
}

void zerocrossing()
{
  ++counter;
  Serial.println(counter);
  if( counter == 60)
  {
    counter=0;
    digitalWrite(LED_PIN,!digitalRead(LED_PIN));
  }
}

The program is developed for blinking an LED once for every half of a second.You can develop this program further according to your requirement.

As per chris'suggestion, I have tried to expand my answer for detailing. Here is the circuit of Arduino and H11AA1 for zero cross detecting. What I have made here is, simply connecting an output of optocoupler to the Arduino's interrupt pin which will sense an output from optocoupler for every zero-cross detection of the input supply. Then, interruption is requested to the controller and controller will stop other activity and call's the function 'zerocrossing' in the program. Then the function increments the counter value and blink an LED if the value reaches 60. Since, for a single AC cycle, two zero crossing events happens, LED will blink for every half of a second. You can print the counter value in a serial monitor to see the frequency value. I have developed the program by assuming that the frequency value is 60Hz.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ This is a relatively poor answer; you've dumped in some code that avoids a few mistakes in the original, but it doesn't solve the problem of this question but rather a very different one, and you neglect to explain how your changes correct issues in the original. \$\endgroup\$ – Chris Stratton Mar 23 '18 at 19:22
  • \$\begingroup\$ Actually when I posted this answer question is in theory..But that was not clear too. After my post, he might changes the question's content..Just see the time of the answer and question... \$\endgroup\$ – Dhans Mar 24 '18 at 0:36
  • \$\begingroup\$ Not true. History shows your answer was posted after the code in the question. \$\endgroup\$ – Chris Stratton Mar 24 '18 at 0:58
  • \$\begingroup\$ Might be...If it is like that I am sorry. I have seen the code after I posted my answer. Ok...Let me know how to improve my answer!!! \$\endgroup\$ – Dhans Mar 24 '18 at 3:10
  • \$\begingroup\$ Thanks @Chris and Dhans . The problem is solved. Code worked. I just wanted to be sure if there was error or not before i try, since i am new in arduino. \$\endgroup\$ – Ryadiff Mar 24 '18 at 13:02

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.