Do you mean I need to connect PIN2 directly to the Black wind sensor wire, or do I need to ground them together? also, do I still connect the red wire to the 3.3V?
You don't want me to use any attachInterrupt in the code, correct?
I learned over time here that if you want people to help you need to listen so I am going to rebuild it all as you guys suggested. So found this code for debouncing...
#define DEBOUNCE_TIME 20 //20mS per debounce step
const int tbrgPin = 2; // Rain gauge is connected to this pin
// debounce pin function
// - returns 1 when input switches low, otherwise returns 0
//
int debounceThePin( int pin )
{
static uint32_t debounceTimer = 0;
static uint8_t debounceShift = 0xff; // 8-bit shift to debounce 8 times
static uint8_t debounceState = 1; // current debounced state of the imput
int returnValue = 0;
if ( millis() > (debounceTimer + DEBOUNCE_TIME) )
{
debounceTimer = millis();
if ( digitalRead( pin ) == HIGH )
{
debounceShift = (debounceShift << 1) | 0x01; // shift in a '1'
if ( debounceShift == 0xff )
{
debounceState = 1; // input is really HIGH
}
}else
{
// input is low
debounceShift = (debounceShift << 1); // shift in a '0'
if ( (debounceShift == 0x00) && (debounceState == 1) )
{
returnValue = 1; // switch has just switched LOW
debounceState = 0;
}
}
}
return returnValue;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(tbrgPin, INPUT_PULLUP);// initialize the TBRG pin as an input:
}
void loop() {
// put your main code here, to run repeatedly:
if ( debounceThePin( tbrgPin ) )
{
// do something useful...
Serial.println("Pin gone LOW");
}
}
Do you think is that something I can start with?
I am kind of lost on how to get from this to finish calculation of the speed like I have in my code.
The problem you originally posted has been solved. The extra few mA comes from the pulldown resistor on the input pin, which you forgot to mention.
If that is the only problem, you could reduce the current draw upon switch closure by increasing the pulldown resistor (for example to 100K and also reduce the debounce capacitor value proportionally, to 10 nF). You must use INPUT instead of INPUT_PULLUP on the pin.
No changes to the code are necessary, but the RC time constant may need adjusting to suit switch bounce, if it is a problem. 100K and 10 nF lead to about one millisecond timeout.
@jremington thank you! I will be happy to solve it as you suggested.
just making sure:
-replace the 1k resistor with 100k (the other 360 resistor and the diode are staying)
-replace the 1uF capacitor to 10nF
-the INPUT_PULLUP changes to INPUT (but the attachInterrupt and the rest of the code are staying the same).
also, can you please explain more on the "RC time constant" and what actually needs to be done with the 1-millisecond timeout.
In this case, roughly the time it takes the capacitor to discharge, and arm the input for new pulse. If the switch bounces excessively, RC may need to be 10x larger (100 nF capacitor instead of 10nF).
With an oscilloscope and a wind tunnel! Or at least a hair dryer (set on cold).
For the choice of cap, can you tell us the number of switch operations per revolution, and the wind speed needed for one revolution per second? You don't want the cap to be too large, because genuine switch changes could be missed. But I suspect "too large" will be quite a high value.
Original Vortex Specifications:
Sensor Type:
3-Cup Lexan rotor. Reed switch/magnet provide 1 pulse per rotation.
Output:
1 pulse per rotation, 2.5 mph per Hz
Rotor Diameter:
approx. 5 in (~125 mm)
Speed Range:
approx. 3 mph to 125+ mph (~5 kph to over 200 kph)
Mounting Bracket:
Supplied with an aluminum mounting bracket with 2 holes for screws. Designed to be mounted on top of a pole or bracket.
Wire
Standard length is 25 feet (8m), custom lengths available upon request. Tested OK to over 1,500 feet. The wire is provided stripped and unterminated. 2 small wire nuts provided to connect to the display once installed
Power:
None Required
Vortex II Specifications:
Sensor Type:
3-Cup Lexan Rotor (w/ Metal Option).
Dry Contact Reed Switch (standard) Accuracy:
+/-4% of reading or 1 mph, whichever is greater (depends on the display used)
Power:
None Required
Hall Sensor (optional) Accuracy:
+/-4% of reading or 1 mph, whichever is greater (depends on the display used)
Power:
Requires 5VDC input
Optical Sensor with 8-flag disc (optional - best for low wind speed readings) Accuracy:
<4% of reading or 1mph, whichever is greater
Power:
Input: 5VDC +/-5%, less than 50mV noise
Output: Low: <0.1VDC High: 3-3.3VDC, 0.31 mph/Hz
Rotor Diameter:
approx. 6 in (152 mm)
Speed Range:
approx. 3 mph (0.5 mph for 8-pulse sensor) to 125+ mph (~5 kph to over 200 kph)
Mounting Bracket:
Flat metal bracket: 2 holes provided
Tubular mounting option: 2 holes provided
Designed to be mounted on top of a pole or bracket.
Wire
Standard length is 25 feet (8m), custom lengths available upon request. Tested OK to over 1,500 feet. Any wire will work. Inpeed wire is 2-conductor (Reed Switch) or 3-conductor w/ UV-rated jacket, 22 gauge
Length:
pprox. 11 in (280 mm) overall
Width
approx. 6 in (152 mm)
With the specification: option "Dry Contact Reed Switch (standard) Accuracy", you don't really need any external circuit to handle debouncing and the sketch could be very straightforward and simple to optimise for minimal power consumption in sleep mode. As already suggested, use simple polling in the loop instead of interrupts to detect the pulses, ignore pulses which come with in say 50mS of the last valid pulse. You can retain the logic which you have already developed to (a) calculate wind speed from the rate of switch pulses and (b) force sleep mode. For absolute minimum power consumption, you can disable the internal pullup resistor prior to entering sleep mode.
Bamanio:
Original Vortex Specifications:
...
Will, that answer your question?
Too much, actually. That was the specification for 3 different models (reed switch, hall sensor, optical sensor). You have the reed switch version, so only those specs were needed.
Your capacitor question:
Max wind speed = 125MPH, 2.5MPH per Hz --> max switching frequency = 50Hz --> min switching period = 20ms
If we assume the reed switch is 10ms closed and 10ms open, then the RC constant of your debounce circuit needs to be significantly less than that.
With 10nF
RC = (100K Ohms*10 nF) = 0.001 seconds = 1ms. Should be OK. That's the same as the original debounce circuit, and whoever designed that presumably tested it. With my anemometer (a different design but also uses a reed switch), I poll the switch every 1ms and had no problems with bouncing.
With 100nF
RC = (100K Ohms*100 nF) = 0.01 seconds = 10ms. Too long, I think. Genuine switch edges could be lost.
I sent the wind sensor to a lab and got back the results, the measurements were extremely accurate but for some reason (still investigating) they told me the results did not go over 69.8 KMH.
I was wondering what can cause that cause I don't see any problem in the code itself.
I was wondering if the debounce circuit that I used (The resistors and Capacitors) can cause that????
1x 360 Ohms resistor (update: I am using a 360 Ohms resistor not 360k)
1x 1uF Cap
1x 1k resistor
1x diod
(this was in the lab and got to 69.8KMH)
or
1x 360 Ohms resistor (update: I am using a 360 Ohms resistor not 360k)
1x 10nF Cap
1x 100k resistor
1x diod
(this is the saving power version that you guys helped me with, I did not have a chance to send it to the lab yet)
@Paul__B I tried changing to code "de-bounce" but nobody shared a fully working code with me , So it was too hard for me to figure on my own :(, also the solution that the guys here gave me worked perfectly! (until now :))
I am trying to understand if I need to connect it like that:
red wire to a digital input (pin 2) on pro mini that is also connected to vcc 3.3 via a 10k resistor (pull up).
the black wire to ground.
also, I am trying to change the code to fit my Inspeed Vortex:
#include <math.h>
#define WindSensorPin (2) // The pin location of the anemometer sensor
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine
float WindSpeed; // speed miles per hour
void setup() {
Serial.begin(9600);
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
Serial.println("Davis Wind Speed Test");
Serial.println("Rotations\tMPH");
}
void loop() {
Rotations = 0; // Set Rotations count to 0 ready for calculations
sei(); // Enables interrupts
delay (3000); // Wait 3 seconds to average
cli(); // Disable interrupts
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/3) = P * 0.75
WindSpeed = Rotations * 0.75;
Serial.print(Rotations); Serial.print("\t\t");
Serial.println(WindSpeed);
}
// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {
if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
Are you certain you used those cap & resistor values? If the cap was in fact 0.1uF or the resistor was in fact 36K, then the RC constant would be 36ms, leading to a max frequency of 27.7Hz which would be equivalent to a max speed of 69.4KPH.