unsigned long duration;
void setup()
{
Serial.begin(9600);
pinMode(13,INPUT); //reads the input from pin 13
// attachInterrupt(0, count, LOW);
}
void loop(){
duration=pulseIn(13,LOW);
Serial.print(duration );
Serial.print("windspeed in m/s ");
windspeed =((1000000.0/ duration*2.5)/0.447);
Serial.print(windspeed,DEC);
Serial.println(" ");
delay(500);
}
my outputs are shown below makes no sense to me
0 windspeed in m/s :0.0000000000
192355 windspeed in m/s :29.0756206512
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
6 windspeed in m/s :932140.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
0 windspeed in m/s :0.0000000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
159973 windspeed in m/s :34.9611549377
6 windspeed in m/s :932140.2500000000
0 windspeed in m/s :0.0000000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
0 windspeed in m/s :0.0000000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
0 windspeed in m/s :0.0000000000
4 windspeed in m/s :1398210.2500000000
0 windspeed in m/s :0.0000000000
Don't think so, he is printing "duration" - first digit in line:
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
4 windspeed in m/s :1398210.2500000000
6 windspeed in m/s :932140.2500000000
I think, the problem is related to pin 13 (unlucky) number, it has led-resistor attached to it on board, that would mix up with pull-up resistor in wrong way.
Edited:
Schematic.
duration=pulseIn(10,LOW);
Serial.print( duration );
Serial.print("windspeed in m/s ");
windspeed ( 2.5 * 1000000.0 / duration );
Serial.print(windspeed,DEC);
Serial.println(" MPH");
delay(500);
I think I see a couple of problems. First, I wouldn't use pin 13 as my digital input because of the LED that is normally on that pin. Pick another if you can. Your sensor is just a reed switch that closes every time a magnet passes by it, so basically it is a one pulse per revolution (1PPR) generator. I would put a pull-up resistor on one of the DIO pins, say 10K, pulled up to your supply voltage. Then connect one of your sensor leads to the DIO pin and the other sensor lead to ground. This will produce a 1PPR signal that is normally at supply voltage and goes to 0 volts every time the reed switch closes. The signal is pretty noisy too because of the reed switch and will require some sort of de-bouncing. That's the reason you get the 4-6 from the pulse() function. That's the duration of the first bounce.
Measuring the pulse width on that pin doesn't give you the information you need. You need to measure the duration of each revolution, that is the time between the signal making its High to Low transition each revolution. The data sheet for your sensor says that if it rotates one revolution in one second (1Hz) then that is 2.5 mph. Let's say you're on Mars or somewhere that the wind can get up to 250 mph. In that case the sensor would be rotating at 100 revolutions per second (100Hz) and the duration of one revolution would be 10 milliseconds, so you can safely de-bounce your sensor input for 10 to 20 milliseconds and still measure wind speed up to a reasonable maximum.
I would start a timer when your DIO pin goes LOW. Then I would wait 10-20 milliseconds for the reed switch to stop bouncing and then wait for it to go LOW again and then stop and read the timer. The amount of elapsed time then would be used to calculate wind speed.
Sounds like a good approach, EmilyJane; and re "duration" variable, pulseIn() returns an unsigned long, and I had real problems with it in code similar this until I cast it as a float before using it in a formula with division.
Not my area of expertise at all, but seems to me a potential problem!
Another thing: I've done a lot of work with water velocity meters like this, and you'll always want some way to average readings over a time period, both a short one like 1 or 2 seconds to eliminate spurious readings, and a longer one, maybe 5 or 10 seconds, to eliminate real changes in the rotor speed caused by turbulence and small gusts. Probably the literature with the meter covers this.
I agree. Without averaging, the reading is going to be pretty "noisy". Sensing the reed switch closure will introduce some noise as well. I don't think the pulseIn() function will be of any use here but one should always be wary of mixing data types in arithmetic.
I left out some steps in my post above. After sensing that the input has gone LOW one will have to make sure that it has gone HIGH again before looping to detect its next LOW transition. This will be important at low wind speeds.
This topic came up a couple of years ago and I don't know if the OP ever got it to work. The company that sell the sensor supplies a "readout" that is just repurposed bicycle computer.
I recommend taking a look at the conditioned sensor output with an oscilloscope to get a feel for how the LOW time varies with windspeed. If it was me, I'd run it through a one-shot to create a de-bounced, repeatable signal, as well.
I will purchase some resistors tomorrow and have the wires connected to a digital I/o port as u suggested. I think I would understand better if you can please provide me with some kind of a pseudocode as I am very new to programming
One approach would be to generate an interrupt with your sensor and increment a counter in the interrupt handler. You could wait a fixed period of time in your foreground program and then read the number of interrupts (sensor revolutions) that had occurred during that time period and calculate windspeed from that.
Thanks Magician and Emilly for your help for the following code and hardware setup
Hi this is my new code. According to the outputs im getting its still not right. When I initially starts to twist the anemometer it gives a proper reading then even while its spinning its gives me 0m/s before coming into live once again.when I let it go it slows down gives proper readings and then when its suddenly stopped it gives a wrong speed(eg 278m/s).Then when i twist again it goes back to zero and starts giving correct outputs again.Outputs are on the bottom of this page.
float windspeed; // wind speed
unsigned long start, duration;
void setup()
{
Serial.begin(9600);
pinMode(2,INPUT); //reads the input from pin 2
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 60
windspeed : 18.62 m/s //START
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 224
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 98
windspeed : 11.40 m/s
duration of the one turn ( msec ) : 118
windspeed : 9.47 m/s
duration of the one turn ( msec ) : 142
windspeed : 7.87 m/s
duration of the one turn ( msec ) : 115
windspeed : 9.72 m/s
duration of the one turn ( msec ) : 133
windspeed : 8.40 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 101
windspeed : 11.06 m/s
duration of the one turn ( msec ) : 79
windspeed : 14.15 m/s //SLOWING DOWN BEGINS
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 91
windspeed : 12.28 m/s
duration of the one turn ( msec ) : 89
windspeed : 12.56 m/s
duration of the one turn ( msec ) : 102
windspeed : 10.96 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0 //0 AGAIN!!!
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
duration of the one turn ( msec ) : 122
windspeed : 9.16 m/s
duration of the one turn ( msec ) : 138
windspeed : 8.10 m/s
duration of the one turn ( msec ) : 159
windspeed : 7.03 m/s
duration of the one turn ( msec ) : 180
windspeed : 6.21 m/s
duration of the one turn ( msec ) : 180
windspeed : 6.21 m/s
duration of the one turn ( msec ) : 212
windspeed : 5.27 m/s
duration of the one turn ( msec ) : 212
windspeed : 5.27 m/s
duration of the one turn ( msec ) : 252
windspeed : 4.43 m/s
duration of the one turn ( msec ) : 252
windspeed : 4.43 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 482
windspeed : 2.32 m/s
duration of the one turn ( msec ) : 482
windspeed : 2.32 m/s
duration of the one turn ( msec ) : 482
windspeed : 2.32 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 679
windspeed : 1.65 m/s
duration of the one turn ( msec ) : 679
windspeed : 1.65 m/s
duration of the one turn ( msec ) : 679
windspeed : 1.65 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 4
windspeed : 279.38 m/s
duration of the one turn ( msec ) : 4 //ANEMOMETER HAS STOPPED ALLREADY! WRONG OUTPUT
windspeed : 279.38 m/s
duration of the one turn ( msec ) : 4
windspeed : 279.38 m/s
duration of the one turn ( msec ) : 4
windspeed : 279.38 m/s
duration of the one turn ( msec ) : 4
windspeed : 279.38 m/s
duration of the one turn ( msec ) : 0 //NOW I TWIST AGAIN TO MAKE IT SPIN
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 0
windspeed : 0.00 m/s
duration of the one turn ( msec ) : 659
windspeed : 1.70 m/s
duration of the one turn ( msec ) : 659
windspeed : 1.70 m/s
Well, it's true that variable windspeed is not "up to date" when no "new" interruption was registered since last printing. To make your printing "smart", and print information only if it's fresh new, add flag in your code: "updated".
float windspeed; // wind speed
unsigned long start, duration;
int updated = 0; // <<<<-------- flag
void setup()
{
Serial.begin(9600);
pinMode(2,INPUT); //reads the input from pin 2
attachInterrupt( 0, wspeed, FALLING );
start=millis();
delay(20);
}
void wspeed()
{
duration = millis() - start;
start = millis();
updated = 1;
}
void loop(){
if (updated){
Serial.print (" duration of the one turn ( msec ) : ");
Serial.println ( duration );
Serial.print ("windspeed : ");
windspeed = ( 2.5 * 1000.0 / (float) duration )*(0.447);
Serial.print (windspeed);
Serial.println (" m/s");
delay(500);
updated = 0; // <<<<<---- reset a flag, so it woudn't print "old data".
}
}
thanks for ur code but now it's mostly zero .gives an accurate value here and there
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 9.72 m/s
windspeed : 8.28 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.89 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 0.00 m/s
windspeed : 1.89 m/s
windspeed : 0.00 m/s
Most likely you have to fight second problem, as always one step at a time
The issue is bouncing contact from sensor ( anemometer ). There are a few ways to fix it, you can search on forum or google for it.
I'd recommend to set limits on reported duration, so it never goes less than physical capability of the sensor itself. Anemometer, according to web link you posted, works up to 125 mph.
Rugged wind sensor handles speeds from 5 to over 125 mph.
or 50 Hz, so minimum duration is 1 / 50 = 20 millisecond.