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

Anemometer Software

In the final part of this hookup guide we take some of the code used on the wind direction and wind speed sketch to create a combined program that does both. We also add some additional functionality like display the speed in knots or km/hr. We can also display the window direction by degrees or direction based on the wind vane position.

Arduino to Davis Anemometer Sketch Output

In part 2 of this hookup guide in the software we used a delay function to create a 3 second delay to average the wind speed over that time. The delay function will suspend the loop from executing for that time period but the interrupt handler routine that is executed when ever the wind cups rotate one revolution is still functioning. Normally you would avoid using delays as it blocks any other code from executing for that time period.

In this code we are using a timer interrupt that will generate an interrupt when the 0.5 second timer is triggered. The interrupt service routine called isr_timer will execute and this will be used to create a 2.5 second sample period.

We are using the TimerOne library to provide the timer interrupt functionality. You can download this library from several sources including the arduino website.


You will need to set the variables WindSensorPin (line 2), WindVanePin (line 3) and VaneOffset (line 4) to match your configuration.

Davis Anemometer Sketch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "TimerOne.h" // Timer Interrupt set to 2 second for read sensors
#include <math.h>

#define WindSensorPin (2) // The pin location of the anemometer sensor
#define WindVanePin (A4) // The pin the wind vane sensor is connected to
#define VaneOffset 0; // define the anemometer offset from magnetic north

int VaneValue; // raw analog value from wind vane
int Direction; // translated 0 - 360 direction
int CalDirection; // converted value with offset applied
int LastValue; // last direction value

volatile bool IsSampleRequired; // this is set true every 2.5s. Get wind speed
volatile unsigned int TimerCount; // used to determine 2.5sec timer count
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in isr

float WindSpeed; // speed miles per hour

void setup() {

LastValue = 0;

IsSampleRequired = false;

TimerCount = 0;
Rotations = 0; // Set Rotations to 0 ready for calculations

Serial.begin(9600);

pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), rotation, FALLING);

Serial.println("Davis Anemometer Test");
Serial.println("Speed (MPH)\tKnots\tDirection\tStrength");

// Setup the timer interupt
Timer1.initialize(500000);// Timer interrupt every 2.5 seconds
Timer1.attachInterrupt(isr_timer);
}

void loop() {

getWindDirection();

// Only update the display if change greater than 5 degrees.
if(abs(CalDirection - LastValue) > 5) {
LastValue = CalDirection;
}

if(IsSampleRequired) {
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/2.5) = P * 0.9
WindSpeed = Rotations * 0.9;
Rotations = 0; // Reset count for next sample

IsSampleRequired = false;

Serial.print(WindSpeed); Serial.print("\t\t");
Serial.print(getKnots(WindSpeed)); Serial.print("\t");
Serial.print(CalDirection);
getHeading(CalDirection); Serial.print("\t\t");
getWindStrength(WindSpeed);
}
}

// isr handler for timer interrupt
void isr_timer() {

TimerCount++;

if(TimerCount == 6)
{
IsSampleRequired = true;
TimerCount = 0;
}
}

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

// Convert MPH to Knots
float getKnots(float speed) {
return speed * 0.868976;
}

// Get Wind Direction
void getWindDirection() {

VaneValue = analogRead(WindVanePin);
Direction = map(VaneValue, 0, 1023, 0, 359);
CalDirection = Direction + VaneOffset;

if(CalDirection > 360)
CalDirection = CalDirection - 360;

if(CalDirection < 0)
CalDirection = CalDirection + 360;

}

// Converts compass direction to heading
void getHeading(int direction) {

if(direction < 22)
Serial.print(" N");
else if (direction < 67)
Serial.print(" NE");
else if (direction < 112)
Serial.print(" E");
else if (direction < 157)
Serial.print(" SE");
else if (direction < 212)
Serial.print(" S");
else if (direction < 247)
Serial.print(" SW");
else if (direction < 292)
Serial.print(" W");
else if (direction < 337)
Serial.print(" NW");
else
Serial.print(" N");
}

// converts wind speed to wind strength
void getWindStrength(float speed) {

if(speed < 2)
Serial.println("Calm");
else if(speed >= 2 && speed < 4)
Serial.println("Light Air");
else if(speed >= 4 && speed < 8)
Serial.println("Light Breeze");
else if(speed >= 8 && speed < 13)
Serial.println("Gentle Breeze");
else if(speed >= 13 && speed < 18)
Serial.println("Moderate Breeze");
else if(speed >= 18 && speed < 25)
Serial.println("Fresh Breeze");
else if(speed >= 25 && speed < 31)
Serial.println("Strong Breeze");
else if(speed >= 31 && speed < 39)
Serial.println("Near Gale");
else
Serial.println("RUN");
}

In the isr_timer interrupt handler we check if 2.5 seconds has elapsed. If so then we set the IsSampleRequired flag to true. In the loop code we check if this flag is true. If so we then calculate the speed of the anemometer. We then update the console with the new values. We also reset the IsSampleRequired flag to false.

References


License