Go Down

Topic: Is this work for frequency counter (Read 9282 times) previous topic - next topic

RIDDICK

#30
May 03, 2010, 10:03 am Last Edit: May 03, 2010, 11:08 pm by RIDDICK Reason: 1
here is a "peak detector":
http://www.falstad.com/circuit/e-555monostable.html
(it works better than my RC-plan) :-)

but u need the negative part of ur AC signal... so u dont need any rectification, because of the horizontal capacitor...

for a 3.2kHz signal the horizontal capacitor should be 100nF and the vertical cap should be 150nF...

-arne
-Arne

novamax

Quote
You also need to ensure the voltage on the Arduino input pin does not exceed 5 volts. There are many ways to do that, for example using a transistor (Grumpy_Mike explained this your "DC to the Arduino" thread) or you could use an opto-isolater.


oh
that's great, i am going to try this
let see what we can see

Quote
here is a "peak detector":
http://www.falstad.com/circuit/e-555monostable.html
(it works better than my RC-plan)

but u need the negative part of uc AC signal... so u dont need any rectification, because of the horizontal capacitor...

for a 3.2kHz signal the horizontal capacitor should be 100nF and the vertical cap should be 150nF...

-arne


Thanks for your advise, but i don't know about 555 timer, it is kind of difficult :(
i will try it anyway, thanks
let see what it can show me :)

RIDDICK

#32
May 03, 2010, 11:28 pm Last Edit: May 03, 2010, 11:29 pm by RIDDICK Reason: 1
u could try this, too:
http://arduino.cc/en/Reference/AttachInterrupt
the short peak should trigger the interrupt safely...
then u just measure the time between the interrupt calls...

but looking for a peak should be good, too:
Code: [Select]

const uint8_t pin = 5; // digital pin #5
void setup() {
 Serial.begin(115200);
 pinMode(pin,INPUT);
 digitalWrite(pin,HIGH); // pullup
}
void loop() {
 uint32_t sts = micros(); // start time stamp
 const uint32_t c = 1000; // wait for 1000 pulses
 for (uint32_t i=c; i>0; i--)
   pulseIn(pin,HIGH);
 uint32_t ets = micros(); // end time stamp
 Serial.println(c*1e-3/(ets-sts)); // output kHz
}


this circuit should be good in order to protect ur arduino:
AC signal ---- diode anode
diode cathode ---- 100k resistor lead A
100k resistor lead B ----- NPN transistor base
NPN transistor emitter ---- arduino ground
NPN transistor collector ----- resistor 1k lead A
resistor 1k lead B ------ arduino pin #5

dont forget this:
external circuit signal ground ----- arduino ground
(but make sure that this doesnt cause a short circuit (e. g. when the external circuit uses USB 5V as ground and USB ground as -5V power supply)... :-)

-arne
-Arne

RIDDICK

oh
this
Code: [Select]
Serial.println(c*1e-3/(ets-sts));
should be this (because ets-sts r _u_secs)
Code: [Select]
Serial.println(c*1e3/(ets-sts));

:-)

-arne
-Arne

novamax

HELP!! :(
this code below still could not show the frequency. it keeps showing a number, god knows where it comes from. the number is around 45.19

whether there is sensor connecting to the analog or not

Code: [Select]
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);
  lcd.begin(16, 2);
 Serial.begin(9600);
 
}

void loop()
{
 
val = analogRead(1);     {
     if (pulseIn(val, HIGH)); {
       // wait until pin goes high
      timeStart = micros();
       // wait until input goes low again
       while (pulseIn(val, LOW) ); {
       // take the actual time in microseconds
       timeStop = micros();
       // the timedifference is the the time between two rising
       // edges of the input pin (= 1 period)
       int timeStart1 = 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 (timeStart1 > 0) {
         // calculate frequency
         // 1000000 microseconds/second, f = 1/t
         double f = 1000000.0/timeStart1;
         // report the measured frequency
         lcd.println(f);
         delay(1000);  
         lcd.clear();
        }
     }
     }
}
}


