Pages: [1] 2 3 ... 9   Go Down
Author Topic: Is this work for frequency counter  (Read 5241 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);

        }
      }
  }
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (millis() < 5000)

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

Quote
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:
Code:
#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?
« Last Edit: April 29, 2010, 11:46:32 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

so you are saying is that should be deleted ?

Quote
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?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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);

        }
      }
  }
}

« Last Edit: April 29, 2010, 12:50:34 pm by max88poon » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
pinMode(analogPin, INPUT);
Wrong.
Code:
val = analogRead(1);
Unused.
Code:
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.
« Last Edit: April 29, 2010, 01:15:06 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45973
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
val = analogRead(1);

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

Code:
     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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Wrong

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

Quote
Unused

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

Quote
Why not use the defined constant for the pin?

so it should be :
Code:
while (digitalRead(frqpin) == 0)

right ?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

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

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

Quote
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 ?
Code:
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();
         }
      }
« Last Edit: April 29, 2010, 01:32:14 pm by max88poon » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
http://www.flickr.com/photos/43337932@N04/4564634381/

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
http://www.farnell.com/datasheets/21735.pdf

Thanks a lot, very appreciate it.
« Last Edit: April 30, 2010, 02:52:35 am by max88poon » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Quote
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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   smiley-sad
« Last Edit: April 30, 2010, 06:10:26 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Pages: [1] 2 3 ... 9   Go Up
Jump to: