# How to Hookup Davis Anemometer to Arduino (Part 2 of 3)

## How to Measure the Wind Speed

The wind cups have a reed switch mounted inside near the shaft. This switch is activated once per revolution of the cups. To calculate the wind speed we apply a formula that converts the number of time the switch activates per period of time to miles per hour.

According to the Davis Anemometer technical document 1 mile per hour is equal to 1600 revolutions per hour.

Using the formula V = P(2.25/T) we can calculate the speed in miles per hour.

• V is speed in miles per hour
• P is number of pulses per sample period
• T is the sample period in seconds

## Calculate the Wind Speed

In this sketch for the Arduino we are using the formula as provided by Davis to calculate the instantaneous wind speed in miles per hour.

Due to the random nature of switch activations on pin 2 we use a hardware interrupt on digital input pin 2 which generates an interrupt on the falling edge of the pulse (5V dropping to ground and back to 5V when the switch is closed by the anemometer). The interrupt service routine (ISR) that is executed on the interrupt increments a counter.

We are using a delay of 3 seconds to get an average sample for the wind speed. Even though the delay will halt the loop code for that period the interrupt handler called rotation is still executed. This will be the sample period (T) we use in the calculation. The calculation is performed in the main loop.

Note: Code in Interrupt Service Routines should be kept to a minimum.

## Wind Speed Sketch

The wind speed recorded here is an instantaneous value. The normal process would be to average the wind speed over a period of time. A common value is 10 minutes. In the sample sketch below we are using the instantaneous value only.

Davis Wind Speed Sketch
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 #include #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(); } }

The output from the sketch is the number of rotations in the 3 second time period in the first column. In the second column is the speed in miles per hour In the next part of this hookup guide we update the softare to measure both wind speed and direction. We also add some additional functionality to provide more information from the anemometer.