PaulS

Code: [Select]
void loop()
{
val = analogRead(1);     [glow]{[/glow]

What's this curly brace for?
Code: [Select]
     if (pulseIn(val, HIGH))[glow]; {[/glow]
Are you sure you want that semicolon there?
Code: [Select]

       // wait until pin goes high
      timeStart = micros();
       // wait until input goes low again
       while (pulseIn(val, LOW) )[glow];[/glow] {

What about this one?

The pulseIn function returns a value. The value is how long it took the pulse to go HIGH or LOW. You will get misleading results if you do not use that time.

novamax

Thanks Paul
i have cancelled all the ";"
however, it still keeps showing me a number
it is much larger, around 30-900
what did i do wrong :( ?

PaulS

Quote
i have cancelled all the ";"

Well, that probably wasn't the greatest idea.

Post your current code.

novamax

here it is
thanks
Code: [Select]
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);
  lcd.begin(16, 2);
 Serial.begin(9600);
 
}

void loop()
{
 
val = analogRead(1);  
     if (pulseIn(val, HIGH)){
       // wait until pin goes high
      timeStart = micros();
       // wait until input goes low again
       while (pulseIn(val, LOW) ) {
       // take the actual time in microseconds
       timeStop = micros();
       // the timedifference is the the time between two rising
       // edges of the input pin (= 1 period)
       int timeStart1 = 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 (timeStart1 > 0) {
         // calculate frequency
         // 1000000 microseconds/second, f = 1/t
         double f = 1000000.0/timeStart1;
         // report the measured frequency
         lcd.println(f);
         delay(1000);  
         lcd.clear();
        }
     }
     }
}


PaulS

Please read this:
http://arduino.cc/en/Reference/PulseIn

The first argument to pulseIn is the pin to measure, not some value read from that pin.

novamax

#40
May 10, 2010, 10:40 am Last Edit: May 10, 2010, 10:40 am by max88poon Reason: 1
Quote

The first argument to pulseIn is the pin to measure, not some value read from that pin.


umm
so i am just giving a pulse to the pin, not reading it?

how can i read pulse  :( ?

PaulS

Where is the pulse coming into the Arduino? To some pin, presumably. Pass that pin number to pulseIn.

novamax

#42
May 10, 2010, 01:40 pm Last Edit: May 10, 2010, 01:43 pm by max88poon Reason: 1
Quote
Where is the pulse coming into the Arduino? To some pin, presumably. Pass that pin number to pulseIn


i think that is digital 8 or 9
when i took out the digital 8 and 9, the screen show me nothing
but how to pass the pin ?
there should be no pulse in the first place, due to the fact that i didn't connect the sensor output to the arduino
however, it keeps showing a number , i don't know what the pulse is

i am sorry about that Paul, that i was off, it's because i gotta be out of the school, i am home now, and thanks, please keep talking to me :(

could some one guide me to do the program?
i very appreciate it, it is my final project, i may not get a pass if i couldn't do it :(

novamax

ah
hey guys, i found something
there is nothing shown, if the arduino analog 0 is connected to the LCD analog 0
if i took out the analog input, the number is coming out again.
someone know what is happening :( ?

could anyone help me checked the program, is it correct that
the frequency can be determinated from the analog pin 1 ?
and the pin 1 is connected to the sensor.

Thanks everyone :)

Groove

#44
May 10, 2010, 02:05 pm Last Edit: May 10, 2010, 02:07 pm by GrooveFlotilla Reason: 1
Quote
if the arduino analog 0 is connected to the LCD analog 0


What sort of LCD are you using that has an analogue input?

Quote
the frequency can be determinated from the analog pin 1 ?

Analogue pin 1 (what's wrong with analogue pin zero?) can be used with "pulseIn", but you have to number it as a digital pin in your sketch.
It's digital pin 15.
Per Arduino ad Astra

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy