Skip to content

Commit acee391

Browse files
authored
Merge pull request #4 from sparkfun/platform-adc-values
Add predefined wind vane ADC values for various platforms
2 parents 4dc7dba + 09f2fb4 commit acee391

File tree

5 files changed

+128
-68
lines changed

5 files changed

+128
-68
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ void setup()
2525
Serial.println("for operation, and may not be accurate for your project.");
2626
Serial.println("It is recommended to check out the calibration examples.");
2727

28-
// The library assumes a 12-bit ADC, but if yours is different, you can set
29-
// the resolution here
30-
weatherMeterKit.setADCResolutionBits(12);
28+
// Expected ADC values have been defined for various platforms in the
29+
// library, however your platform may not be included. This code will check
30+
// if that's the case
31+
#ifdef SFE_WMK_PLAFTORM_UNKNOWN
32+
// The platform you're using hasn't been added to the library, so the
33+
// expected ADC values have been calculated assuming a 10k pullup resistor
34+
// and a perfectly linear 16-bit ADC. Your ADC likely has a different
35+
// resolution, so you'll need to specify it here:
36+
weatherMeterKit.setADCResolutionBits(10);
37+
38+
Serial.println("Unknown platform! Please edit the code with your ADC resolution!");
39+
Serial.println();
40+
#endif
3141

3242
// Begin weather meter kit
3343
weatherMeterKit.begin();

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun Weather Meter Kit Arduino Library
2-
version=1.0.0
2+
version=1.1.0
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=A library to use the SparkFun Weather Meter Kit

