Frequency Counter

What is the best way to turn a 5Vp-p sine wave into something an ATMEGA328P can use for a frequency counter input? Can I run the AC through a diode and get a 50% duty cycle DC voltage? Someone else suggested using an LM339, but I'm not familiar with comparators enough to know how that works, or how to implement it...

It's got a built-in comparator. That detects passing a threshold.

http://www.gammon.com.au/forum/?id=11916

Thanks! So, input 5VAC on pin12 and ground pin 13? Does that cause it to trigger every time it crosses 0V?

Bear in mind you can't put negative voltages into the Arduino. So filter out negative voltages with a diode. Then have the reference pin (13/D7) have (say) 2.5V on it from a voltage divider, and then trigger on when the comparison pin (12/D6) crosses that threshold.

@nick very interesting link (learned something today :) any info on how much faster a comparator is compared to ADC?

robtillaart: @nick very interesting link (learned something today :) any info on how much faster a comparator is compared to ADC?

Datasheet says 500ns typical at 4V supply.

Thanks Mark!

MarkT: Datasheet says 500ns typical at 4V supply.

What page did you find that on Mark? I thought I knew where it was but couldn't find it today.

BTW, compare that to 104 ┬ÁS for a typical ADC conversion and you see the comparator is much faster.