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);
}
}
}
}
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
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);
}
}
}
}
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.
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();
}
}
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?
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)
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.
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...
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);
}