src/SparkFun_Weather_Meter_Kit_Arduino_Library.cpp

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,25 @@ SFEWeatherMeterKit::SFEWeatherMeterKit(uint8_t windDirectionPin, uint8_t windSpe
2020
_rainfallPin = rainfallPin;
2121

2222
// The wind vane has 8 switches, but 2 could close at the same time, which
23-
// results in 16 possible positions. The datasheet specifies the resistance
24-
// for each direction, which were used to calculate the expected ADC values
25-
// for a 12-bit ADC (4095 max) with a 10k pullup
26-
_calibrationParams.vaneADCValues[WMK_ANGLE_0_0] = 3143;
27-
_calibrationParams.vaneADCValues[WMK_ANGLE_22_5] = 1624;
28-
_calibrationParams.vaneADCValues[WMK_ANGLE_45_0] = 1845;
29-
_calibrationParams.vaneADCValues[WMK_ANGLE_67_5] = 335;
30-
_calibrationParams.vaneADCValues[WMK_ANGLE_90_0] = 372;
31-
_calibrationParams.vaneADCValues[WMK_ANGLE_112_5] = 264;
32-
_calibrationParams.vaneADCValues[WMK_ANGLE_135_0] = 738;
33-
_calibrationParams.vaneADCValues[WMK_ANGLE_157_5] = 506;
34-
_calibrationParams.vaneADCValues[WMK_ANGLE_180_0] = 1149;
35-
_calibrationParams.vaneADCValues[WMK_ANGLE_202_5] = 979;
36-
_calibrationParams.vaneADCValues[WMK_ANGLE_225_0] = 2520;
37-
_calibrationParams.vaneADCValues[WMK_ANGLE_247_5] = 2397;
38-
_calibrationParams.vaneADCValues[WMK_ANGLE_270_0] = 3780;
39-
_calibrationParams.vaneADCValues[WMK_ANGLE_292_5] = 3309;
40-
_calibrationParams.vaneADCValues[WMK_ANGLE_315_0] = 3548;
41-
_calibrationParams.vaneADCValues[WMK_ANGLE_337_5] = 2810;
42-
43-
// The SparkFun Weather Shield Values uses a slightly different circuit, the
44-
// expected 12-bit values for it are specified here
45-
// _calibrationParams.vaneADCValues[WMK_ANGLE_0_0] = 3610;
46-
// _calibrationParams.vaneADCValues[WMK_ANGLE_22_5] = 2645;
47-
// _calibrationParams.vaneADCValues[WMK_ANGLE_45_0] = 2803;
48-
// _calibrationParams.vaneADCValues[WMK_ANGLE_67_5] = 1560;
49-
// _calibrationParams.vaneADCValues[WMK_ANGLE_90_0] = 1595;
50-
// _calibrationParams.vaneADCValues[WMK_ANGLE_112_5] = 1490;
51-
// _calibrationParams.vaneADCValues[WMK_ANGLE_135_0] = 1932;
52-
// _calibrationParams.vaneADCValues[WMK_ANGLE_157_5] = 1722;
53-
// _calibrationParams.vaneADCValues[WMK_ANGLE_180_0] = 2279;
54-
// _calibrationParams.vaneADCValues[WMK_ANGLE_202_5] = 2139;
55-
// _calibrationParams.vaneADCValues[WMK_ANGLE_225_0] = 3247;
56-
// _calibrationParams.vaneADCValues[WMK_ANGLE_247_5] = 3171;
57-
// _calibrationParams.vaneADCValues[WMK_ANGLE_270_0] = 3943;
58-
// _calibrationParams.vaneADCValues[WMK_ANGLE_292_5] = 3701;
59-
// _calibrationParams.vaneADCValues[WMK_ANGLE_315_0] = 3826;
60-
// _calibrationParams.vaneADCValues[WMK_ANGLE_337_5] = 3422;
23+
// results in 16 possible positions. Each position has a different resistor,
24+
// resulting in different ADC values. The expected ADC values has been
25+
// experiemntally determined for various platforms, see the constants file
26+
_calibrationParams.vaneADCValues[WMK_ANGLE_0_0] = SFE_WMK_ADC_ANGLE_0_0;
27+
_calibrationParams.vaneADCValues[WMK_ANGLE_22_5] = SFE_WMK_ADC_ANGLE_22_5;
28+
_calibrationParams.vaneADCValues[WMK_ANGLE_45_0] = SFE_WMK_ADC_ANGLE_45_0;
29+
_calibrationParams.vaneADCValues[WMK_ANGLE_67_5] = SFE_WMK_ADC_ANGLE_67_5;
30+
_calibrationParams.vaneADCValues[WMK_ANGLE_90_0] = SFE_WMK_ADC_ANGLE_90_0;
31+
_calibrationParams.vaneADCValues[WMK_ANGLE_112_5] = SFE_WMK_ADC_ANGLE_112_5;
32+
_calibrationParams.vaneADCValues[WMK_ANGLE_135_0] = SFE_WMK_ADC_ANGLE_135_0;
33+
_calibrationParams.vaneADCValues[WMK_ANGLE_157_5] = SFE_WMK_ADC_ANGLE_157_5;
34+
_calibrationParams.vaneADCValues[WMK_ANGLE_180_0] = SFE_WMK_ADC_ANGLE_180_0;
35+
_calibrationParams.vaneADCValues[WMK_ANGLE_202_5] = SFE_WMK_ADC_ANGLE_202_5;
36+
_calibrationParams.vaneADCValues[WMK_ANGLE_225_0] = SFE_WMK_ADC_ANGLE_225_0;
37+
_calibrationParams.vaneADCValues[WMK_ANGLE_247_5] = SFE_WMK_ADC_ANGLE_247_5;
38+
_calibrationParams.vaneADCValues[WMK_ANGLE_270_0] = SFE_WMK_ADC_ANGLE_270_0;
39+
_calibrationParams.vaneADCValues[WMK_ANGLE_292_5] = SFE_WMK_ADC_ANGLE_292_5;
40+
_calibrationParams.vaneADCValues[WMK_ANGLE_315_0] = SFE_WMK_ADC_ANGLE_315_0;
41+
_calibrationParams.vaneADCValues[WMK_ANGLE_337_5] = SFE_WMK_ADC_ANGLE_337_5;
6142

6243
// Datasheet specifies 2.4kph of wind causes one trigger per second
6344
_calibrationParams.kphPerCountPerSec = 2.4;
@@ -120,7 +101,7 @@ void SFEWeatherMeterKit::setADCResolutionBits(uint8_t resolutionBits)
120101
{
121102
for(uint8_t i = 0; i < WMK_NUM_ANGLES; i++)
122103
{
123-
int8_t bitShift = (SFE_WIND_VANE_ADC_RESOLUTION_DEFAULT) - resolutionBits;
104+
int8_t bitShift = (SFE_WMK_ADC_RESOLUTION) - resolutionBits;
124105

125106
if(bitShift > 0)
126107
{

src/SparkFun_Weather_Meter_Kit_Arduino_Library.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,7 @@
22
#define __SPARKFUN_WEATHER_METER_KIT_H__
33

44
#include "Arduino.h"
5-
6-
// Enum to define the indexes for each wind direction
7-
enum SFEWeatherMeterKitAnemometerAngles
8-
{
9-
WMK_ANGLE_0_0 = 0,
10-
WMK_ANGLE_22_5,
11-
WMK_ANGLE_45_0,
12-
WMK_ANGLE_67_5,
13-
WMK_ANGLE_90_0,
14-
WMK_ANGLE_112_5,
15-
WMK_ANGLE_135_0,
16-
WMK_ANGLE_157_5,
17-
WMK_ANGLE_180_0,
18-
WMK_ANGLE_202_5,
19-
WMK_ANGLE_225_0,
20-
WMK_ANGLE_247_5,
21-
WMK_ANGLE_270_0,
22-
WMK_ANGLE_292_5,
23-
WMK_ANGLE_315_0,
24-
WMK_ANGLE_337_5,
25-
WMK_NUM_ANGLES
26-
};
27-
28-
#define SFE_WIND_VANE_DEGREES_PER_INDEX (360.0 / WMK_NUM_ANGLES)
29-
#define SFE_WIND_VANE_ADC_RESOLUTION_DEFAULT 12
5+
#include "SparkFun_Weather_Meter_Kit_Constants.h"
306

317
// Calibration parameters for each sensor
328
struct SFEWeatherMeterKitCalibrationParams
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Enum to define the indexes for each wind direction
2+
enum SFEWeatherMeterKitAnemometerAngles
3+
{
4+
WMK_ANGLE_0_0 = 0,
5+
WMK_ANGLE_22_5,
6+
WMK_ANGLE_45_0,
7+
WMK_ANGLE_67_5,
8+
WMK_ANGLE_90_0,
9+
WMK_ANGLE_112_5,
10+
WMK_ANGLE_135_0,
11+
WMK_ANGLE_157_5,
12+
WMK_ANGLE_180_0,
13+
WMK_ANGLE_202_5,
14+
WMK_ANGLE_225_0,
15+
WMK_ANGLE_247_5,
16+
WMK_ANGLE_270_0,
17+
WMK_ANGLE_292_5,
18+
WMK_ANGLE_315_0,
19+
WMK_ANGLE_337_5,
20+
WMK_NUM_ANGLES
21+
};
22+
23+
// Angle per index of wind vane (360 / 16 = 22.5)
24+
#define SFE_WIND_VANE_DEGREES_PER_INDEX (360.0 / WMK_NUM_ANGLES)
25+
26+
// The ADC of each platform behaves slightly differently. Some have different
27+
// resolutions, some have non-linear outputs, and some voltage divider circuits
28+
// are different. The expected ADV values have been obtained experimentally for
29+
// various platforms below
30+
#ifdef AVR
31+
// Tested with RedBoard Qwiic with Weather Shield
32+
#define SFE_WMK_ADC_ANGLE_0_0 902
33+
#define SFE_WMK_ADC_ANGLE_22_5 661
34+
#define SFE_WMK_ADC_ANGLE_45_0 701
35+
#define SFE_WMK_ADC_ANGLE_67_5 389
36+
#define SFE_WMK_ADC_ANGLE_90_0 398
37+
#define SFE_WMK_ADC_ANGLE_112_5 371
38+
#define SFE_WMK_ADC_ANGLE_135_0 483
39+
#define SFE_WMK_ADC_ANGLE_157_5 430
40+
#define SFE_WMK_ADC_ANGLE_180_0 570
41+
#define SFE_WMK_ADC_ANGLE_202_5 535
42+
#define SFE_WMK_ADC_ANGLE_225_0 812
43+
#define SFE_WMK_ADC_ANGLE_247_5 792
44+
#define SFE_WMK_ADC_ANGLE_270_0 986
45+
#define SFE_WMK_ADC_ANGLE_292_5 925
46+
#define SFE_WMK_ADC_ANGLE_315_0 957
47+
#define SFE_WMK_ADC_ANGLE_337_5 855
48+
49+
#define SFE_WMK_ADC_RESOLUTION 10
50+
#elif ESP32
51+
// Tested with ESP32 processor board installed on Weather Carrier
52+
#define SFE_WMK_ADC_ANGLE_0_0 3118
53+
#define SFE_WMK_ADC_ANGLE_22_5 1526
54+
#define SFE_WMK_ADC_ANGLE_45_0 1761
55+
#define SFE_WMK_ADC_ANGLE_67_5 199
56+
#define SFE_WMK_ADC_ANGLE_90_0 237
57+
#define SFE_WMK_ADC_ANGLE_112_5 123
58+
#define SFE_WMK_ADC_ANGLE_135_0 613
59+
#define SFE_WMK_ADC_ANGLE_157_5 371
60+
#define SFE_WMK_ADC_ANGLE_180_0 1040
61+
#define SFE_WMK_ADC_ANGLE_202_5 859
62+
#define SFE_WMK_ADC_ANGLE_225_0 2451
63+
#define SFE_WMK_ADC_ANGLE_247_5 2329
64+
#define SFE_WMK_ADC_ANGLE_270_0 3984
65+
#define SFE_WMK_ADC_ANGLE_292_5 3290
66+
#define SFE_WMK_ADC_ANGLE_315_0 3616
67+
#define SFE_WMK_ADC_ANGLE_337_5 2755
68+
69+
#define SFE_WMK_ADC_RESOLUTION 12
70+
#else
71+
// Values calculated assuming 10k pullup and perfectly linear 16-bit ADC
72+
#define SFE_WMK_ADC_ANGLE_0_0 50294
73+
#define SFE_WMK_ADC_ANGLE_22_5 25985
74+
#define SFE_WMK_ADC_ANGLE_45_0 29527
75+
#define SFE_WMK_ADC_ANGLE_67_5 5361
76+
#define SFE_WMK_ADC_ANGLE_90_0 5958
77+
#define SFE_WMK_ADC_ANGLE_112_5 4219
78+
#define SFE_WMK_ADC_ANGLE_135_0 11818
79+
#define SFE_WMK_ADC_ANGLE_157_5 8099
80+
#define SFE_WMK_ADC_ANGLE_180_0 18388
81+
#define SFE_WMK_ADC_ANGLE_202_5 15661
82+
#define SFE_WMK_ADC_ANGLE_225_0 40329
83+
#define SFE_WMK_ADC_ANGLE_247_5 38365
84+
#define SFE_WMK_ADC_ANGLE_270_0 60494
85+
#define SFE_WMK_ADC_ANGLE_292_5 52961
86+
#define SFE_WMK_ADC_ANGLE_315_0 56785
87+
#define SFE_WMK_ADC_ANGLE_337_5 44978
88+
89+
#define SFE_WMK_ADC_RESOLUTION 16
90+
91+
// Set macro to indicate that this platform isn't known
92+
#define SFE_WMK_PLAFTORM_UNKNOWN
93+
#endif

0 commit comments

Comments
 (0)