Pages: [1] 2   Go Down
Author Topic: Reading signal frequency  (Read 4568 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I would like to reading the signal by using arduino uno and display the output to serial lcd 16x2. my code at this moment is like this

Code:
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27,16,2);
#include <FreqMeasure.h>

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(14400);
  lcd.init();
  

  
FreqMeasure.begin();
}/*--(end setup )---*/

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      double frequency = F_CPU / (sum / count);
      lcd.setCursor(0, 1);
      unsigned long speeding = (frequency*0.3)/2; // calculate speed
      Serial.print("Freq:");
      Serial.print(frequency);
      Serial.print("       ");
      delay(5000);
      sum = 0;
      count = 0;
    }
  }

}

the original code is from here, http://www.pjrc.com/teensy/td_libs_FreqMeasure.html

i feed the signal from signal generator to the arduino pin 8 and then i connect serial lcd to the pin Tx arduino. The problem is the output keep going around 49-51 and it not change although i change the frequency from the signal generator. can someone help me? thank you

Moderator edit: Code tags.
« Last Edit: October 02, 2012, 06:22:08 am by AWOL » Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3216
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What are the intervals for frequencies do you use?

Try this:

Code:
#include <FreqMeasure.h>

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(14400);
  FreqMeasure.begin();
}/*--(end setup )---*/

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      double frequency = F_CPU / (sum / count);

      unsigned long speeding = (frequency*0.3)/2; // calculate speed
      Serial.print("Freq:");
      Serial.print(frequency);
      Serial.print("       ");
      delay(5000);
      sum = 0;
      count = 0;
    }
  }

}

Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bubulindo, why did you take away the lcd.SetCursor() call ?

I'd replace it with lcd.clear() instead.
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3216
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Mainly to see if the libraries were interfering with the internal setup. Didn't you notice some headers missing?

The clear option is also a good one. smiley
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
double frequency = F_CPU / (sum / count);

That's not the greatest piece of code. You may want to change it.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mainly to see if the libraries were interfering with the internal setup. Didn't you notice some headers missing?

Ooops, I didn't notice the OP was mixing lcd and Serial print()s smiley-razz  Taking away every (non-strictly necessary) lib and using only Serial.print()s is a good debugging suggestion.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for the reply.....i try the code suggest by bubulindo and it seems the output still the same. The weird is when i try unconnected the signal input from pin 8, there is still output and the value is 49-50
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for the reply.....i try the code suggest by bubulindo and it seems the output still the same. The weird is when i try unconnected the signal input from pin 8, there is still output and the value is 49-50

Then you should make sure pin 8 is the right one to use.
Logged

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

I'd scrap the speed calculation, put an "analogWrite" into "setup" on a spare PWM pin set to a 50% duty cycle, and see if you can measure the PWM frequency of around 490Hz by connecting the PWM pin to your input pin..
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.

USA
Offline Offline
Sr. Member
****
Karma: 13
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... the output keep going around 49-51 and it not change although i change the frequency from the signal generator.
... when i try unconnected the signal input from pin 8, there is still output and the value is 49-50.
It looks like pin 8, ICP1, the Input Capture pin for timer0/counter0, is open-circuited, and it sees only power frequency noise.  An easy way to test this is to connect a high-value resistor - like maybe 100k - between pin 8 and either ground and VCC, ort turn on the internal pullup resistor for pin 8.  If ICP1 has no signal connected, then FreqMeasure.available() should never return true.  You'll probably want to add some diagnostic code to tell you that it doesn't ever come true, rather than simply waiting to see if nothing happens.  Save millis() in setup, and after each look at  FreqMeasure.available(), check to see if a second has passed; if it has, Serial.print() something to tell you so, and save millis() again.

Check your frequency generator.  It may have a button that disconnects its output.  If it's disconnected, all you have connected to ICP1 is an antenna to pick up noise.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the original code is from here

If you are measuring a signal with sufficient swing (that it will trigger a counter click), all you need is a small resistor + the mcu.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL, yup I tried and it can read the 490 Hz.

It looks like pin 8, ICP1, the Input Capture pin for timer0/counter0, is open-circuited, and it sees only power frequency noise.  An easy way to test this is to connect a high-value resistor - like maybe 100k - between pin 8 and either ground and VCC, ort turn on the internal pullup resistor for pin 8.  [/quote]

tmd3, so, i should try connect resistor between pin 8 and ground is it and dont i have to feed with any signal? by the way, what is ort?
Logged

USA
Offline Offline
Sr. Member
****
Karma: 13
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... so, i should try connect resistor between pin 8 and ground is it and dont i have to feed with any signal?
Um, no.  If you want to know something about a signal, like it's frequency, you'll certainly have to connect it.  My recommendation is that you connect a resistor to either 5V or to ground, and connect the other end to ICP1, or pin 8 on an Arduino Uno.  Then connect your signal between ICP1 and ground, as you normally would.

Here's what I suspect:  For some reason - a broken lead, an in correct connection, a switch in the wrong position, or something, there's nothing at all connected to ICP1.  That makes ICP1 a "floating input."  The pin has a very high impedance to both ground and VCC, and it's not referenced to anything.  In that state, it's much more susceptible to electromagnetic interference than it would be if it were connected to something.  I suspect that what you're reading is not your input signal, but the frequency of electromagnetic fields caused by the electric power system.  Your readings suggest that you live in a region where the power frequency 50 Hz.  Tell us if that's correct.

The fact that you can read 490 Hz from a PWM pin supports this theory.  I suspect that you didn't use the leads from your frequency generator to connect the PWM pin to ICP1.  That means that both the frequency generator and its leads were out of the circuit.  I suspect that one of them isn't doing what you think:  either the lead is broken, or improperly connected, or the frequency generator is disconnected from the circuit - probably by a switch on the front panel.

By applying a resistor between ICP1 and a good logic level, like VCC or GND, you make the pin a lot more resistant to the effects of electromagnetic fields.  Pick a high value resistor - like maybe 100K.  Then, connect your signal between ICP1 and ground, and run your program.  I suspect that you'll never get a reading, because the logic level on ICP1 will never change.  If that's what happens, it supports my theory that the input isn't seeing the signal, or anything else.  To see what the frequency measurement library does with an input that never changes - one whose frequency is zero - see your link - http://www.pjrc.com/teensy/td_libs_FreqMeasure.html - under the heading, "Zero Handling."

Quote
... what is ort?
"ort" is a word that I typed by mistake.  I had intended to type "or".
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I try to connect as u suggested but no luck. or is it possible because i connect wrong pin? i looked the code again and there is no mention about pin 8 as an input.
Logged

USA
Offline Offline
Sr. Member
****
Karma: 13
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... but no luck.
Be more specific.  What were your results?  Did you continue to measure about 50 Hz?  Did you fail to get a reading at all?

Quote
... or is it possible because i connect wrong pin?
I can't see your connections.  If you connected to the wrong pin, that would be an explanation.  Maybe you're asking if pin 8 is the correct pin for this program to use - I'll suggest that you review the site you referenced in your original post - http://www.pjrc.com/teensy/td_libs_FreqMeasure.html - which states that pin 8 is the pin to use for the Arduino Uno.  If you're not using an Uno, maybe you are indeed connected to the wrong pin.  Which Arduino are you using?

Quote
... i looked the code again and there is no mention about pin 8 as an input.
There's no mention of any pin as an input.  INPUT is the default state of the pins, so it's generally not necessary to explicitly set the mode to INPUT.

I don't see that you've answered the question about the power frequency at your location.  Is it 50 Hz?
Logged

Pages: [1] 2   Go Up
Jump to: