我有NRG#40风速传感器输出频率与风速呈线性关系 输出信号范围为0 Hz至125 Hz 0 Hz均值= 0.35 m / s,125 Hz = 96 m / s,传递函数为 m / s =(Hz×0.765)+ 0.35 如何使用Arduino mega连接此传感器 以前我连接Adafruit(产品编号:1733)输出电压不频率与风速呈线性关系 和Adafruit的代码:
//Setup Variables
const int sensorPin = A0; //Defines the pin that the anemometer output is connected to
int sensorValue = 0; //Variable stores the value direct from the analog pin
float sensorVoltage = 0; //Variable that stores the voltage (in Volts) from the anemometer being sent to the analog pin
float windSpeed = 0; // Wind speed in meters per second (m/s)
float voltageConversionConstant = .004882814; //This constant maps the value provided from the analog read function, which ranges from 0 to 1023, to actual voltage, which ranges from 0V to 5V
int sensorDelay = 1000; //Delay between sensor readings, measured in milliseconds (ms)
//Anemometer Technical Variables
//The following variables correspond to the anemometer sold by Adafruit, but could be modified to fit other anemometers.
float voltageMin = .4; // Mininum output voltage from anemometer in mV.
float windSpeedMin = 0; // Wind speed in meters/sec corresponding to minimum voltage
float voltageMax = 2.0; // Maximum output voltage from anemometer in mV.
float windSpeedMax = 32; // Wind speed in meters/sec corresponding to maximum voltage
void setup()
{
Serial.begin(9600); //Start the serial connection
}
void loop()
{
sensorValue = analogRead(sensorPin); //Get a value between 0 and 1023 from the analog pin connected to the anemometer
sensorVoltage = sensorValue * voltageConversionConstant; //Convert sensor value to actual voltage
//Convert voltage value to wind speed using range of max and min voltages and wind speed for the anemometer
if (sensorVoltage <= voltageMin){
windSpeed = 0; //Check if voltage is below minimum value. If so, set wind speed to zero.
}else {
windSpeed = (sensorVoltage - voltageMin)*windSpeedMax/(voltageMax - voltageMin); //For voltages above minimum value, use the linear relationship to calculate wind speed.
}
//Print voltage and windspeed to serial
Serial.print("Voltage: ");
Serial.print(sensorVoltage);
Serial.print("\t");
Serial.print("Wind speed: ");
Serial.println(windSpeed);
delay(sensorDelay);
}
答案 0 :(得分:1)
假设您使用Arduino UNO或Nano,一种简单的方法是将传感器连接到引脚D2或D3,可以用作中断引脚。 然后,您可以创建一个函数或ISR,每次传感器发出脉冲时都会调用它。然后将新创建的函数附加到中断引脚。 所以它看起来像这样。
<Button x:Name="reportPathSelectBtn" Grid.Row="1" Grid.Column="1" Margin="10,0,0,0" MinWidth="70" Width="Auto" Height="23" VerticalAlignment="Bottom"
Click="BtnSelectPath_Click" Background="#e3e3e3" Style="{StaticResource FolderOpenBtn}"/>
ISR功能的唯一作用是增加每个脉冲的脉冲变量。然后你可以每秒计算出频率和速度。如果您等待3秒而不是像上面那样,您将获得更好的分辨率,但必须考虑等式中的额外时间。
我没有测试这段代码。