Is this work for frequency counter

Hello everyone
i’ve been trying to find out the frequency from a two-wire output speed sensor that generating Alternating-Current (AC)

However, the code that i wrote is not working. could anyone help me please.
i am very appreciate it.

here’s the code:

#define frqPin 1  //analog pin 1 for frqpin
const int sensorPin = 1; //sensor input is analog pin 1
long timeStart, timeStop;
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.println("Start measuring");
  
}

void loop()
{
  while (millis() < 5000) {
   int val;
      val=analogRead(1);  
      if (digitalRead(1) == 0) {
        // wait until pin goes high
        while (digitalRead(1) == 0) {}
        // get the time when input goes high
        timeStart = micros();
        // wait until input goes low again
        while (digitalRead(1) == 1) {
        }
        // wait until input goes high again
        while (digitalRead(1) == 0) {
        }
        // take the actual time in microseconds
        timeStop = micros();
        // the timedifference is the the time between two rising
        // edges of the input pin (= 1 period)
        timeStart = timeStop-timeStart;
        // normally the result is > 0, otherwise the
        // function micros() had an overflow (every 70 min)
        // the discard this measuring (or correct the value)
        if (timeStart > 0) {
          // calculate frequency
          // 1000000 microseconds/second, f = 1/t
          double f = 1000000.0/timeStart;
          // report the measured frequency
          lcd.println(f);
          delay(1000);   
          lcd.clear();
          delay(1000);

        }
      }
  }
}
 while (millis() < 5000)

Could it be that it works, but only for five seconds?

the code that i wrote is not working

Can you explain what the difference is between what you expect it to do, and what it actually does do is, please? That’s the only commonly-agreed definition of “working” that we currently have.
Maybe a reference to your speed sensor?

Why do you read “val”? You don’t do anything with it.

You’ve declared these:

#define frqPin 1  //analog pin 1 for frqpin
const int sensorPin = 1; //sensor input is analog pin 1

so why don’t you use them?

Could it be that it works, but only for five seconds?

so you are saying is that should be deleted ?

Can you explain what the difference is between what you expect it to do, and what it actually does do is, please? That’s the only commonly-agreed definition of “working” that we currently have.
Maybe a reference to your speed sensor?

Why do you read “val”? You don’t do anything with it.

by the mean of not working, it cannot show the frequency through the LCD, it is blank. The thing that i would like to do is showing the frequency through the LCD.

the reference to the speed sensor, you mean a data sheet?

and one more question
the AC must be converted, right? if i want to use the ADC in the ATmega168
Does the code i wrote can do the ADC function?

Had some changes
Are there any useless codes or codes that i haven’t use them ?

