# Calculating frequency through analog pin

As a part of a project I need to read variable signal with arduino uno analog pin and I need to determine the frequency (the range is 0-100 Hz). I am making the project in Proteus and the signal comes from a Sine generator or a Pulse generator. The frequency has to be printed on lcd screen every 5 seconds. Already Googled everything that came to mind, but couldn't find what I needed. Can anybody help me?

So why do you think an analog input is appropriate?

Hi,

arduino frequency counter

What model Arduino are you using?

Thanks.. Tom...

Because that's what's given in my homework Also because I need to read the voltage and determine maximum and minimum values the voltage takes.

I have tried googling that also, but couldn't find what I need. I wrote the code myself at first, where I try to measure the period of a cycle and frequency equals the inverse of that, but everytime the screen updates the frequency is completely different and most of all, plain wrong.

This is all it says in Proteus: Simulino Uno ATMEGA328P V3.0, VSM DLL Model[AVR2.DLL]. I don't know if it's at all useful.

Hi,
Its sounds like you are working under Covid conditions.
Your best bet is to physically build your project and work with hardware.

Can you post your simulation schematic?
Is your input signal, AC either side of gnd, or a pulsed input from gnd to a positive voltage?

Tom...

So the idea is we ask more and more questions to painfully extract all the minute but important details of the project.

Hi,
@Paul_B yes.
@Elizabeth182 can you post your code please?

Tom...

Hello
You can use an analogPin as digital input pin.

How would I use that to determine frequency?

You would be better off using...
frequency = 1000/(t2-t1)

Question:
Is your input sine wave always >0 or does it go +/- ?

Always >= 0

Does this also have to be on the LCD?
Do you have the lcd working in Proteus?
Do you know the clock on the ATMEGA328P? Not sure its necessary.

I'm sure there are many ways to approach this. My first thought is:
1)
2) Read analog input in a loop until the voltage starts to reduce. The reading before is the max. Store the max in a variable and the millis() in a variable.
3) Repeat for the min.
4) Repeat #1 but add in the LED on at median = (max - min)/2 + min
5) Repeat #2 but add in the LED off at median.

1. While you are watching for the max, use its millis() to "trigger" and LCD write at seconds.

Now you will have to "fudge" the 0 - some low number frequencies. As at 0 hz the millis() will be infinite.
You can likely gain points by displaying < 0.2 Hz when the max to min (or min to max) is greater than 5 seconds (i.e. your display update).

I don't know what you were taught but I would first draw a graphical sine and note the different things going on. Then I would make a flow chart of the above. Then code.
Many folks think flowcharts are passé but I find them a time saving tool. Both for writing code and troubleshooting code.

good luck

Hi,
Can you please post the exact contents of the project description/requirements that the teacher gave you?

Thanks.. Tom...
PS A picture of the assignment sheet would be fine.

Might as well.

Any competent teacher setting an assignment specifying an Arduino will be monitoring these fora in any case and will have already recognised that this enquiry relates to their assignment, so you might as well come clean with the full details, just so you stop wasting people's time.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.