首页 文章

根据频率值计算风速

提问于
浏览
1

我有NRG#40风速传感器,输出频率是线性的,风速输出信号范围从0 Hz到125 Hz 0 Hz均值= 0.35 m / s和125 Hz = 96 m / s,传递函数是m / s =( Hz x 0.765)0.35如何将此传感器与Arduino mega连接以前我连接Adafruit(产品编号:1733),这是 output voltage 不是频率与风速呈线性关系,此代码为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);
}

1 回答

  • 1

    假设您使用Arduino UNO或Nano,一种简单的方法是将传感器连接到引脚D2或D3,可以用作中断引脚 . 然后,您可以创建一个函数或ISR,每次传感器发出脉冲时都会调用它 . 然后将新创建的函数附加到中断引脚 . 所以它看起来像这样 .

    byte sensorPin = 2;
    double pulses = 0;
    double wSpeed = 0;
    long updateTimer = 0;
    int updateDuration = 3000;
    
    void setup() {
      Serial.begin(115200);
      pinMode(sensorPin, INPUT_PULLUP);
      attachInterrupt(digitalPinToInterrupt(sensorPin), sensorISR, FALLING);
    }
    
    void loop() {
      long now = millis();
      if(updateTimer < now) {
        updateTimer = now + updateDuration;
        wSpeed = ((pulses/(updateDuration/1000)) * 0.765) + 0.35;
        pulses = 0;
        Serial.println("Windspeed is:" + String(wSpeed));
      }
    }
    
    void sensorISR() {
      pulses++;
    }
    

    ISR功能的唯一作用是增加每个脉冲的脉冲变量 . 然后你可以每秒计算出频率和速度 . 如果您等待3秒而不是像上面那样,您将获得更好的分辨率,但必须考虑等式中的额外时间 .

    我没有测试这段代码 .

相关问题