#define frqPin 1  //analog pin 1 for frqpin
int analogPin = 1; //sensor input is analog pin 1
int val = 0;
long timeStart, timeStop;
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {
  pinMode(analogPin, INPUT);
  pinMode(frqPin, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.println("Start measuring");
  
}

void loop()
{
  
val = analogRead(1);     {
      if (digitalRead(1) == 0) {
        // wait until pin goes high
        while (digitalRead(1) == 0) {}
        // get the time when input goes high
        timeStart = micros();
        // wait until input goes low again
        while (digitalRead(1) == 1) {
        }
        // wait until input goes high again
        while (digitalRead(1) == 0) {
        }
        // take the actual time in microseconds
        timeStop = micros();
        // the timedifference is the the time between two rising
        // edges of the input pin (= 1 period)
        timeStart = timeStop-timeStart;
        // normally the result is > 0, otherwise the
        // function micros() had an overflow (every 70 min)
        // the discard this measuring (or correct the value)
        if (timeStart > 0) {
          // calculate frequency
          // 1000000 microseconds/second, f = 1/t
          double f = 1000000.0/timeStart;
          // report the measured frequency
          lcd.println(f);
          delay(1000);   
          lcd.clear();
          delay(1000);

        }
      }
  }
}

pinMode(analogPin, INPUT); Wrong.
val = analogRead(1); Unused.
while (digitalRead(1) == 0) Why not use the defined constant for the pin?

Put some serial debug prints - you've got the hardware, help us to help you.

by the mean of not working, it cannot show the frequency through the LCD, it is blank.

In a simple sketch, write "Yo stupid!" to the LCD. Does that appear on the LCD? If not, you have one problem. If it does, you have a different problem. It would be nice to know which problem you are having.

val = analogRead(1);

What is this for? You don't use val anywhere.

      if (digitalRead(1) == 0) {
        // wait until pin goes high
        while (digitalRead(1) == 0) {}
        // get the time when input goes high
        timeStart = micros();
        // wait until input goes low again
        while (digitalRead(1) == 1) {
        }
        // wait until input goes high again
        while (digitalRead(1) == 0) {
        }

Looks to me like you are trying to re-invent the pulseIn function. Why not just use pulseIn? We know that function works.

Wrong

it doesn't need to be set a pin mode?

Unused

If i would like to use the ADC, what the code is exactly ?

Why not use the defined constant for the pin?

so it should be :

while (digitalRead(frqpin) == 0)

right ?

In a simple sketch, write "Yo stupid!" to the LCD. Does that appear on the LCD? If not, you have one problem. If it does, you have a different problem. It would be nice to know which problem you are having.

The LCD is doing fine, it can show what i typed
but it cannot show the frequency that i am trying to find

What is this for? You don't use val anywhere

umm, if i would like to use the ADC, what the code is exactly

Looks to me like you are trying to re-invent the pulseIn function. Why not just use pulseIn? We know that function works.

If using the pulseIn function, could it be like this ?

 if (pulseIn(1, HIGH)); {
        // wait until pin goes high
        while (digitalRead(frqPin) == 0) {}
        // get the time when input goes high
        timeStart = micros();
        // wait until input goes low again
        while (pulseIn(1, LOW) ){
        }
        // wait until input goes high again
        while (digitalRead(frqPin) == 0) {
        }
        // take the actual time in microseconds
        timeStop = micros();
        // the timedifference is the the time between two rising
        // edges of the input pin (= 1 period)
        timeStart = timeStop-timeStart;
        // normally the result is > 0, otherwise the
        // function micros() had an overflow (every 70 min)
        // the discard this measuring (or correct the value)
        if (timeStart > 0) {
          // calculate frequency
          // 1000000 microseconds/second, f = 1/t
          double f = 1000000.0/timeStart;
          // report the measured frequency
          lcd.println(f);
          delay(1000);   
          lcd.clear();
         }
      }

An analogue input pin doesn't need a pinMode.

You're not using the analogue input value "val", so ignore it. It's noise in the program, and distracting.

If "frqpin" has a valid digital signal on it, yes.

If "frqpin" has a valid digital signal on it, yes.

now i am using the analog pin 1 with two function
one is counting the frequency, the other one is doing the ADC

for the valid digital signal, the signal is analog from the begining. the signal became digital signal after the ADC, right ?
so theoretically, it is valid digital signal, isn't it ?
Meanwhile, after the ADC, it starts measuring the frequency, then the freq can be shown on the LCD

However, i couldn't use the ADC, how to command the microcontroller to do it?

the signal became digital signal after the ADC, right

No. The analogue and digital sections are completely separate.
Pin 1 digital != pin 1 analogue

Can you post your schematic, and some reference to the signal you're trying to read?

Can you post your schematic, and some reference to the signal you're trying to read?

Thanks AWOL

Here's the Schematic, it is a little bit of rough, forgive me
Imgur

And it is the signal that i try to read. It is around 3.2kHz that shown on the CRO.

This the data sheet of speed sensor that i use, part no. is 0 261 210 104

Thanks a lot, very appreciate it.

Upfront caveat: I'm not much use with analogue!

However, could you use the analogue comparator, rather than the ADC?
Other than that, half-wave rectification and some squaring of the edges?
(you may also need a DC offset - difficult to tell from the oscilloscope trace)

However, could you use the analogue comparator, rather than the ADC?

what is the analogue comparator ? what is the function exactly ?

half-wave rectification and some squaring of the edges?

I'd tried the half-wave rectification, but i don't know how to square it

it showed me like this

The comparator is a feature of the AVR, but I don't think is directly supported by the Arduino, so you'd have to go off piste. Have a look at the AVR datasheet.
Simply put, the comparator is just that - it compares the voltages on two pins and can give an interrupt based on whether the voltage on the positive pin is rising, falling or equal (IIRC) with respect to the voltage on the negative pin.
I would assume that if you can get the DC offset right, this would be an ideal solution - what does that trace look like if you DC couple it, anstead of AC?
But, like I said, I'm not really an analogue person. :frowning:

u could use a RC-combo that prolongues the peak...
e. g.
signal ---- 100kR ---- 470nF (positive pin) ----- arduino digital input pin ----- 100kR (voltage divider) ----- ground
470nF (negative pin) ---- ground

u should use a voltage divider so that the arduino pin isnt hurt... i can just take 5.3V (max.!) or so...

-arne

prolongues the peak

What do you mean by prolongues the peak?

What do you mean by prolongues the peak?

I think he means prolongs - to make longer.
However an RC will not to that it will average. You actually need a peak detector for that.

I think he means prolongs - to make longer.
However an RC will not to that it will average. You actually need a peak detector for that.

I see.

Can i use the plan from the begining ?
do you have any ideas to do the find the frequency, Mike ?
using pulseIn ?

i don't know much about electronics or program :frowning:

If i would like to use the ADC and then find the frequency by pulseIn
Can the previous schematic that i post, do that ?

And there is a way finding the frequency

but i don’t understand how to declare the pin i used

Here’s the code:

#include <FreqCounter.h>
void setup() {
  Serial.begin(57600);                    // connect to the serial port
  Serial.println("Frequency Counter");
}

long int frq;
void loop() {

 FreqCounter::f_comp= 8;             // Set compensation to 12
 FreqCounter::start(100);            // Start counting with gatetime of 100ms
 while (FreqCounter::f_ready == 0)         // wait until counter ready
 
 frq=FreqCounter::f_freq;            // read result
 Serial.println(frq);                // print result
 delay(20);
}