# Could analogRead detect a 60Hz sine wave?

I'm trying to create a 'zero cross detector' within my Arduino code. The analog input will be a 60 hz, 5Vp-p sine wave. I want to create a counter within the code to monitor every time the sine wave crosses zero (every 8.33mS), because my digitalWrite pulse is based on this timing. I had an external zero cross detector used as a digital input to my Arduino however it doesn't seem to work too efficiently. Is there an easier way to do this??? Thank you! I had an external zero cross detector used as a digital input to my Arduino however it doesn't seem to work too efficiently. Is there an easier way to do this??? Thank you!

You and we need to figure out why the digital method isn't working efficiently. Trying to use analog measurements to detect zero crossings is the most inefficient method I can think of.

Post what the external digital zero crossing circuit looks like, how it's wired to the arduino, and finally the code being used to count the zero crossings.

Lefty

I'm hoping your signal starts at 0V and goes to 5V. Assuming it does, there is an analog comparator on board, which you could set to a low threshold and detect when that is crossed. That will be much faster than doing analog reads.

Lefty,

yeah I guess I should have given more info regarding that topic. I have a few lab results I snapped pictures of.

One of my pictures is the zero cross detect. It's hard to visually see, but only every other pulse is a true zero detect because they RISE at the cross, while the middle ones FALL at the cross:

http://bayimg.com/haKfKAaeg

The I achieved this zero cross circuit over the internet with the following schematic:

http://bayimg.com/HaKfmAaEg

I didn't care too much about this because it's inaccuracy is only in the microseconds, however after my output pulse results I started to question my digitalRead and Zero cross detection circuit:

http://bayimg.com/hAKFlAaEG

I have the zero cross detection pulse going into my digitalRead pin.
My pulse delays are based on how many times zero cross has occurred.
The zero cross' purpose is for synchronization.

Below is my desired output pulse (only showing positive pulse, coding has both):

http://bayimg.com/IAkFAaAeG

Here is my code shown below:

``````///Arduino SCR firing revised

int zC = 7; /// zero detect input 7
int x = 0; ///phase angle firing reference
int y = 0; /// zero cross count
float z = 0; /// time delay in decimals
int SCR1 = 1;
int SCR2 = 2;
int SCR3 = 3;
int SCR4 = 4;

void setup()
{
pinMode (zC, INPUT);
pinMode (SCR1, OUTPUT);
pinMode (SCR2, OUTPUT);
pinMode (SCR3, OUTPUT);
pinMode (SCR4, OUTPUT);
}

void loop()
{
if (digitalRead(zC) == HIGH)
{
x = x++;
y = y++;
}
switch (y) {
case 1:
z = 4.16;  //90 degrees
break;

case 30: //250mS
z = 3.9;  //85 degrees
break;

case 60:  //500mS
z = 3.7; //80 degrees
break;

case 90: //750mS
z = 3.47; //75 degrees
break;

case 120: //1000mS
z = 3.23; //70 degrees
break;

case 150: //1250mS
z = 3.0; //65 degrees
break;

case 180: //1500mS
z = 2.77; //60 degrees
break;

case 210: //1750mS
z = 2.54; //55 degrees
break;

case 240: //2000mS
z = 2.31; //50 degrees
break;

case 270: //2250mS
z = 2.08; //45 degrees
break;

case 300: //2500mS
z = 1.85; //40 degrees
break;

case 330: //2750mS
z = 1.61; //35 degrees
break;

case 360: //3000mS
z = 1.38; //30 degrees
break;

case 390: //3250mS
z = 1.15; //25 degrees
break;

case 420: //3500mS
z = 0.92; //20 degrees
break;

case 450: //3750mS
z = 0.694; //15 degrees
break;

case 480: //4000mS
z = 0.462; //10 degrees

case 510: //4250mS
z = 0.231; ///5 degrees
break;

case 540: //4500mS
z = 0.0; ///0 degrees
break;
}

if (x == 1)
{
delay(z);
digitalWrite(1, HIGH);
digitalWrite(1, LOW);
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
}
if (x == 2)
{
delay(z);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(3, LOW);
x = 0;
}
}
``````

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

I apologize, that looks much cleaner.

``````    x = x++;
y = y++;
``````

I’m not sure this is valid. Try either:

``````    x = x + 1;
y = y + 1;
``````

or:

``````    x++;
y++;
``````

I'm not sure what your question is (a couple of posts up).

I'm right, I think.

``````i = i++;
``````

... is undefined.

http://c-faq.com/expr/seqpoints.html

Also: